Chinese garbled characters appear during code writing in Qt4.
Find a lot on the internet can not, and finally see here http://blog.chinaunix.net/uid-20382483-id-3030806.html
Only the methods in it are available !!! I tried it myself. It is feasible !!!
My code is as follows:
1 # include <QApplication>
2 # include <QDirModel>
3 # include <QListView>
4 # include <QTreeView>
5 # include <QTableView>
6 # include <QSplitter>
7 # include <QTextCodec>
8
9 int main (int argc, char * argv [])
10 {
11 QApplication app (argc, argv );
12
13 QTextCodec: setCodecForTr (QTextCodec: codecForName ("utf8"); // allow the program to support Chinese characters (it only affects tr, so try to program using tr)
14
15 QDirModel dirModel;
16 QTreeView treeView;
17 QListView listView;
18 QTableView tableView;
19
20 // set to browse files in three ways
21 treeView. setModel (& dirModel );
22 listView. setModel (& dirModel );
23 tableView. setModel (& dirModel );
24
25 treeView. setSelectionMode (q1_actitemview: MultiSelection );
26 listView. setSelectionModel (treeView. selectionModel (); // set the same selection mode as treeView
27 tableView. setSelectionModel (treeView. selectionModel (); // set the same selection mode as treeView
28
29 QObject: connect (& treeView, SIGNAL (doubleClicked (QModelIndex), & listView, SLOT (setRootIndex (QModelIndex); // when double-clicking a tree node, expand all the files in the selected directory in the form of list
30 QObject: connect (& treeView, SIGNAL (doubleClicked (QModelIndex), & tableView, SLOT (setRootIndex (QModelIndex )));
31
32 QSplitter * splitter = new QSplitter;
33 splitter-> addWidget (& treeView );
34 splitter-> addWidget (& listView );
35 splitter-> addWidget (& tableView );
36 // splitter-> setWindowTitle (txtCode-> toUnicode ("instance used for Model/View "));
37 splitter-> setWindowTitle (QObject: tr ("instance used for Model/View "));
38 splitter-> show ();
39
40 return app.exe c ();
41}
Pay attention to lines 13th and lines 37th. Here is the method to prevent Chinese characters from being garbled.