Qt多语言QCombobox
我在多语言qt(动态更改语言)方面遇到问题。我的表单有一个组合框,当语言发生变化时,它应该被翻译。当语言发生更改时,应用程序调用方法retranslateUi()将转换组合框中的项。组合框具有与信号currentIndexChanged()对应的插槽 但当调用方法retranslateUi()时,我会执行以下操作:Qt多语言QCombobox,qt,qcombobox,multilingual,Qt,Qcombobox,Multilingual,我在多语言qt(动态更改语言)方面遇到问题。我的表单有一个组合框,当语言发生变化时,它应该被翻译。当语言发生更改时,应用程序调用方法retranslateUi()将转换组合框中的项。组合框具有与信号currentIndexChanged()对应的插槽 但当调用方法retranslateUi()时,我会执行以下操作: void retranslateUi() { ui->comboBox->clear(); ui->comboBox->insertItems(0, QStr
void retranslateUi()
{
ui->comboBox->clear();
ui->comboBox->insertItems(0, QStringList()
<< QApplication::translate("SettingDialog", "English", 0, QApplication::UnicodeUTF8)
<< QApplication::translate("SettingDialog", "French", 0, QApplication::UnicodeUTF8)
);
}
void-retranslateUi()
{
用户界面->组合框->清除();
用户界面->组合框->插入项(0,QStringList()
clear()
和insertItems()
将触发combobox的currentIndexchanged(int)
SLOT函数,前者将索引更改为-1,而后者在位置0插入项时也将更改索引
因此,使用如下标志限制组合框上的\u currentIndexChanged(int)
void on_comboBox_currentIndexChanged(int index)
{
if(!retranslateFlag)
//do something
}
void retranslateUi()
{
retranslateFlag = true;
ui->comboBox->clear();
ui->comboBox->insertItems(0, QStringList()
<< QApplication::translate("SettingDialog", "English", 0, QApplication::UnicodeUTF8)
<< QApplication::translate("SettingDialog", "French", 0, QApplication::UnicodeUTF8)
);
retranslateFlag = false;
}
void on\u组合框\u currentIndexChanged(int索引)
{
如果(!retranslateFlag)
//做点什么
}
无效重新转换()
{
重新传输标志=真;
用户界面->组合框->清除();
用户界面->组合框->插入项(0,QStringList()
clear()
和insertItems()
将触发combobox的currentIndexchanged(int)
SLOT函数,前者将索引更改为-1,而后者在位置0插入项时也将更改索引
因此,使用如下标志限制组合框上的\u currentIndexChanged(int)
void on_comboBox_currentIndexChanged(int index)
{
if(!retranslateFlag)
//do something
}
void retranslateUi()
{
retranslateFlag = true;
ui->comboBox->clear();
ui->comboBox->insertItems(0, QStringList()
<< QApplication::translate("SettingDialog", "English", 0, QApplication::UnicodeUTF8)
<< QApplication::translate("SettingDialog", "French", 0, QApplication::UnicodeUTF8)
);
retranslateFlag = false;
}
void on\u组合框\u currentIndexChanged(int索引)
{
如果(!retranslateFlag)
//做点什么
}
无效重新转换()
{
重新传输标志=真;
用户界面->组合框->清除();
用户界面->组合框->插入项(0,QStringList()
另一种解决方案是临时阻止对象的信号:
void retranslateUi()
{
auto isBlocked = 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(isBlocked);
}
另一种解决方案是临时阻止对象的信号:
void retranslateUi()
{
auto isBlocked = 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(isBlocked);
}
您只能通过void QComboBox::setItemText(int index,const QString&text)
我假设您的解决方案和来自ScarCode的答案将丢失组合框中当前选定的项目。此解决方案不应丢失它。(未测试)而不是清除组合框,您只能通过void QComboBox::setItemText(int index,const QString&text)
我假设您的解决方案和来自ScarCode的答案将丢失组合框中当前选定的项。此解决方案不应丢失它。(未测试)
void retranslateUi()
{
ui->comboBox->setItemText(0, QApplication::translate("SettingDialog", "English", 0, QApplication::UnicodeUTF8));
ui->comboBox->setItemText(1, QApplication::translate("SettingDialog", "French", 0, QApplication::UnicodeUTF8));
}