The 2D drawing section has come to an end. I am still thinking about what to write in the following section. I planned to talk about the view-model issue first, but I saw a friend asking me about internationalization, so now let's talk about the internationalization of Qt! There are many internationalization methods in Qt, commonly used QTextCodec class and tr () function. The former writes the encoding name into the code. Unless you use Unicode encoding, internationalization is still a problem; the latter won't have this problem, and this is also recommended by Qt. Therefore, we mainly use the tr () function to internationalize applications. Let's first look at a very simple MainWindow. For clarity, here only the content of the cpp file is given: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> # include" mainwindow. h"
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> MainWindow: MainWindow (QWidget * parent)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/>: QMainWindow (parent)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> QMenuBar * menuBar = new QMenuBar (this );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> QMenu * fileMenu = new QMenu (tr (" & File "), menuBar );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif"/> QAction * newFile = new QAction (tr ("& New... "), fileMenu );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> fileMenu-> addAction (newFile );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif"/> QAction * openFile = new QAction (tr ("& Open... "), fileMenu );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> fileMenu-> addAction (openFile );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> menuBar-> addMenu (fileMenu );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> setMenuBar (menuBar );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif"/> connect (openFile, SIGNAL (triggered (), this, SLOT (fileOpen ()));
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> MainWindow ::~ MainWindow ()
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> void MainWindow: fileOpen ()
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> QFileDialog * fileDialog = new QFileDialog (this );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> fileDialog-> setWindowTitle (tr (" Open File "));
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> fileDialog-> setDirectory (".");
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> if (fileDialog-> exec () = QDialog: Accepted ){
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> QString path = fileDialog-> selectedFiles () [0];
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif"/> QMessageBox: information (NULL, tr ("Path "), tr ("You selected \ n % 1 "). arg (path ));
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/>} else {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif"/> QMessageBox: information (NULL, tr ("Path"), tr ("You didn't select any files. "));
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/>} This is a very simple class. The running result must be very clear: it is a main window with a menu bar and a File menu with two menu items: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1R4523C0-34.png "/> The reason why the runtime diagram is pasted is to make it visible to everyone. The & symbol in the Code is actually displayed as an underline on the interface, marking the shortcut key for this menu or menu item. According to the Code, when we click Open, a dialog box will pop up to open the file: 650) this. width = 650; "onclick = 'window. Open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1R4523192-35.png "/> The code in the slot is described in detail in the previous article. Maybe you will ask, why do you need to write this troublesome code? As we once said, using static functions is actually a direct call to the system dialog box, and this constructor method is drawn by Qt. This has a certain impact on the internationalization behind us. Now, we are all ready for internationalization. The so-called internationalization is not only about translating various texts on the interface into other languages, but also about the conversion of writing methods and currencies. For example, Arabic writing from right to left must be completed in internationalization. But here, we only do the simplest job, that is, to translate the text on the interface into Chinese. First, we need to add a line in the pro file: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> TRANSLATIONS + = myapp. ts myapp. ts is the translation file we need to create. The file name is arbitrary, but the suffix must be ts. Then, open the command prompt and enter the project directory. For example, My project is E: \ My Documents \ Workspace \ Qt \ MyApp, that is, the folder where the pro file is located, then enter the command 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> Lupdate MyApp. pro. If the command does not exist, add the bin directory of Qt to the environment variable. If the number of updates is reached, the ts file is successfully created: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1R452F58-38.png "/> The last line indicates that the first seven original texts to be translated exist. That is to say, this file is newly created. In this case, you will find the myapp. ts file in the project directory. Maybe you may wonder why the number already exists here? Because Qt is a smart tool that can recognize existing texts and modify or add new texts, you do not need to repeat the previous texts in future work. This is why the tool name is "lupdate", because it is "update", not just generated. If you are interested, open the ts file in Notepad. The file is actually an XML file with a clear structure. However, we need to use professional translation tools for translation. Qt provides a tool, Qt Linguist, which can be found in the Tools under the Qt item in the Start Menu. You can use it to open our ts file and perform our translation work: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1R4523N9-39.png "/> Save the file after the full translation is complete, and then there is a" publish "under the File menu ". Click this button, and a myapp. qm file will appear in the project directory. This is the file we have translated. The qm file of Qt is actually in binary format, so it is highly optimized and small in size. Next we need to modify the main () function to load this qm file: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> Int main (int argc, char * argv [])
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> QApplication a (argc, argv );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> QTranslator qtTranslator;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> qtTranslator. load (" myapp. qm ");
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> a. installTranslator (& qtTranslator );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> MainWindow w;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> w. resize (800,600 );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> w. show ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/> return a.exe c ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R4525430-0.gif "/>} note that the QTranslator class is actually under QtCore. The code is still clear: Create a QTranslator object, load the qm file, and install the object to the QApplication class. Okay, now we're done. Re-compile and run the program! Why? Why English? What's wrong? This is often confusing. In fact, this is because we use the load () function to load the qm file using a relative path, so that we directly load ("myapp. (qm). In fact, we will find this qm file in the directory where the compiled exe is located. Therefore, as long as we put the qm file in the same directory as the exe file and run it again: 650) this. width = 650; "onclick = 'window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1R452GM-51.png "/> now, this interface is already Chinese! In fact, this small detail shows that the qm file is actually dynamically loaded into the exe file, rather than directly compiled in. This provides the basis for dynamic language switching.
This article is from the "bean space" blog, please be sure to keep this source http://devbean.blog.51cto.com/448512/244689