Solved Translate texts in QTableWidget
-
I started using Qt Linguist yesterday and I was able to successfully translate my application to the desired language but only the texts in widgets.
My application has a
QTableWidget
with headers and texts to be added dynamically in the rows that need to be translated but they aren't translated together with the widgets' texts.MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); ui->tableWidget->setColumnCount(3); QStringList headers; headers << tr("Column1") << tr("Column2") << tr("Column3") ; ui->tableWidget->setHorizontalHeaderLabels(headers); if(translator.load(":/Languages/Languages/translation.qm")) qDebug() << "OK"; }
I thought that by using the function
tr()
inQString
literals would suffice to make it translatable.Also I have a function that adds rows to the table and they have texts to be translated too.
What am I am missing?
-
@hbatalha Hi,
how is Linguist supposed to know anything about the data that is dynamically added to the widget during the runtime?Usual approach would be to have different (translated or otherwise locale aware) data per language and load them depending on the selected language. But you need to take care of that behaviour yourself.
-
how is Linguist supposed to know anything about the data that is dynamically added to the widget during the runtime?
But it is just text, a
QString
declared in the source code.Usual approach would be to have different (translated or otherwise locale aware) data per language and load them depending on the selected language.
Any reference?
-
@hbatalha If that text is in the code and, as you say, included in tr(), then I misunderstood. Sorry.
Can you show that code please?
Also, please make sure that your translations adhere to the notation the translator expects. -
@hbatalha said in Translate texts in QTableWidget:
QStringList headers;
headers << tr("Column1") << tr("Column2") << tr("Column3") ;ui->tableWidget->setHorizontalHeaderLabels(headers); if(translator.load(":/Languages/Languages/translation.qm")) qDebug() << "OK";
You're installing the translator AFTER you ask for the translation of headers (the
tr()
calls)BTW, the translator(s) is(are) installed at QApp level so when you're working with any widgets (i.e. MainWindow) the translated strings are available
-
@artwaw said in Translate texts in QTableWidget:
Can you show that code please?
It is something like this:
void MainWindow::loadSettings() { QSettings qsettings(QSettings::IniFormat, QSettings::UserScope, "HBatalha", "Table"); int size = qsettings.beginReadArray("Table"); for(int i = 0; i < size; ++i) { QStringList args = qsettings.value("args").toString().split(" "); FooClass::Status status = static_cast<FooClass::Status>(qsettings.value("status").toInt()); const int dest_row = ui->tableWidget->rowCount(); ui->tableWidget->setRowCount(dest_row + 1); QString status_str; switch (status) { case Downloader::Status::DONE: status_str= tr("Done"); // Here break; case Downloader::Status::PAUSED: status_str= tr("Paused"); // and here break; } ui->tableWidget->setItem(dest_row, 0, new QTableWidgetItem(status_str); ui->tableWidget->setItem(dest_row, 1, new QTableWidgetItem("Text2")); } qsettings.endArray(); }
-
@Pablo-J-Rogina said in Translate texts in QTableWidget:
You're installing the translator AFTER you ask for the translation of headers (the tr() calls)
I changed that and it it is still the same.
-
A little update: I was able to translate the headers by disabling the code
ui->tableWidget->setHorizontalHeaderLabels(headers);
in the constructor and add the headers in the Design. -
@hbatalha
Hi
tr() does make it translatable.
BUT
all text is not by reference so
if you give some text to a widget and then load a new translator the
text in the widget is not affected as its a copy.so to use the new text, you must set the text again.
the reason it works when done via design is that code is generated to reapply it
void retranslateUi(QMainWindow *MainWindow) { QTableWidgetItem *___qtablewidgetitem = tableWidget->horizontalHeaderItem(0); ___qtablewidgetitem->setText(QCoreApplication::translate("MainWindow", "New Column", nullptr));
-
@mrjj
Thank you very much for your insight, this solved my problem.I created a function that retranslates the table.