第一次使用QT 在使用下面的函數時
1 void MainWindow::on_actionHehe_triggered() 2 { 3 QMessageBox::information(this,tr("Tips"),tr("Design Time : 2016-07-26"),QMessageBox::Yes);//tr("Tips") 4 } 5 有這樣的疑惑 tr這個函數是干嘛的 6 查看函數原型,是這樣的 7 static int information(QWidget *parent, const QString &title, 8 const QString& text, 9 const QString& button0Text, 10 const QString& button1Text = QString(), 11 const QString& button2Text = QString(), 12 int defaultButtonNumber = 0, 13 int escapeButtonNumber = -1); 14 這兩個參數只是一個QString的類 ,並且試過把tr去掉,函數變成這樣 15 16 void MainWindow::on_actionHehe_triggered() 17 { 18 QMessageBox::information(this,"Tips","Design Time : 2016-07-26",QMessageBox::Yes);//tr("Tips") 19 } 20 效果是一樣的 21 通過查網上的資料發現 22 是出於國際化的需要,將需要在界面上顯示的文件都用tr包起來,這有分兩種: 23 (2a) 用tr包住英文(最最推薦的用法,源碼英文,然后提供英文到其他語言的翻譯包) 24 (2b) 用tr包住中文(源碼用中文,然后提供中文到其他語言的翻譯包) 25 26 tr 是做什么的?下面二者的區別是什么? 27 28 QString text1 = QObject::tr("hello"); QString text2 = QString("hello"); 29 tr是用來實現國際化,如果你為這個程序提供了中文翻譯包(其中hello被翻譯成中文"你好"),那么text1的內容將是中文"你好";如果你為程序提供且使用日文翻譯包,那么text1的內容將是日文。 30 31 tr是經過多級函數調用才實現了翻譯操作,是有代價的,所以不該用的時候最好不要用。