• 5418阅读
  • 2回复

[提问]请问怎样让QT建的数据表可以显示中文 [复制链接]

上一主题 下一主题
离线zhaowanlan
 
只看楼主 倒序阅读 楼主  发表于: 2011-08-17
— 本帖被 XChinux 从 Qt基础编程 移动到本区(2013-04-01) —


我在QT中建了张数据表,在建表时输入的是中文,但是显示时却是乱码。后来百度,知道QT默认的编码(unicode)是不能显示中文的,需要来更改QT程序的编码。但是因为不太会编码,按找到的例子加入 QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));然后修改一些代码后还是不行。可能是我学QT的时间不长,改的不对。但是究竟需要对源代码进行哪些修改才行呢?还要请高人指点一下啊~~~
我的源代码:
以下是头文件内容:
  1. #ifndef CONNECTION_H
  2. #define CONNECTION_H
  3. #include <QTextCodec>
  4. #include <QTranslator>
  5. #include <QMessageBox>
  6. #include <QSqlDatabase>
  7. #include <QSqlError>
  8. #include <QSqlQuery>
  9. #include <QString.h>
  10. /*
  11.     This file defines a helper function to open a connection to an
  12.     in-memory SQLITE database and to create a test table.
  13.     If you want to use another database, simply modify the code
  14.     below. All the examples in this directory use this function to
  15.     connect to a database.
  16. */
  17. //! [0]
  18. static bool createConnection()
  19. {   QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
  20.     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
  21.     db.setDatabaseName(":memory:");
  22.     if (!db.open()) {
  23.         QMessageBox::critical(0, qApp->tr("Cannot open database"),
  24.             qApp->tr("Unable to establish a database connection.\n"
  25.                      "Click Cancel to exit."), QMessageBox::Cancel);
  26.         return false;
  27.     }
  28.     QSqlQuery query;
  29.     query.exec("create table tr(气虚) (tr(适宜) varchar(20), "              
  30.                "tr(少吃) varchar(20), tr(忌吃) varchar(20))");
  31.    query.exec("insert into QiXu values(tr('豆子'), tr('银耳'),tr( '薏仁粉'))");
  32.     。。。。。。
  33.     return true;
  34. }
  35. //! [0]
  36. #endif
  37. 我看有些欢迎界面显示中文的例子中说,使用中文要用tr(),但是我这么写有错,编译都没通过,不过我又不知道在数据表中应该怎么写~~~
  38. 然后是源文件:
  39. #include <QTextCodec>
  40. #include <QTranslator>
  41. #include <QtGui>
  42. #include <QtSql>
  43. #include "../connection1.h"
  44. void initializeMode1(QSqlTableModel *model1)
  45. {
  46.     model1->setTable(tr("气虚"));
  47.     model1->setEditStrategy(QSqlTableModel::OnManualSubmit);
  48.     model1->select();
  49. }
  50. ...然后建立视图view指向“气虚”表。
  51. QTableView *createView(QSqlTableModel *model,const QString &title = "")
  52. {
  53.     。。。
  54.     return view;
  55. }
  56. int main(int argc, char *argv[])
  57. {
  58.     QApplication app(argc, argv);
  59.     QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
  60.     if (!createConnection())
  61.         return 1;
  62.     QSqlTableModel model1;
  63.     initializeMode1(&model1);
  64.     
  65. #if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5) || defined(Q_WS_SIMULATOR)
  66.     QTabWidget *tabWidget = new QTabWidget;
  67.     tabWidget->addTab(createView(&model1), tr("气虚"));  
  68.     tabWidget->showMaximized();
  69. ......
  70. #endif
  71.     return app.exec();
  72. }

呃,我知道给一大段代码让人帮忙看很拙也很白,不过我确实是个菜鸟小白,看了帮助文档还是不会用那个tr()函数,能不能请高人给点提示啊?
离线zzjin

只看该作者 1楼 发表于: 2011-08-18
不仅仅是tr需要修改.要把local和string的编码都改正了.
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
离线绿叶无忧
只看该作者 2楼 发表于: 2011-09-22
我也遇到过这种情况,有时候是编码的问题,你也可以试试 gb18030   或者是  UTF-8,和gbk
快速回复
限100 字节
 
上一个 下一个