signals:
public slots:};#endif // MYQTOPENGLWINDOW_H// myQtOpenglWindow.cpp#include "myqtopenglwindow.h"#include <gl/GLU.h>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<enum Qt::WindowType>)" (__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 <a href="mailto:_GetRegistrySysColors@8">_GetRegistrySysColors@8 解决方法如下:QT 加载在.pro中加载LIBS += -lGlAux -ladvapi32VS 加载#param comment (lib,"glaux.lib")#param comment (lib,"advapi32.lib")新闻热点
疑难解答