QT Debug大集合 詳細(xì)講解
QT Debug大集合 詳細(xì)講解是本文要介紹的內(nèi)容,相信友們應(yīng)該在編程過程中遇到各種各樣的Debug,先來看內(nèi)容。QT Debug集錦~ 這篇是在10年測試QT 過程中遇到的問題:
1、中文顯示問題:
- #include <QApplication>
- #include <QLabel>
- #include <QTextCodec>
- int main(int argc, char* argv[])
- {
- QApplication app(argc,argv);
- QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
- QLabel *label = new QLabel(tr("這里是中文"));
- label->Show();
- return app.exec();
- }
編譯代碼,得到的錯誤是: 'tr'在此作用域中尚未聲明。
昨天為什么沒有出現(xiàn)這種錯誤呢?因為昨天的代碼是從qt creator生成的MainWindow中挑出來的,tr被聲明為QObject的一個static方法,因此在MainWindow中使用tr不會有問題。
把上面的QLabel *label=new QLabel(tr("這里是中文"));
改為
QLabel *label=new QLabel(QObject::tr("這里是中文"));
2、中文問題:
使用sqlite數(shù)據(jù)庫顯示亂碼的問題
本人近日在使用QT進行sqlite數(shù)據(jù)庫編程時,出現(xiàn)中文數(shù)據(jù)顯示亂碼情況,附源碼如下:
- //main.cpp
- #include <QtGui>
- #include <QtCore/QTextCodec>
- #include <QSqlTableModel>
- #include <QTableView>
- #include <QHeaderView>
- #include <QSqlRecord>
- #include <QtGui/QLabel>
- #include <QString>
- #include <QVariant>
- #include "connection.h"
- #include "sql.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
- //創(chuàng)建數(shù)據(jù)庫連接
- if (!createConnection())
- return 1;
- //創(chuàng)建學(xué)生信息表
- createTables();
- //初始添加數(shù)據(jù)
- addData();
- enum{
- Student_Id = 0,
- Student_Schnum = 1,
- Student_Name = 2,
- Student_Sex = 3,
- Student_Nation = 4
- };
- QSqlTableModel *model = new QSqlTableModel();
- model->setTable("student");
- model->setSort(Student_Schnum, Qt::AscendingOrder);
- model->setHeaderData(Student_Schnum, Qt::Horizontal, QObject::tr("學(xué)號"));
- model->setHeaderData(Student_Name, Qt::Horizontal, QObject::tr("姓名"));
- model->setHeaderData(Student_Sex, Qt::Horizontal, QObject::tr("性別"));
- model->setHeaderData(Student_Nation, Qt::Horizontal, QObject::tr("民族"));
- model->select();
- QTableView *view = new QTableView;
- view->setModel(model);
- view->setSelectionMode(QAbstractItemView::SingleSelection);
- view->setSelectionBehavior(QAbstractItemView::SelectRows);
- view->setColumnHidden(Student_Id, true);
- view->resizeColumnsToContents();
- view->setEditTriggers(QAbstractItemView::NoEditTriggers);
- QHeaderView *header = view->horizontalHeader();
- header->setStretchLastSection(true);
- view->show();
- return a.exec();
- }
- //connection.h
- #ifndef CONNECTION_H
- #define CONNECTION_H
- #include <QMessageBox>
- #include <QSqlDatabase>
- #include <QSqlError>
- #include <QSqlDriver>
- inline bool createConnection()
- {
- QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
- db.setDatabaseName("sim.dat");
- if (!db.open()) {
- QMessageBox::warning(0, QObject::tr("Database Error"),
- db.lastError().text());
- return false;
- }
- return true;
- }
- #endif // CONNECTION_H
- //sql.h
- #include <QSqlQuery>
- #ifndef SQL_H
- #define SQL_H
- inline void createTables()
- {
- QSqlQuery query;
- query.exec("CREATE TABLE student ("
- "id INTEGER PRIMARY KEY, "
- "schnum INTEGER NOT NULL, "
- "name VARCHAR(40) NOT NULL, "
- "sex VARCHAR(4) NOT NULL, "
- "nation VARCHAR(10) NOT NULL)");
- }
- inline void addData(){
- QSqlQuery query;
- for(int i =0;i<100;i++){
- query.exec("INSERT INTO student (schnum, name, sex, nation) VALUES (2614103, '天殘腳,'男', '漢族')");
- }
- }
- #endif // SQL_H
上網(wǎng)查了許多無果,后來在閱讀一篇技術(shù)文章中無意發(fā)現(xiàn),原來在插入數(shù)據(jù)語句若有中文必須先QObject::tr()一番,即進行編碼,
- 將
- Sql.h
- 中
- query.exec("INSERT INTO student (schnum, name, sex, nation) VALUES (2614103, '天殘腳,'男', '漢族')");
- 改為如下
- query.exec(QObject::tr("INSERT INTO student (schnum, name, sex, nation) VALUES (2614103, '天殘腳,'男', '漢族')"));
結(jié)果在顯示中都能得正確顯示。
注意,如果語句 QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));中的編碼改為utf-8則會顯示亂碼。
3、中文問題:
如果使程序只支持一種編碼,也可以直接把整個應(yīng)用程序的編碼設(shè)置為GBK編碼, 然后在字符串之前 加
- tr(QObject::tr), qApp->setDefaultCodec( QTextCodec::codecForName("GBK") );
- QLabel *label = new QLabel( tr("中文標(biāo)簽") );
4、找不到<QtSql >
求助:提示無法打開包含文件QtSql
.Pro文件里加入 QT += sql
4、No rule to make target 'mkspecs/default/qmake.conf', needed by `Makefile'. Stop. 錯誤
- mingw32-make: *** No rule to make target `http://www.cnblogs.com/http://www.cnblogs.com/Qt/4.3.3/mkspecs/default/qmake.conf',
- needed by `makefile'. Stop.
- make[2]: Entering directory `/home/lzy/tps2/rplan/super'
- make[2]: *** No rule to make target `/home/lzy/qt/qt-3.3.2/mkspecs/default/qmake.conf', needed by `Makefile'. Stop.
- make[2]: Leaving directory `/home/lzy/tps2/rplan/super'
5、mingw32\bin\ld.exe: cannot find -lqtmaind錯誤
這個錯誤是缺少某些庫,將mingw重新下載安裝即可。
6、編譯時可能會遇到如下錯誤:previous declaration 'long int InterlockedIncrement(long int*)' here
此為qt的bug需要修改源代碼 (Qt\4.4.3\src\corelib\arch\qatomic_windows.h),原文件如下:
Solution:
(1) Qt\4.4.3\src\corelib\arch\qatomic_windows.h:
- #if !(defined Q_CC_BOR) || (__BORLANDC__ < 0x560)
- extern "C" {
- __declspec(dllimport) long __stdcall InterlockedCompareExchange(long *, long, long);
- __declspec(dllimport) long __stdcall InterlockedIncrement(long *);
- __declspec(dllimport) long __stdcall InterlockedDecrement(long *); __declspec(dllimport) long __stdcall InterlockedExchange(long *, long);
- __declspec(dllimport) long __stdcall InterlockedExchangeAdd(long *, long);
- }
- #else
- extern "C" {
- __declspec(dllimport) long __stdcall InterlockedCompareExchange(long volatile*, long, long);
- __declspec(dllimport) long __stdcall InterlockedIncrement(long volatile*);
- __declspec(dllimport) long __stdcall InterlockedDecrement(long volatile*);
- __declspec(dllimport) long __stdcall InterlockedExchange(long volatile*, long);
- __declspec(dllimport) long __stdcall InterlockedExchangeAdd(long volatile*, long);
- }
- #endif
- you will see above code in Qt\4.4.3\src\corelib\arch\qatomic_windows.h: file. I modified like below and it works.
- /*#if !(defined Q_CC_BOR) || (__BORLANDC__ < 0x560)
- extern "C" {
- __declspec(dllimport) long __stdcall InterlockedCompareExchange(long *, long, long);
- __declspec(dllimport) long __stdcall InterlockedIncrement(long *);
- __declspec(dllimport) long __stdcall InterlockedDecrement(long *); __declspec(dllimport) long __stdcall InterlockedExchange(long *, long);
- __declspec(dllimport) long __stdcall InterlockedExchangeAdd(long *, long);
- }
- #else */
- extern "C" {
- __declspec(dllimport) long __stdcall InterlockedCompareExchange(long volatile*, long, long);
- __declspec(dllimport) long __stdcall InterlockedIncrement(long volatile*);
- __declspec(dllimport) long __stdcall InterlockedDecrement(long volatile*);
- __declspec(dllimport) long __stdcall InterlockedExchange(long volatile*, long);
- __declspec(dllimport) long __stdcall InterlockedExchangeAdd(long volatile*, long);
- }
- // #endif
7、編譯錯誤,顯示 can not find -lqtmaind。
這是qt的debug庫,安裝完成后需要再自己編譯這個庫。在Qt的開始菜單中,你可以找到一個程序 Qt 4.4.0 (Build Debug Libraries),運行這個程序就能編譯Qt的Debug庫了。
小結(jié):QT debug大集合 詳細(xì)講解的內(nèi)容介紹完了,希望本文對你有搜幫助。