自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

QT Debug大集合 詳細(xì)講解

移動開發(fā)
本文介紹的是QT 錯誤大集合 詳細(xì)講解,本文把在QT編程過程中遇到的問題集合了一下,大家一塊來分析解決,我們先來看內(nèi)容。

QT  Debug大集合 詳細(xì)講解是本文要介紹的內(nèi)容,相信友們應(yīng)該在編程過程中遇到各種各樣的Debug,先來看內(nèi)容。QT Debug集錦~ 這篇是在10年測試QT 過程中遇到的問題:

1、中文顯示問題:

  1. #include <QApplication> 
  2. #include <QLabel> 
  3. #include <QTextCodec> 
  4.  
  5. int main(int argc, char* argv[])  
  6. {  
  7.   QApplication app(argc,argv);  
  8.   QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));  
  9.   QLabel *label = new QLabel(tr("這里是中文"));  
  10.   label->Show();  
  11.   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ù)顯示亂碼情況,附源碼如下:
 

  1. //main.cpp  
  2. #include <QtGui> 
  3. #include <QtCore/QTextCodec> 
  4. #include <QSqlTableModel> 
  5. #include <QTableView> 
  6. #include <QHeaderView> 
  7. #include <QSqlRecord> 
  8. #include <QtGui/QLabel> 
  9. #include <QString> 
  10. #include <QVariant> 
  11. #include "connection.h"  
  12. #include "sql.h"  
  13. int main(int argc, char *argv[])  
  14. {  
  15.     QApplication a(argc, argv);  
  16.     QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));  
  17.  
  18.     //創(chuàng)建數(shù)據(jù)庫連接  
  19.     if (!createConnection())  
  20.         return 1;  
  21.     //創(chuàng)建學(xué)生信息表  
  22.     createTables();  
  23.     //初始添加數(shù)據(jù)  
  24.    addData();  
  25.     enum{  
  26.         Student_Id = 0,  
  27.         Student_Schnum = 1,  
  28.         Student_Name = 2,  
  29.         Student_Sex = 3,  
  30.         Student_Nation = 4 
  31.     };  
  32.     QSqlTableModel *model = new QSqlTableModel();  
  33.     model->setTable("student");  
  34.     model->setSort(Student_Schnum, Qt::AscendingOrder);  
  35.     model->setHeaderData(Student_Schnum, Qt::Horizontal, QObject::tr("學(xué)號"));  
  36.     model->setHeaderData(Student_Name, Qt::Horizontal, QObject::tr("姓名"));  
  37.     model->setHeaderData(Student_Sex, Qt::Horizontal, QObject::tr("性別"));  
  38.     model->setHeaderData(Student_Nation, Qt::Horizontal, QObject::tr("民族"));  
  39.     model->select();  
  40.  
  41.     QTableView *view = new QTableView;  
  42.     view->setModel(model);  
  43.     view->setSelectionMode(QAbstractItemView::SingleSelection);  
  44.     view->setSelectionBehavior(QAbstractItemView::SelectRows);  
  45.     view->setColumnHidden(Student_Id, true);  
  46.     view->resizeColumnsToContents();  
  47.     view->setEditTriggers(QAbstractItemView::NoEditTriggers);  
  48.  
  49.     QHeaderView *header = view->horizontalHeader();  
  50.     header->setStretchLastSection(true);  
  51.     view->show();  
  52.     return a.exec();  
  53. }  
  54. //connection.h  
  55. #ifndef CONNECTION_H  
  56. #define CONNECTION_H  
  57. #include <QMessageBox> 
  58. #include <QSqlDatabase> 
  59. #include <QSqlError> 
  60. #include <QSqlDriver> 
  61. inline bool createConnection()  
  62. {  
  63.     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");  
  64.     db.setDatabaseName("sim.dat");  
  65.     if (!db.open()) {  
  66.         QMessageBox::warning(0, QObject::tr("Database Error"),  
  67.                              db.lastError().text());  
  68.         return false;  
  69.     }  
  70.     return true;  
  71. }  
  72. #endif // CONNECTION_H  
  73. //sql.h  
  74. #include <QSqlQuery> 
  75. #ifndef SQL_H  
  76. #define SQL_H  
  77. inline void createTables()  
  78. {  
  79.     QSqlQuery query;  
  80.     query.exec("CREATE TABLE student ("  
  81.                "id INTEGER PRIMARY KEY, "  
  82.                "schnum INTEGER NOT NULL, "  
  83.                "name VARCHAR(40) NOT NULL, "  
  84.                "sex VARCHAR(4) NOT NULL, "  
  85.                "nation VARCHAR(10) NOT NULL)");  
  86. }  
  87. inline void addData(){  
  88.     QSqlQuery query;  
  89.     for(int i =0;i<100;i++){  
  90.     query.exec("INSERT INTO student (schnum, name, sex, nation) VALUES (2614103, '天殘腳,'男', '漢族')");  
  91. }  
  92. }  
  93. #endif // SQL_H 

上網(wǎng)查了許多無果,后來在閱讀一篇技術(shù)文章中無意發(fā)現(xiàn),原來在插入數(shù)據(jù)語句若有中文必須先QObject::tr()一番,即進行編碼,

  1. 將  
  2. Sql.h  
  3. 中  
  4. query.exec("INSERT INTO student (schnum, name, sex, nation) VALUES (2614103, '天殘腳,'男', '漢族')");  
  5. 改為如下  
  6. 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編碼, 然后在字符串之前 加

  1. tr(QObject::tr), qApp->setDefaultCodec( QTextCodec::codecForName("GBK") );  
  2. 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. 錯誤

  1. mingw32-make: *** No rule to make target `http://www.cnblogs.com/http://www.cnblogs.com/Qt/4.3.3/mkspecs/default/qmake.conf', 
  2. needed by `makefile'.  Stop.  
  3. make[2]: Entering directory `/home/lzy/tps2/rplan/super'  
  4. make[2]: *** No rule to make target `/home/lzy/qt/qt-3.3.2/mkspecs/default/qmake.conf', needed by `Makefile'.  Stop.  
  5. 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:

  1. #if !(defined Q_CC_BOR) || (__BORLANDC__ < 0x560)  
  2. extern "C" {  
  3. __declspec(dllimport) long __stdcall InterlockedCompareExchange(long *, long, long);  
  4. __declspec(dllimport) long __stdcall InterlockedIncrement(long *);  
  5. __declspec(dllimport) long __stdcall InterlockedDecrement(long *); __declspec(dllimport) long __stdcall InterlockedExchange(long *, long);  
  6. __declspec(dllimport) long __stdcall InterlockedExchangeAdd(long *, long);  
  7. }  
  8. #else  
  9. extern "C" {  
  10. __declspec(dllimport) long __stdcall InterlockedCompareExchange(long volatile*, long, long);  
  11. __declspec(dllimport) long __stdcall InterlockedIncrement(long volatile*);  
  12. __declspec(dllimport) long __stdcall InterlockedDecrement(long volatile*);  
  13. __declspec(dllimport) long __stdcall InterlockedExchange(long volatile*, long);  
  14. __declspec(dllimport) long __stdcall InterlockedExchangeAdd(long volatile*, long);  
  15. }  
  16. #endif  
  17.  
  18. you will see above code in Qt\4.4.3\src\corelib\arch\qatomic_windows.h: file. I modified like  below and it works.  
  19.  
  20. /*#if !(defined Q_CC_BOR) || (__BORLANDC__ < 0x560)  
  21. extern "C" {  
  22. __declspec(dllimport) long __stdcall InterlockedCompareExchange(long *, long, long);  
  23. __declspec(dllimport) long __stdcall InterlockedIncrement(long *);  
  24. __declspec(dllimport) long __stdcall InterlockedDecrement(long *); __declspec(dllimport) long __stdcall InterlockedExchange(long *, long);  
  25. __declspec(dllimport) long __stdcall InterlockedExchangeAdd(long *, long);  
  26. }  
  27. #else */  
  28. extern "C" {  
  29. __declspec(dllimport) long __stdcall InterlockedCompareExchange(long volatile*, long, long);  
  30. __declspec(dllimport) long __stdcall InterlockedIncrement(long volatile*);  
  31. __declspec(dllimport) long __stdcall InterlockedDecrement(long volatile*);  
  32. __declspec(dllimport) long __stdcall InterlockedExchange(long volatile*, long);  
  33. __declspec(dllimport) long __stdcall InterlockedExchangeAdd(long volatile*, long);  
  34. }  
  35. // #endif 

7、編譯錯誤,顯示 can not find -lqtmaind。

這是qtdebug庫,安裝完成后需要再自己編譯這個庫。在Qt的開始菜單中,你可以找到一個程序 Qt 4.4.0 (Build Debug Libraries),運行這個程序就能編譯QtDebug庫了。

小結(jié):QT debug大集合 詳細(xì)講解的內(nèi)容介紹完了,希望本文對你有搜幫助。

責(zé)任編輯:zhaolei 來源: CSDN博客
相關(guān)推薦

2011-06-21 10:44:32

QT QTE

2010-02-03 09:53:08

Python版本

2012-04-28 10:29:24

jQuery

2010-07-26 09:06:09

SQL Server游

2010-07-14 14:02:52

SQL Server數(shù)

2012-01-05 10:19:43

JavaScript

2014-05-15 15:29:09

Android開發(fā)資源

2013-08-13 13:38:13

Android錯誤解決

2010-10-20 17:31:40

Fedora應(yīng)用

2010-02-24 10:52:24

IBM中端服務(wù)器

2010-06-09 17:00:43

UML試題

2009-08-24 11:04:56

2010-08-04 09:57:28

路由器

2010-10-12 14:28:54

2025-02-17 00:00:03

人工智能AI工具

2009-01-07 10:30:25

2014-06-12 17:02:46

世界杯手游

2018-12-17 09:00:00

大數(shù)據(jù)數(shù)據(jù)科學(xué)工具

2012-12-26 17:14:03

2009-11-24 19:02:35

PHP常用字符串
點贊
收藏

51CTO技術(shù)棧公眾號