Qt只使用QT中的QGLWidget绘制第一帧

时间:2013-07-25 17:58:35

标签: qt opengl qglwidget

我使用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会自动调用。我真的迷失在这种情况面前。有人能帮帮我吗?非常感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

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

您会看到,paint()paintGL()仅在窗口需要绘制时才会被调用。即:创建应用程序时,调整窗口大小或移动窗口时。只有这样才会触发paintGL()。那就是它!

如果您希望多次调用它,则需要添加计时器来执行update(),这是一种强制重绘窗口的方法,转动触发器paintGL()请勿直接致电paintGL()

QTimer可以帮助您实现所需目标!