'QT: Proper way to change color according to translation
I have an unusual situation. The customer wants certain background colors changed depending on the current translations. Currently I am using the normal QT translator which works great for text however there is no option for QColor or any other type as far as I can see.
So my current workaround is that I also use the tr() for the setStyleSheet call.
As dynamic translation is also required I reimplemented the changeEvent to include
ui-> retranslate(this)
however this does not change the styleSheet. So I also added the
ui->pb->setStyleSheet(tr("background-color: rgb(150,150,150)"));
inside the changeEvent. As you can see below the implementation is not really elegant so I would like to know if there is a better, preferred alternative I can use.
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
uiSetupDone = true;
connect(...);
...
}
...
void MainWindow::changeEvent(QEvent *event)
{
// In the case of events changing the application language
if (event->type() == QEvent::LanguageChange) {
ui->retranslateUi(this); // translate the window again
//Check if the UI is fully setup so that we do not try to access not initialized variables
if (uiSetupDone)
{
//Update the background colors depending on the language
ui->pbProgramSelect->setStyleSheet(tr("background-color: rgb(150,150,150))":
...
}
}
QMainWindow::changeEvent(event);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
