PDA

View Full Version : Qt Multi language QCombobox problem



vhptt
31st July 2012, 11:16
I have got a problem with multi language qt (change language on the fly). My form have a combobox which should be translated when language changed. When languageChanged, the app call method retranslateUi() to translate item in the combobox. The combobox have slot corresponding for signal currentIndexChanged().


void on_comboBox_currentIndexChanged(int index)
{
//do something
}

But when method retranslateUi() called, I do this:

void retranslateUi()
{
ui->comboBox->clear();
ui->comboBox->insertItems(0, QStringList()
<< QApplication::translate("SettingDialog", "English", 0, QApplication::UnicodeUTF8)
<< QApplication::translate("SettingDialog", "French", 0, QApplication::UnicodeUTF8)
);
}

Problem is: each statement in retranslateUi() will emit the signal currentIndexChanged(), then the slot will call again.

How can I avoid that ?

Oleg
31st July 2012, 11:27
One of the workarounds is to block signals:



void retranslateUi()
{
ui->comboBox->blockSignals(true);
ui->comboBox->clear();
ui->comboBox->insertItems(0, QStringList()
<< QApplication::translate("SettingDialog", "English", 0, QApplication::UnicodeUTF8)
<< QApplication::translate("SettingDialog", "French", 0, QApplication::UnicodeUTF8)
);
ui->comboBox->blockSignals(false);
}

vhptt
31st July 2012, 11:32
Thank you so much. I tried and everything work fine.

frankiefrank
11th October 2012, 10:09
Never saw this blockSignals(). Thanks for the tip!