Qt只是使用来自Qt的QGLWiget绘制第一帧



我使用QGLWidget编写了一个小型QT程序。目标是显示一个永久旋转的简单三角形。问题是没有动画,因为只渲染了第一帧。

这是我的主要C++源代码:

#include "qtapplication.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QtApplication w;
    w.show();
    return a.exec();
}

我的qtapplication.h文件:

include <QtWidgets/QMainWindow>
#include "ui_qtapplication.h"
#include "HSGLWidget.hpp"
class QtApplication : public QMainWindow
{
    Q_OBJECT
public:
    QtApplication(QWidget *parent = 0);
    ~QtApplication();
private:
    Ui::QtApplicationClass ui;
    HSGLWidget *_glwidget;
};

我的qtapplication.cpp文件:

#include "qtapplication.h"
QtApplication::QtApplication(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    this->_glwidget = NULL;
    if (this->_glwidget == NULL)
        this->_glwidget = new HSGLWidget();
    this->_glwidget->setVisible(true);
    setCentralWidget(this->_glwidget);
}
QtApplication::~QtApplication()
{
}

我的HSGLWidjet.h文件:

#include <QGLWidget>
class HSGLWidget : public QGLWidget {
    Q_OBJECT
    public:
        explicit HSGLWidget(QWidget *parent = 0);
        virtual ~HSGLWidget();
    protected:
        virtual void initializeGL();
        virtual void resizeGL(int width, int height);
        virtual void paintGL();
        virtual void keyPressEvent(QKeyEvent *keyEvent);
    private:
        void _onSetup();
    private:
        QTimer  *_timer;
};

我的HSGLWidjet.cpp文件:

#include <iostream>
#include "HSGLWidget.hpp"
float toto_angle = 0.0f;
HSGLWidget::HSGLWidget(QWidget *parent) 
    :  QGLWidget(parent)
{
    this->_onSetup();
}
HSGLWidget::~HSGLWidget(void)
{
}
void HSGLWidget::initializeGL()
{   
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.20f, 0.0f, 0.0f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void HSGLWidget::resizeGL(int width, int height)
{
    if (height == 0)
        height = 1;
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
void HSGLWidget::paintGL(void)
{
    std::cout << "PAINTGL" << std::endl;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(-1.5f, 0.0f, -6.0f);
    glEnable(GL_MODELVIEW);
    glPushMatrix();
    glTranslatef(0.0f, 0.0f, 0.0f);
    glRotatef(toto_angle, 1.0f, 1.0f, 1.0f);
    glScalef(1.0f, 1.0f, 1.0f);
    glBegin(GL_TRIANGLES);
    glColor3ub(255, 0, 0);
    glVertex3f(0.0f, 0.75f, 0.0f);
    glColor3ub(0, 255, 0);
    glVertex3f(-0.75f, 0.0f, 0.0f);
    glColor3ub(0, 0, 255);
    glVertex3f(0.75f, 0.0f, 0.0f);
    glEnd();
    toto_angle+=1.0f;
    glPopMatrix();
}
void HSGLWidget::keyPressEvent(QKeyEvent *keyEvent)
{
}
void HSGLWidget::_onSetup()
{
}

字符串"PAINTGL"仅用于第一帧。通常,paintGL是自动调用的。面对这种情况我真的很失落。有人能帮我吗?事先非常感谢你的帮助。

根本没有问题,这是标准行为。

您可以看到,只有在需要绘制窗口时才会调用paint()paintGL()。也就是说:当创建应用程序时,当调整窗口大小或四处移动时。只有这样paintGL()才会被触发。就是这样!

如果希望多次调用它,则需要添加计时器来执行update(),这是一种强制重新绘制窗口的方法,进而触发paintGL()不要直接调用paintGL()

QTimer可以帮助您实现您想要的目标!

相关内容

  • 没有找到相关文章

最新更新