QT Debug大集合 详细讲解

移动开发
本文介绍的是QT 错误大集合 详细讲解,本文把在QT编程过程中遇到的问题集合了一下,大家一块来分析解决,我们先来看内容。

QT  Debug大集合 详细讲解是本文要介绍的内容,相信友们应该在编程过程中遇到各种各样的Debug,先来看内容。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'在此作用域中尚未声明。

昨天为什么没有出现这种错误呢?因为昨天的代码是从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 <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.     //创建数据库连接  
  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 <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 

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

求助:提示无法打开包含文件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大集合 详细讲解的内容介绍完了,希望本文对你有搜帮助。

责任编辑:zhaolei 来源: CSDN博客
相关推荐

2011-06-21 10:44:32

QT QTE

2012-04-28 10:29:24

jQuery

2010-02-03 09:53:08

Python版本

2012-01-05 10:19:43

JavaScript

2010-07-14 14:02:52

SQL Server数

2010-07-26 09:06:09

SQL Server游

2014-05-15 15:29:09

Android开发资源

2010-06-09 17:00:43

UML试题

2010-02-24 10:52:24

IBM中端服务器

2013-08-13 13:38:13

Android错误解决

2010-10-20 17:31:40

Fedora应用

2014-06-12 17:02:46

世界杯手游

2009-01-07 10:30:25

2018-12-17 09:00:00

大数据数据科学工具

2009-08-24 11:04:56

2010-10-12 14:28:54

2010-08-04 09:57:28

路由器

2011-07-21 14:32:06

iPhone App 游戏

2011-07-26 09:48:47

Shell快捷键grepinit

2009-12-22 11:30:02

宽带路由器性能
点赞
收藏

51CTO技术栈公众号