- 
UID:116616 
 
- 
- 注册时间2011-05-10
 
- 最后登录2011-08-28
 
- 在线时间17小时
 
 
 
- 
- 发帖10
 
- 搜Ta的帖子
 
- 精华0
 
- 金钱100
 - 威望20
 - 贡献值0
 - 好评度10
 
 
 
- 
访问TA的空间加好友用道具
 
 
 
 
 
 
  
 
 | 
 —
本帖被 XChinux 从 Qt基础编程 移动到本区(2013-04-01)
—
 
 
     在QT中建了张 数据表,在建表时输入的是中文,但是 显示时却是 乱码。后来百度,知道 QT默认的编码(unicode)是不能显示中文的,需要来更改QT程序的编码。但是因为不太会编码,按找到的例子加入[font='Courier New', monospace] [font='Courier New', monospace]QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));然后修改一些代码后还是不行。可能是我学QT的时间不长,改的不对。但是究竟需要对源代码进行哪些修改才行呢?还要请高人指点一下啊~~~我的源代码:- 以下是头文件内容:
 - #ifndef CONNECTION_H
 - #define CONNECTION_H
 - #include <QTextCodec>
 - #include <QTranslator>
 - #include <QMessageBox>
 - #include <QSqlDatabase>
 - #include <QSqlError>
 - #include <QSqlQuery>
 - #include <QString.h>
 - /*
 -     This file defines a helper function to open a connection to an
 -     in-memory SQLITE database and to create a test table.
 -  
 -  
 -     If you want to use another database, simply modify the code
 -     below. All the examples in this directory use this function to
 -     connect to a database.
 - */
 - //! [0]
 - static bool createConnection()
 - {   QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
 -     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
 -     db.setDatabaseName(":memory:");
 -     if (!db.open()) {
 -         QMessageBox::critical(0, qApp->tr("Cannot open database"),
 -             qApp->tr("Unable to establish a database connection.\n"
 -                      "Click Cancel to exit."), QMessageBox::Cancel);
 -         return false;
 -     }
 -  
 -  
 -     QSqlQuery query;
 -     query.exec("create table tr(气虚) (tr(适宜) varchar(20), "              
 -                "tr(少吃) varchar(20), tr(忌吃) varchar(20))");
 -    query.exec("insert into QiXu values(tr('豆子'), tr('银耳'),tr( '薏仁粉'))");
 -     。。。。。。
 -     return true;
 - }
 - //! [0]
 -  
 -  
 - #endif
 -  
 - 我看有些欢迎界面显示中文的例子中说,使用中文要用tr(),但是我这么写有错,编译都没通过,不过我又不知道在数据表中应该怎么写~~~
 - 然后是源文件:
 - #include <QTextCodec>#include <QTranslator>#include <QtGui>#include <QtSql>#include "../connection1.h"void initializeMode1(QSqlTableModel *model1){model1->setTable(tr("气虚"));model1->setEditStrategy(QSqlTableModel::OnManualSubmit);model1->select();}QTableView *createView(QSqlTableModel *model,const QString &title = ""){QTableView *view = new QTableView;view->setModel(model);#if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5) || defined(Q_WS_SIMULATOR)Q_UNUSED(title)#elseimages->setWindowTitle(title);#endifreturn view;}int main(int argc, char *argv[]){QApplication app(argc, argv);QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));if (!createConnection())return 1;QSqlTableModel model1;initializeMode1(&model1);#if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5) || defined(Q_WS_SIMULATOR)QTabWidget *tabWidget = new QTabWidget;tabWidget->addTab(createView(&model2), "气虚");tabWidget->showMaximized();#elseQTableView *images = createView(&model1, QObject::tr("Table Model (气虚)"));气虚->show();#endifreturn app.exec();}
 
  呃,我知道给一大段代码让人帮忙看很拙也很白,不过我确实是个菜鸟小白,看了帮助文档还是不会用那个tr()函数,能不能请高人给点提示啊?  
 
 
 |