tr() for plural forms is not defined in QML - qt

I'm trying to use tr() for defining texts with plural/singular forms of nouns in QML. But Qt Creator says tr() is not defined. Before I used qsTr() and it worked for me. Could you please help me to find the reason? Thank you
Example:
Text {
property int minutesValue: calculateMinutesLeft()
text: qsTr("%1 %2".arg(minutesValue).arg(minutesValue > 1 ? " minutes left": " minute left"))
}

Related

Behavior QT translate tr() method with arguments (%1)

Will dynamic translation work for such a code:
const QString myText = tr("%1 Hello World").arg(someVar);
I have few doubts:
1: Will the translation entry be generated for above code (when running lupdate). If yes, will the "%1" argument part be ignored?
2: Is the above code correct ? Should the dynamic part be translated separately before using it in the argument with tr. Provided we know all possible value of someVar
When your run lupdate, you will see this in your .ts file:
<source>%1 Hello World</source>
The translator will need to know they can just ignore the '%1' part.
If someVar is a number, there is no need to do anything else. If it's a string, it will need to be translated separately.

Highlight a text Qt [duplicate]

I'm a student programmer currently developing an application for work using Qt4. I am building an equation editor and I'm having issues attempting to highlight a string within my QTextEdit field. I have a function that parses through the QTextEdit string and returns an a start and end integer of where an error is located. My original strategy was to use HTML tags at these two points to highlight the error. Unfortunately there appears to be an issue with html tagging and the equation syntax.
What I think I need is a strategy that relies on Qt's library to set a background color between these two indices. I began looking a QSyntaxHighlighter; however I think that this is more for highlighting using a predefined set of laws and not for just grabbing up anything between a & b and setting the background color. If I can use syntax highlighter please provide me with and example or reference as I have already read through the documentation and didn't find anything.
Thanks for any help in advance!
P.S. Just to emphasize on the html compatibility issues; html becomes problematic due to multiple < and > signs used.
You can use QTextCursor and QTextCharFormat for it:
QTextEdit *edit = new QTextEdit;
...
int begin = ...
int end = ...
...
QTextCharFormat fmt;
fmt.setBackground(Qt::yellow);
QTextCursor cursor(edit->document());
cursor.setPosition(begin, QTextCursor::MoveAnchor);
cursor.setPosition(end, QTextCursor::KeepAnchor);
cursor.setCharFormat(fmt);

standard notation behind the QT reference documentation

I've searched extensively for an answer but to no avail. I am progressing well with self-tutoring of the basics of PyQT5. I am using http://pyqt.sourceforge.net to gain insight into how classes and functions link together but I am finding the QT reference documentation http://doc.qt.io/qt-5/ a useful means of gaining further insight.
I can pick up on the fact that there is a standard notation running through the documentation but I cannot follow what various symbols and reoccurring text refers to. There also appear to be many parameters and nested parameters applied. I paste a few samples below:
void setFocus(Qt::FocusReason reason)
render(QPainter *painter, const QPoint &targetOffset = QPoint(), const QRegion &sourceRegion = QRegion(), RenderFlags renderFlags = RenderFlags( DrawWindowBackground | DrawChildren ))
qmake: QT += widgets
enum RenderFlag { DrawWindowBackground, DrawChildren, IgnoreMask }
My question: is the documentation following an industry standard notation when it uses symbols such as "|" ? If so, is there a reference I can refer to to interpret the notation? I've combed through the QT website but can find nothing.
Qt is a C++ library and the snippets you are showing are excerpts from C++ code.
except for the qmake line which is qmake code.
| is the bitwise OR operator. The enum RenderFlag consists of flags (where usually one unique bit is set) which can be combined via this operator.

lupdate is not getting tr() strings from QObject objects

I've been trying to make a simple text editor using QT Creator. This text editor has a translatable UI that supports 3 languages: English, Spanish, and Portuguese.
My problem is that every time I run lupdate, string literals in my code that are tagged as translatable (i.e. enclosed in tr() ) are parsed and can be edited and translated using QLinguist. However, strings that are part of QObjects (E.g. ButtonText for QFileDialog are not parsed. Am I missing some procedure in order for lupdate to parse these strings?
void PeterTextEditor::on_actionOpen_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Document"), QDir::currentPath(),
tr("Text documents (*.txt)"), 0, QFileDialog::DontUseNativeDialog);
if (fileName.isNull())
return;
if ( m_fileName.isNull() && !isWindowModified() )
{
loadFile(fileName);
return;
}
else
{
PeterTextEditor * openFile = new PeterTextEditor( fileName );
openFile->show();
openFile->setAttribute(Qt::WA_DeleteOnClose);
}
}
In above example tr("Open Document") would be parsed, but QFileDialog has QButton and QLabel objects with strings for button text and label text.These are not parsed. I would like these to be parsed so I could add translations using QLinguist.
All compiled translation files (*.qm) should be in the /translations directory and you would load them as shown in QFileDialog localization.
Unfortunately, the Qt libraries don't come with all translations for all languages, and, not all the translations supplied are complete.
For the version that I have (Qt 5.2.1), there is a Spanish translation(qt_es.qm) of the libraries but there are many strings that have not been translated yet, and there is no translation file for Portuguese(qt_pt.qm).
The translation of Qt to other languages is an ongoing project so I suggest you search the web and/or other forums to see if anyone has got updated files you can use.
If you cannot find any, and your Spanish translation is missing a couple of strings you need, you can file the source extraction files(*.ts) in the /Src/qttranslations/translations directory. Unfortunately, you probably will find only one for Spanish.
If you're willing to start a Portuguese translation, you can extract all the necessary strings you want by running lupdate on the /Src/qtbase/qtbase.pro file.

Programmatically adding new line in mx:TextArea

I have a flex 4.5 application. I want to add new line in an mx:TextArea when certain event occurs. I have been searching for the proper way to add a OS independent line ending. I found out that the File class has lineEnding property. However the documentation states that this class is not exposed when running inside a browser (which is my case).
I have searched, but I couldn't find any other class, which can provide this information. Actually I am not sure if the TextArea line ending is OS dependent or not.
So actually I have two questions: Are TextArea line endings OS dependent or not? And if so, how can I get the proper line ending in flex?
You can use String.fromCharCode(13). This will return a line ending.
This is the equivalent of PHP's chr() method.
Example:
var address_str:String = "dog" + String.fromCharCode(64) + "house.net";
trace(address_str); // output: dog#house.net
From my experience, "\r" works in both Windows and Mac.
Quite simply, you just need to add the newline character to the text of the textArea.
myTextArea.text+="\n"; //This should work, if not try the other two
myTextArea.text+="\r";
myTextArea.text+="\r\n";

Resources