Qtcreator使用中遇到的错误汇总

1. 安装完成后,只能用vs新建qt项目,下面代码才能运行,新建控制台没用
//#include "qt11.h"
#include // Qt5.0以上,需要加上QtWidgets
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel *label = new QLabel("Hello Qt!");
label->show();
//qt11 w;
//w.show();
return a.exec();
}
cmd命名中用qmake命令,qmake路径:D:\Qt\Qt5.2.0VS2010\5.2.0\msvc2010_opengl\bin,导入到path路径中即可使用

2. 用Qtcreator添加Qt设计师界面类时,用该类声明一个对象,编译有可能出现:Qtcreator中编译出现无法解析的外部符号,未找到文件:mainwindow.obj类似的错误,解决方法:删除项目目录下的release和dubug文件夹,然后清理项目,重新构建,执行qmake,参考:http://tieba.baidu.com/p/2567360449
注:执行qmake即可,再重新运行即可。

3. http://www.netfoucs.com/jcy8126/article/details/8278916
编译时,gluPerspective处如下错误:
  C:\Users\Administrator\Desktop\tornadomeet\opengl\opengl_nehe_01\glwidget.cpp:46: error: C3861: “gluPerspective”: 找不到标识符。

很多人在用QT写OpenGL程序的时候需要使用glu开头的函数,但是却发现怎么也没法使用,例如:gluPerspective函数,用来建立透视投影矩阵的。
其实不仅仅是QT,包括VC编译器,也都会出现上面的问题。
错误:
错误:C3861: 'gluPerspective': identifier not found
解决办法:
在pro文件中加入:LIBS += glut.lib glut32.lib
但是前提是你将这两个lib文件放入了qt的bin目录下,我的电脑安装的对应目录是:
C:\QtSDK\Simulator\Qt\mingw\lib
然后在你使用到相应函数中添加头文件:
#include
这样基本上就可以使用glu开头的函数了。

5. Qtcreator中继承QGLWidget,然后重写下列函数:
 void initializeGL();
    void paintGL();
    void resizeGL( int width, int height );
会调用一下函数:
  glShadeModel(GL_SMOOTH);
    glClearColor( 0.0, 0.0, 0.0, 0.0 );
    glClearDepth( 1.0 );
    glEnable( GL_DEPTH_TEST );
    glDepthFunc( GL_LEQUAL );
 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glLoadIdentity();

完整代码如下:
// myQtOpenglWindow.h
#ifndef MYQTOPENGLWINDOW_H
#define MYQTOPENGLWINDOW_H
#include
#include
#include
class myQtOpenglWindow : public QGLWidget
{
    Q_OBJECT
public:
    explicit myQtOpenglWindow( QWidget* parent = 0, const char* name = 0, bool fs = false );\
    ~myQtOpenglWindow();


protected:
    void initializeGL();
    void paintGL();
    void resizeGL( int width, int height );
    void keyPressEvent(QKeyEvent *);

protected:
    bool fullScreen;

signals:


public slots:

};

#endif // MYQTOPENGLWINDOW_H

// myQtOpenglWindow.cpp
#include "myqtopenglwindow.h"
#include
myQtOpenglWindow::myQtOpenglWindow( QWidget* parent, const char* name, bool fs )
    :QGLWidget( parent )
{
     //保存窗口是否为全屏的状态。
    fullScreen = fs;


    //设置窗口的位置,即左上角为(0,0)点,大小为640*480。
    setGeometry( 0, 0, 640, 480 );

    // 设置窗口的标题为“My's OpenGL Framework”。
    //setCaption( "My's OpenGL Framework" );

    // 如果fullscreen为真,那么就全屏显示这个窗口。
    if ( fullScreen )
    {
        showFullScreen();
    }

}

myQtOpenglWindow::~myQtOpenglWindow()
{
}

void myQtOpenglWindow::initializeGL()
{
    glShadeModel(GL_SMOOTH);
    glClearColor( 0.0, 0.0, 0.0, 0.0 );
    glClearDepth( 1.0 );
    glEnable( GL_DEPTH_TEST );
    glDepthFunc( GL_LEQUAL );
    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
    return;
}

void myQtOpenglWindow::paintGL()
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glLoadIdentity();
    return;
}

void myQtOpenglWindow::resizeGL(int width, int height)
{
    //防止height为0
    if( height == 0 )
    {
        height =1;
    }
    //重置当前的视口(Viewport)
    glViewport( 0, 0, (GLint)width, (GLint)height );


    //选择投影矩阵。
    glMatrixMode( GL_PROJECTION );

    //重置投影矩阵
    glLoadIdentity();

    //建立透视投影矩阵
    gluPerspective( 45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0 );
    //gluPerspective(45.0,(GLfloat)width() / (GLfloat)height(),0.1,100.0);
    // 选择模型观察矩阵
    glMatrixMode( GL_MODELVIEW );
    // 重置模型观察矩阵。
    glLoadIdentity();

    return;
}

void myQtOpenglWindow::keyPressEvent(QKeyEvent * e)
{
    switch ( e->key() )
    {
        //如果按下了F2键,那么屏幕是否全屏的状态就切换一次。然后再根据需要,显示所要的全屏窗口或者普通窗口。
        case Qt::Key_F2:
            fullScreen = !fullScreen;


            if ( fullScreen )
            {
                showFullScreen();
            }
            else
            {
                showNormal();
                setGeometry( 0, 0, 640, 480 );
            }


            updateGL();
            break;


        //如果按下了Escape键,程序退出。
        case Qt::Key_Escape:
            close();
    }

    return;
}

使用上面代码:myQtOpenglWindow *p = new myQtOpenglWindow; p->show();即可在Qt中渲染一个opengl窗口,进行opengl编程,但可能会遇到下面错误:

运行出现下列错误:

myqtopenglwindow.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: virtual __thiscall QGLWidget::~QGLWidget(void)" (__imp_??1QGLWidget@@UAE@XZ),该符号在函数 "public: virtual __thiscall myQtOpenglWindow::~myQtOpenglWindow(void)" (??1myQtOpenglWindow@@UAE@XZ) 中被引用
myqtopenglwindow.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: __thiscall QGLWidget::QGLWidget(class QWidget *,class QGLWidget const *,class QFlags)" (__imp_??0QGLWidget@@QAE@PAVQWidget@@PBV0@V?$QFlags@W4WindowType@Qt@@@@@Z),该符号在函数 "public: __thiscall myQtOpenglWindow::myQtOpenglWindow(class QWidget *,char const *,bool)" (??0myQtOpenglWindow@@QAE@PAVQWidget@@PBD_N@Z) 中被引用
myqtopenglwindow.obj : error LNK2001: 无法解析的外部符号 "protected: virtual bool __thiscall QGLWidget::event(class QEvent *)" (?event@QGLWidget@@MAE_NPAVQEvent@@@Z)
myqtopenglwindow.obj : error LNK2001: 无法解析的外部符号 "protected: virtual void __thiscall QGLWidget::paintEvent(class QPaintEvent *)" (?paintEvent@QGLWidget@@MAEXPAVQPaintEvent@@@Z)
myqtopenglwindow.obj : error LNK2001: 无法解析的外部符号 "protected: virtual void __thiscall QGLWidget::resizeEvent(class QResizeEvent *)" (?resizeEvent@QGLWidget@@MAEXPAVQResizeEvent@@@Z)
myqtopenglwindow.obj : error LNK2001: 无法解析的外部符号 "public: virtual void __thiscall QGLWidget::updateGL(void)" (?updateGL@QGLWidget@@UAEXXZ)
myqtopenglwindow.obj : error LNK2001: 无法解析的外部符号 "public: virtual void __thiscall QGLWidget::updateOverlayGL(void)" (?updateOverlayGL@QGLWidget@@UAEXXZ)
myqtopenglwindow.obj : error LNK2001: 无法解析的外部符号 "protected: virtual void __thiscall QGLWidget::initializeOverlayGL(void)" (?initializeOverlayGL@QGLWidget@@MAEXXZ)
myqtopenglwindow.obj : error LNK2001: 无法解析的外部符号 "protected: virtual void __thiscall QGLWidget::resizeOverlayGL(int,int)" (?resizeOverlayGL@QGLWidget@@MAEXHH@Z)
myqtopenglwindow.obj : error LNK2001: 无法解析的外部符号 "protected: virtual void __thiscall QGLWidget::paintOverlayGL(void)" (?paintOverlayGL@QGLWidget@@MAEXXZ)
myqtopenglwindow.obj : error LNK2001: 无法解析的外部符号 "protected: virtual void __thiscall QGLWidget::glInit(void)" (?glInit@QGLWidget@@MAEXXZ)
myqtopenglwindow.obj : error LNK2001: 无法解析的外部符号 "protected: virtual void __thiscall QGLWidget::glDraw(void)" (?glDraw@QGLWidget@@MAEXXZ)
myqtopenglwindow.obj : error LNK2001: 无法解析的外部符号 "public: virtual class QPaintEngine * __thiscall QGLWidget::paintEngine(void)const " (?paintEngine@QGLWidget@@UBEPAVQPaintEngine@@XZ)
moc_myqtopenglwindow.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: virtual void * __thiscall QGLWidget::qt_metacast(char const *)" (__imp_?qt_metacast@QGLWidget@@UAEPAXPBD@Z),该符号在函数 "public: virtual void * __thiscall myQtOpenglWindow::qt_metacast(char const *)" (?qt_metacast@myQtOpenglWindow@@UAEPAXPBD@Z) 中被引用
moc_myqtopenglwindow.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: virtual int __thiscall QGLWidget::qt_metacall(enum QMetaObject::Call,int,void * *)" (__imp_?qt_metacall@QGLWidget@@UAEHW4Call@QMetaObject@@HPAPAX@Z),该符号在函数 "public: virtual int __thiscall myQtOpenglWindow::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@myQtOpenglWindow@@UAEHW4Call@QMetaObject@@HPAPAX@Z) 中被引用
moc_myqtopenglwindow.obj : error LNK2001: 无法解析的外部符号 "__declspec(dllimport) public: static struct QMetaObject const QGLWidget::staticMetaObject" (__imp_?staticMetaObject@QGLWidget@@2UQMetaObject@@B)
debug\test.exe : fatal error LNK1120: 16 个无法解析的外部命令

解决方法:
添加外部文件:QT/QT5.3.1/5.3/msvc2010_opengl/lib/Qt5OpenGL.lib,再次试一试,不行,删除debug和release文件夹,然后清理项目,然后重新构建项目,再次运行,或清理项目后,执行qmake,再次运行试试。
出现上面错误,看看是否函数只有声明没有实现。

6. Qtcreator中不能有/* */注释,否则会出现:”在注释中遇到意外的文件结束“的错误,把/* */改为//即可

7. Qtcreator中有时改了代码还是出现上一次出现的情况,执行qmake一下,重新构建试一下。
        
8. Qtcreator中#include <头文件>编译出现:打开包括文件:"gl/glaux.h" no such file or direcotry
解决方法:右击项目-》添加库-》外部库,把库文件(.lib文件)和包含路径添加进来,注意,库文件命名要为小写,且路径不要包含中文等其他不好的字符,然后重新构建项目,执行qmake,清理项目,多执行几次,开始时可能还会出现上面的错误。
执行qmake通过后,运行时可能会出现:
glaux.lib(tk.obj) : error LNK2019: unresolved external symbol __imp__RegCloseKey@4 referenced in function _GetRegistrySysColors@8  
1>glaux.lib(tk.obj) : error LNK2019: unresolved external symbol __imp__RegQueryValueExA@24 referenced in function _GetRegistrySysColors@8  
1>glaux.lib(tk.obj) : error LNK2019: unresolved external symbol __imp__RegOpenKeyExA@20 referenced in function _GetRegistrySysColors@8  

解决方法如下:
QT 加载
在.pro中加载
LIBS += -lGlAux -ladvapi32

VS 加载
#param comment (lib,"glaux.lib")
#param comment (lib,"advapi32.lib")

你可能感兴趣的:(Qt)