我想通过按键事件来控制paintGL方法。目的是通过推动回归来显示另一个点。 换句话说:我画了一个漂亮的背景场景,现在我想推回(在一个lineEdit中)并且在已经显示的背景前面出现一个红点。
// MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
glWidget = new GLWidget;
connect(ui->lineEdit, SIGNAL(returnPressed()), glWidget, SLOT (set_draw()));
}
// glwidget.h
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QGLWidget>
#include <QMessageBox>
#include "mainwindow.h"
#include "cstdio"
class MainWindow;
class GLWidget : public QGLWidget
{
Q_OBJECT
MainWindow *myMainWindow;
public:
GLWidget(QWidget *parent = 0);
//~GLWidget;
int draw;
void initializeGL();
void paintGL();
void resizeGL(int w, int h);
public slots:
void set_draw();
};
#endif // GLWIDGET_H
// glwidget.cpp
GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent)
{
draw = 0;
}
//-------------
void GLWidget::set_draw() //this SLOT is activated by pushing return
{
draw = 1;
updateGL(); //updating paintGL...
}
//-------------
void GLWidget::paintGL()
{
swapBuffers();
glClear(GL_COLOR_BUFFER_BIT);
/* drawing a lot of stuff*/
if( draw == 1 )
{
/*the following messagebox is shown at the screen*/
QMessageBox* Box = new QMessageBox();
Box->setText("Bert");
Box->show();
/*this big red point is NOT shown at the screen*/
glPointSize(30);
glBegin(GL_POINTS);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(45,45,0);
glEnd();
}
}
有人可以解释,为什么这不起作用?红点不会出现...... int draw
的值是否受paintGL方法的影响?
答案 0 :(得分:1)
在OpenGL中,您总是重绘整个场景。将附加点存储在某个数组中。绘制时,迭代遍历该数组并根据数组的内容绘制点。