今天写个用Qtdesigner 设计器设计的界面实现语言动态切换,程序的界面如下
我想实现下拉语言选择的combox实现语言的切换,其实现的代码如下
.pro文件
TRANSLATIONS = hellotr_CH.ts\
hellotr_EN.ts
QT += core gui
TARGET = hellotr
TEMPLATE = app
SOURCES += main.cpp\
dialog.cpp
HEADERS += dialog.h
FORMS += dialog.ui
OTHER_FILES += \
hellotr_EN.qm \
hellotr_CH.qm
做了两个语言文件,一个英文-英文,一个英文-中文。
在建好工程,程序代码写完后编译会在工程的源文件目录下生成两个文件hellotr_CH.ts\
hellotr_EN.ts
在Qt creator中 工具->外部->Qt 预言家,更新lupdate,后
打开Qt库的bin文件夹中有个可执行文件linguist,在这个软件中打开上面的两个文件,翻译,最后保存,发布,编译工程后,工具->外部->Qt 语言家,release发布,只执行程序
注意:*.qm文件要拷贝到build目录下执行
void Dialog::switchlanguage(int flag)
{
qDebug("flag=%d",flag);
QTranslator translator(qApp);
bool b = false;
if(flag==0)
{
//chinese
b = translator.load("hellotr_CH.qm");
if(b==false)
qDebug("load err!");
else
qDebug("chinese load ok");
qApp->installTranslator(&translator);
// this->ui->retranslateUi(this);
}
else if(flag==1)
{
// english
b = translator.load("hellotr_EN.qm");
if(b==false)
qDebug("load err!");
else
qDebug("english load ok");
qApp->installTranslator(&translator);
this->ui->retranslateUi(this);
}
}
void Dialog:: to_chinese(void){
qDebug("to chinese clicked");QTranslator translator(qApp);bool b = false;b = translator.load("hellotr_CH.qm");if(b==false)qDebug("load err!");elseqDebug("chinese load ok");qApp->installTranslator(&translator);this->ui->retranslateUi(this);}
void Dialog::to_english(void){
qDebug("to_english clicked");QTranslator translator(qApp);bool b = false;b = translator.load("hellotr_EN.qm");if(b==false)qDebug("load err!");elseqDebug("english load ok");qApp->installTranslator(&translator);this->ui->retranslateUi(this);}
早构造函数中已经定义的槽函数连接
connect(this->ui->pushButton_chinese,SIGNAL(clicked()),SLOT(to_chinese()));
connect(this->ui->pushButton_english,SIGNAL(clicked()),this,SLOT(to_english()));
connect(this->ui->comboBox,SIGNAL(currentIndexChanged(int)),SLOT(switchlanguage(int)));
当我点击界面的英语,或者汉语时可以切换到英语或者汉语,但是当用combox来切换语言不能执行,不知道为什么
现象是我选择英语后它会显示英语那个文档加载成功,但是紧接着combox回到index为-1,0,接着测试,在点击按钮的时候也会这样,输出
to chinese clicked
chinese load ok
flag=-1
flag=0
最后一句句屏蔽发现是this->ui->retranslateUi(this)这句话引起的,
追踪这个函数
void retranslateUi(QDialog *Dialog)
{
Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", 0, QApplication::UnicodeUTF8));
pushButton->setText(QApplication::translate("Dialog", "hello", 0, QApplication::UnicodeUTF8));
pushButton_2->setText(QApplication::translate("Dialog", "close", 0, QApplication::UnicodeUTF8));
comboBox->clear();
comboBox->insertItems(0, QStringList()
<< QApplication::translate("Dialog", "chinese", 0, QApplication::UnicodeUTF8)
<< QApplication::translate("Dialog", "english", 0, QApplication::UnicodeUTF8)
);
const bool __sortingEnabled = listWidget->isSortingEnabled();
listWidget->setSortingEnabled(false);
QListWidgetItem *___qlistwidgetitem = listWidget->item(0);
___qlistwidgetitem->setText(QApplication::translate("Dialog", "item1", 0, QApplication::UnicodeUTF8));
QListWidgetItem *___qlistwidgetitem1 = listWidget->item(1);
___qlistwidgetitem1->setText(QApplication::translate("Dialog", "item2", 0, QApplication::UnicodeUTF8));
QListWidgetItem *___qlistwidgetitem2 = listWidget->item(2);
___qlistwidgetitem2->setText(QApplication::translate("Dialog", "item3", 0, QApplication::UnicodeUTF8));
QListWidgetItem *___qlistwidgetitem3 = listWidget->item(3);
___qlistwidgetitem3->setText(QApplication::translate("Dialog", "item4", 0, QApplication::UnicodeUTF8));
listWidget->setSortingEnabled(__sortingEnabled);
pushButton_english->setText(QApplication::translate("Dialog", "to_english", 0, QApplication::UnicodeUTF8));
pushButton_chinese->setText(QApplication::translate("Dialog", "to_chinese", 0, QApplication::UnicodeUTF8));
} // retranslateUi
再看看combox函数
This signal is sent whenever the currentIndex in the combobox changes either through user interaction or programmatically. The item's index is passed or -1 if the combobox becomes empty or the currentIndex was reset.
This signal is sent when the user chooses an item in the combobox. The item's index is passed. Note that this signal is sent even when the choice is not changed. If you need to know when the choice actually changes, use signal currentIndexChanged().
原因应该是在于
comboBox->insertItems(0, QStringList()
<< QApplication::translate("Dialog", "chinese", 0, QApplication::UnicodeUTF8)
<< QApplication::translate("Dialog", "english", 0, QApplication::UnicodeUTF8)
);
但是具体的我不知道怎么办
无奈,最后换成
信号,程序可以运行了