How to translate variable text in Qt? - c++

I have a difficult problem and tried too many things but in vain.
I have 4 translation files for 4 different languages, and whenever the user changes the language, I need to re-translate the UI.
Fixed text can be re-translated as follows:
ui->BusinessNameHelpText->setText(tr("Enter the business name."));
However, the variable text like the action on a button can't be done this way, because it's either “Install” or “Update” for example.
I tried this code block:
QString action = ui->actionButton->text();
ui->retranslateUi(this);
ui->actionButton->setText(trUtf8(action.toUtf8().data()));
And this:
QString action = ui->actionButton->text();
ui->retranslateUi(this);
ui->actionButton->setText(tr(action.toUtf8().data()));
And this:
QString action = ui->actionButton->text();
ui->retranslateUi(this);
ui->actionButton->setText(tr(action.toStdString.c_str()));
This works for only the first time when I change language, but after that, it doesn't work.

It sounds like you have code like
ui->actionButton->setText(isInstall ? tr("Install") : tr("Update"));
in your program. You need to run that again, in response to the language change event.

Ok now I get it what is actually your problem.
Problem is how you treat change text of button.
It works only when you change English to other langue. It happens since English text is equivalent to text you have used in code as identifies of translation.
When your application calls this:
QString action = ui->actionButton->text();
if previously English was selected action will contain text which is same as identifier of translation.
if previously other langue was selected action will contain something else then identifier and translator is unable to find matching translation. In such case translator will return original value.
To fix just leave only ui->retranslateUi(this); and thrash other code and make sure your UI has all respective strings marked that they need translation.

I've found a post on Qt Forum which shows a problem like mine, and this answer says that it's impossible to translate dynamic text.

Related

Can I use a variable as a text property when calling class window?

I have a child object class window with a parameter regexptitle, so text being changed dynamically depending on the regular expression. I need to check if this window is opened and active using variable. I tried to put there string but it didn't help. Please help me find a solution.
Example of code repeated:
If Window("Excel").Window("Prompts for Project Analysis"). Exist Then ...
If Window("Excel").Window("Prompts for Engagements"). Exist Then ...
Assuming I read your question correctly, you should be able to achieve the goal using descriptive programming with this:
If Window("Excel").Window("regexptitle:=Prompts for.*"). Exist Then
Or if you have at least one version of the window learned in the object repository, add the regexptitle property to the test object details under Description properties, set it to a regular expression and set its value to be "Prompts for.*" - this will cause UFT to recognise all windows of this class with a regexptitle beginning "Prompts for" as this object (assuming the other recognition properties match up as well, and you get to use the OR-friendly object name in your code.
Let me know if that works for you, or if you need further help.

Strange symbol appears in GtkTreeView

I am writting log viewer with GTK for Windows. I use GtkTreeView widget to display log records. It contains 3 columns: date & time, event source, event text. For any reason, in event time column strange symbol appears:
I used debugger watch window to see string value, and it doesn't contain any extra characters that can result in this strange symbol appearence.
What are possible reasons of such tree view behavior?
All text used in Gtk+-2.0 widgets needs to be in UTF8 encoding.Text in plain ASCII is automatically valid UTF8, but as soon as you have special characters that do not exist in plain ASCII (usually characters that are not used in the English language alphabet), they need to be in UTF8 encoding.Otherwise you may get what you are getting now.
This is something weird but gtk use to have this problem if you do not use glib library functions. You need to handle all of the strings with this functions. This could be a rendering issue.
for paths you use g_path_get_basename(path);
for dirs you use g_path_get_dirname (path);
for date and time use some of this docs gtk date and time
so it can be displayed properly in the treeview

How to highlight a string of text within a QTextEdit

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);

HTML entities in Textile

HTML entities are not displayed correctly in Textile because the ampersand is converted into & by the system. Is there any way to input e.g. ⌘ and actually get ⌘?
Wrapping the entity in ==⌘== does disable Textile processing for that block. Maybe that's as good as it gets?
I'm using RedCloth.
After having had a look through RedCloth sources, there does not seem to be a way of doing this.
Maybe it's as well, on second thought: Textile is not necessarily meant to output HTML, but HTML entities are entirely an HTML solution. I'll just input funky characters directly.
RedCloth does not get hex values in html entities, but others, like or € are processed properly.

How to load qt linguist dynamically changed label text

In my project i'm trying to use qt linguist. When i change the language from English to Turkish, it is working all constant label.
But some labels i m loading them dynamically according to scenario of use cases.
Whatever i do with qt linguist, it doest workthe texts of these label.
How can i get rid of this problem?
Any help will be apprecialted
Qt has a guide to internationalization, which includes the basic information: to wrap your string in a tr function call.
label->setText( tr( "Hello, World!" ) );
In addition to this, if you want the language to change on the fly, you'll need to identify when the context has changed, and update your labels appropriately. Unfortunately, I can't easily put my hands on the signal that indicates when to do so.