QT4 Chinese garbled Solution

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.