C++ QTimer与扩展QThread无关
当有人用自己的类重载线程时,这个问题似乎以这样或那样的形式得到了回答,但是如果只是尝试使用QTimer类而不扩展QThread类呢。我正在尝试将QTimer用于QT。他们的简单例子 他们的例子如下:C++ QTimer与扩展QThread无关,c++,qt,timer,qthread,qtimer,C++,Qt,Timer,Qthread,Qtimer,当有人用自己的类重载线程时,这个问题似乎以这样或那样的形式得到了回答,但是如果只是尝试使用QTimer类而不扩展QThread类呢。我正在尝试将QTimer用于QT。他们的简单例子 他们的例子如下: QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(update())); timer->start(1000); 但当我尝试这样做时,我不断地得到错误: QObject::start
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);
但当我尝试这样做时,我不断地得到错误:
QObject::startTimer:计时器只能用于以QThread启动的线程
我正在通过Jamie King的游戏引擎开发系列在YouTube上播放一组视频。我计划跟随他的视频到T台,所以现在不喜欢任何完全不同的抽象编码技术。这就是我目前所拥有的
我试着做一个小的快速辅助项目,尽可能使代码简单。我没有任何构建错误-它们只会在程序启动后发生
QTimerApp.cpp
#include <QtWidgets\qapplication.h>
#include <QtWidgets\qwidget.h>
#include "GLWin.h"
int main(int argc, char* argv[])
{
QApplication application(argc, argv);
GLWin MyGL;
MyGL.show();
return application.exec();
}
#包括
#包括
#包括“GLWin.h”
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
GLWin-MyGL;
MyGL.show();
返回application.exec();
}
格温
#ifndef My_GL_Window
#define My_GL_Window
#include <QtOpenGL\qglwidget>
#include <QtCore\qtimer.h>
class GLWin : public QGLWidget
{
Q_OBJECT // preprocessor from QT - gives what we need for slots
// Declaration* - should be in class. Declared in class, but not defined.
// Needs to be defined in .cpp (manually)
// Or allow QT to define for us. (preferred)
// use moc.exe (found in $(ProjectDir)..\Middleware\Qt\bin\moc.exe against myGLWindow.h
// C:\MyEngine\ProgramFiles\Middleware\Qt\bin\moc.exe myGLWindow.h > MyGLWindow_moc.cpp
// then include MyGLWindow_moc.cpp in project
GLuint vertexBufferID;
QTimer myTimer;
protected:
void initializeGL();
void paintGL();
private slots: //All classes that contain signals or slots must mention Q_OBJECT at the top of their declaration.
// They must also derive (directly or indirectly) from QObject.
private:
void updateWin();
};
#endif
\ifndef My\u GL\u窗口
#定义我的\u GL\u窗口
#包括
#包括
类GLWin:公共QGLWidget
{
Q_OBJECT//QT中的预处理器-提供插槽所需的内容
//声明*-应在类中。在类中声明,但未定义。
//需要在.cpp中定义(手动)
//或允许QT为我们定义。(首选)
//对myGLWindow.h使用moc.exe(位于$(ProjectDir)…\Middleware\Qt\bin\moc.exe
//C:\MyEngine\ProgramFiles\Middleware\Qt\bin\moc.exe myGLWindow.h>myGLWindow\u moc.cpp
//然后在项目中包括MyGLWindow_moc.cpp
GLuint vertexBufferID;
QTimer-myTimer;
受保护的:
void initializeGL();
void paintGL();
私有插槽://所有包含信号或插槽的类必须在其声明的顶部提到Q_对象。
//它们还必须(直接或间接)从QoObject派生。
私人:
void updateWin();
};
#恩迪夫
GLWin.cpp
#include <gl\glew.h>
#include "GLWin.h"
#include <cassert>
#include <QTCore\QTimer.h>
void GLWin::initializeGL()
{
GLenum errorCode = glewInit();
assert(errorCode == 0);
glGenBuffers(1, &vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
float verts[] =
{
+0.0f, +0.1f,
-0.1f, -0.1f,
+0.1f, -0.1f,
};
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
QTimer* timer = new QTimer(0);
connect(timer, SIGNAL(timeout()), this, SLOT(updateWin()));
timer->start();
}
void GLWin::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
void GLWin::updateWin()
{
}
#包括
#包括“GLWin.h”
#包括
#包括
void GLWin::initializeGL()
{
GLenum errorCode=glewInit();
断言(errorCode==0);
glGenBuffers(1和vertexBufferID);
glBindBuffer(GL_数组_BUFFER,vertexBufferID);
浮动顶点[]=
{
+0.0f、+0.1f、,
-0.1f,-0.1f,
+0.1f,-0.1f,
};
glBufferData(GLU数组缓冲区、大小(顶点)、顶点、GLU静态绘制);
QTimer*定时器=新的QTimer(0);
连接(计时器、信号(timeout())、此、插槽(updateWin());
定时器->启动();
}
void GLWin::paintGL()
{
glClear(GLU颜色缓冲位);
GlenableVertexAttributeArray(0);
glvertexattributepointer(0,2,GL_FLOAT,GL_FALSE,0,0);
gldrawArray(GL_三角形,0,3);
}
void GLWin::updateWin()
{
}
我所做的大部分研究都与在类扩展了QThread时重载run()函数有关。据我所知,我不应该为了一个简单的计时器循环而扩展或创建另一个类。已经扩展了QWidget,我的对象已经是QObject类型了
非常感谢您的帮助。谢谢!您的代码有很多问题:
style include。最好使用正斜杠,因为这是一种常用的永久使用方式QtFoo\
- 实际上,您不需要显式地使用
,它也更好,因为它使构建系统更可靠,项目更可移植QtFoo
- 您不需要为最终类使用
优化Q\u DECL\u FINAL
- 对于被重写的成员方法,不使用
Q\u DECL\u OVERRIDE
- 实际上,您并没有将插槽标记为插槽
- 您没有为计时器设置父级,因此它会泄漏内存
- 当Qt有自己的
和Q_断言
时,使用Q_断言
cassert
- 您使用
样式include,而qfoo.h
是常见的使用模式qfoo
- 您不使用新的信号插槽语法
哇,我真傻,我终于找到了答案 …在调试模式下,请确保链接的所有.lib和.dll都是调试器,而不是release.dll …例如,我在使用 Qt5OpenGL.dll和Qt5OpenGL.lib 而不是 Qt5OpenGLd.dll和Qt5OpenGLd.lib …我觉得自己是个傻瓜,但这再也抓不住我了。只要盲目地选择“哦,嘿…我看到QT5打开了…”就足够了!只要确保你检查了所有文件,而不仅仅是这两个文件
/facepalm!非常感谢!!我很高兴能学到这些东西,真希望有一位导师能帮我找到正确的方向。今晚下班后我会再复习一遍!我仍然会遇到同样的错误。我添加了许多最后的语句,使用了你的connect(),甚至尝试了另一种方法,但我得到了相同的错误。我的三角形仍然显示,但我知道这与connect()无关,只与updateWin()有关。一旦找到解决方案,我将在这里发布。顺便说一句,如果有任何后果,我将使用Visual Studio 2013。到目前为止,一切似乎都运行良好。
#include <glew.h>
#include <QApplication>
#include <QGLWidget>
#include <QTimer>
class GLWin Q_DECL_FINAL : public QGLWidget
{
Q_OBJECT
GLuint vertexBufferID;
QTimer myTimer;
protected:
void initializeGL() Q_DECL_OVERRIDE
{
GLenum errorCode = glewInit();
Q_ASSERT(errorCode == 0);
glGenBuffers(1, &vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
float verts[] =
{
0.0f, 0.1f,
-0.1f, -0.1f,
0.1f, -0.1f,
};
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
QTimer* timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &GLWin::updateWin);
timer->start();
}
void paintGL() Q_DECL_OVERRIDE
{
glClear(GL_COLOR_BUFFER_BIT);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
private slots:
void updateWin() {}
};
#include "main.moc"
int main(int argc, char **argv)
{
QApplication application(argc, argv);
GLWin MyGL;
MyGL.show();
return application.exec();
}
TEMPLATE = app
TARGET = main
QT += widgets opengl
SOURCES += main.cpp
packagesExist(glew) {
CONFIG += link_pkgconfig
PKGCONFIG += glew
}
qmake && make && ./main