QT Debug大集合 详细讲解
创始人
2024-08-02 10:51:18
0

QT  Debug大集合 详细讲解是本文要介绍的内容,相信友们应该在编程过程中遇到各种各样的Debug,先来看内容。QT Debug集锦~ 这篇是在10年测试QT 过程中遇到的问题:

1、中文显示问题:

  1. #include  
  2. #include  
  3. #include  
  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'在此作用域中尚未声明。

昨天为什么没有出现这种错误呢?因为昨天的代码是从qt creator生成的MainWindow中挑出来的,tr被声明为QObject的一个static方法,因此在MainWindow中使用tr不会有问题。

把上面的QLabel *label=new QLabel(tr("这里是中文"));

改为

QLabel *label=new QLabel(QObject::tr("这里是中文"));

2、中文问题:

使用sqlite数据库显示乱码的问题

本人近日在使用QT进行sqlite数据库编程时,出现中文数据显示乱码情况,附源码如下:
 

  1. //main.cpp  
  2. #include  
  3. #include  
  4. #include  
  5. #include  
  6. #include  
  7. #include  
  8. #include  
  9. #include  
  10. #include  
  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.     //创建数据库连接  
  19.     if (!createConnection())  
  20.         return 1;  
  21.     //创建学生信息表  
  22.     createTables();  
  23.     //初始添加数据  
  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("学号"));  
  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  
  58. #include  
  59. #include  
  60. #include  
  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  
  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 

上网查了许多无果,后来在阅读一篇技术文章中无意发现,原来在插入数据语句若有中文必须先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, '天残脚,'男', '汉族')")); 

结果在显示中都能得正确显示。

注意,如果语句 QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));中的编码改为utf-8则会显示乱码。

3、中文问题:

如果使程序只支持一种编码,也可以直接把整个应用程序的编码设置为GBK编码, 然后在字符串之前 加

  1. tr(QObject::tr), qApp->setDefaultCodec( QTextCodec::codecForName("GBK") );  
  2. QLabel *label = new QLabel( tr("中文标签") ); 

4、找不到

求助:提示无法打开包含文件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库了。

小结:QT debug大集合 详细讲解的内容介绍完了,希望本文对你有搜帮助。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...