1、汉字显示乱码问题的简单解决方法
QTextCodec::codecForName("GB2312")->toUnicode("显示汉字");
在这里得包含头文件 #include<QTextCodec>
2、当在Windows下运行时,程序默认不会有控制台输出。这就意味着,默认情况下,windows程序不会将输出信息写到命令行界面。为了能看到qDebug()的输出信息,你需要在工程文件中增加一行内容:
CONFIG += console
(注:网上有写 win32:CONFIG += console,暂不知有何不同)
摘自:Johan Thelin《Foundations of Qt Development》
原文:When running Windows, applications do not get a console output by default. This means that Windows applications cannot, by default, write output to the command-line users. To see any output from qDebug(), you must add a line reading CNFIGO += console to the project file. If you built the executable and then saw this tip, try fixing the project file; then run make clean followed by make. This process ensures that the project is completely rebuilt and that the new configuration is taken into account.
3、undefined reference to `vtable for ClassName错误
今天练习一个关于clipboard的小程序时,由于只需要一个类并且只有几个不大的函数,所以直接和main函数写在了同一个cpp文件中。刚开始执行程序时,命令提示符里报警告:
Object::connect: No such slot QWidget::copy() in main.cpp:39
Object::connect: No such slot QWidget::paste() in main.cpp:40
才突然想起一句话:对于所有定义了信号和槽的类,在类的定义开始处都要加上Q_OBJECT宏。可是加上这个宏之后又会报另外的错误: undefined reference to `vtable for CopyToClipboard'。上网一查才知道:qmake 不会处理.cpp文档里的Q_OBJECT,所以,假如在.cpp文档中有它的话,就会出现该错误。关于该错误,网上解释还有可能是:
预编译器打开宏Q_OBJECT,声明若干个由moc处理(implement)的成员函数。假如得到类似于“undefined reference to vtable for LcdNumber”的编译错误(if you get compiler errors along the lines of "undefined reference to vtable for LcdNumber"),您可能是忘记了执行moc,或忘记了将moc输出加入到link命令里。
4、 error: QSqlDatabase: No such file or directory错误
当包含了<QSqlDatabase>头文件,程序报错:error: QSqlDatabase: No such file or directory。
解决方法:在.pro工程文件中添加“QT += sql”。
5、error: invalid use of incomplete type 'struct QSqlError'错误
当程序中使用了QSqlDatabase::lastError()函数,而没有包含<QSqlError>头文件,便会报出error: invalid use of incomplete type 'struct QSqlError'错误,加上<QSqlError>头文件即可。
分析原因:QSqlDatabase类的声明文件中引用了前向声明class QSqlError,同时其cpp文件中也没有包含<QSqlError>头文件,仅仅是简单的调用了d->driver->lastError()(其中d为QSqlDatabasePrivate对象指针,声明在qsqldatabase.cpp文件中。dirver为QSqlDriver对象指针,qsqldatabase.cpp文件中包含"qsqldriver.h"头文件,qsqldriver.cpp文件中是包含"qsqlerror.h"文件的)。详细参看qt源码,下面写个简单的模拟程序(包括A.h、A.cpp、C.h、D.h、main.cpp五个文件,main函数在main.cpp文件中):
- // A.h
- #ifndef __A_H__
- #define __A_H__
- class D;
- class A
- {
- public:
- A();
- ~A();
- D GetDObj();
- private:
- friend class B;
- B *m_b;
- };
- #endif // __A_H__
- // A.cpp
- #include "A.h"
- #include "C.h"
- class B
- {
- public:
- B(){m_c = new C;}
- ~B(){delete m_c;}
- C *m_c;
- };
- A::A()
- {
- m_b = new B;
- }
- A::~A()
- {
- delete m_b;
- }
- D A::GetDObj()
- {
- return m_b->m_c->GetDObj();
- }
- // C.h
- #ifndef __C_H__
- #define __C_H__
- #include "D.h"
- class C
- {
- public:
- D GetDObj(){return D();}
- };
- #endif //__C_H__
- // D.h
- #ifndef __D_H__
- #define __D_H__
- #include <stdio.h>
- class D
- {
- public:
- D(){printf("D object is created!/n");}
- };
- #endif // __D_H__
- // main.cpp
- #include "A.h"
- #include "D.h"
- int main(int argc, char *argv[])
- {
- A a;
- a.GetDObj();
- return 0;
- }
从main.cpp函数可以看出,这里没有直接创建D类对象,但是 main.cpp文件中必须包含D.h头文件,否则在vc6中会报出error C2027: use of undefined type 'D'错误,上面所说的error: invalid use of incomplete type 'struct QSqlError'错误就是这个原因。