问题描述
我是QT的新手,并在Ubuntu平台中使用其openGL
我已经创建了一个模板,如:
我的目标:
单击“计算”按钮时,应该发生两件事:
- 文本将被打印在文本框中
- 将从QGLWidget继承的expOutput(实验对象)上画一条线。
我正在创建两个信号插槽。该程序的代码如下:
nearestneighbour.h
#ifndef NEARESTNEIGHBOUR_H
#define NEARESTNEIGHBOUR_H
#include <QWidget>
#include <QGLWidget>
#include "experimental.h"
namespace Ui {
class NearestNeighbour;
}
class NearestNeighbour : public QWidget
{
Q_OBJECT
public:
explicit NearestNeighbour(QWidget *parent = 0);
~NearestNeighbour();
signals:
void clicked();
private slots:
void on_Compute_clicked();
private:
Ui::NearestNeighbour *ui;
};
#endif // NEARESTNEIGHBOUR_H
nearestneighbour.cpp
#include "nearestneighbour.h"
#include "ui_nearestneighbour.h"
NearestNeighbour::NearestNeighbour(QWidget *parent) :
QWidget(parent),ui(new Ui::NearestNeighbour)
{
//expt = new Experimental(this);
ui->setupUi(this);
QObject::connect(ui->Compute,&QPushButton::clicked,ui->expOutput,&Experimental::draw);
QObject::connect(ui->Compute,this,&NearestNeighbour::on_Compute_clicked);
}
NearestNeighbour::~NearestNeighbour()
{
delete ui;
}
void NearestNeighbour::on_Compute_clicked()
{
ui->textEdit->setText("Hello World !!");
}
实验。h
#ifndef EXPERIMENTAL_H
#define EXPERIMENTAL_H
#include <QWidget>
#include <QGLWidget>
#include <iostream>
#include "nearestneighbour.h"
// #include "ui_nearestneighbour.h"
class Experimental : public QGLWidget
{
Q_OBJECT
public:
explicit Experimental(QWidget *parent=NULL);
public slots:
void draw(void);
protected:
void initializeGL();
void paintGL();
};
#endif // EXPERIMENTAL_H
experimental.cpp
#include "experimental.h"
Experimental::Experimental(QWidget *parent): QGLWidget(QGLFormat(QGL::SampleBuffers),parent){}
void Experimental::initializeGL(){
std::cout<<"In initializegl !! ";
qglClearColor(Qt::white);
glViewport(0,600,300);
glMatrixMode(GL_MODELVIEW);
glOrtho(0,300,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void Experimental::paintGL(){
std::cout<<"In paintgl !! ";
}
void Experimental::draw(void){
std::cout<<"In draw !!";
glLoadIdentity();
qglColor(Qt::blue);
glBegin(GL_LINE);
glVertex2i(100,100);
glVertex2i(200,200);
glEnd();
updateGL();
}
问题:
由于有了一些很好的建议,我能够消除错误,并且系统可以正常编译。
我单击“计算”按钮,该行应在openGl小部件上绘制。但是,没有任何输出,但是在退出GUI模式后,可以注意到有一些相关的消息,表明功能已被访问。
有人可以帮助我解决问题吗?
解决方法
如果您需要在paintGL()
处理程序之外进行绘制,则必须通过调用makeCurrent()
来使上下文成为当前内容,而Qt实际上并不会善意地尝试在paint事件之外绘制,加上输出顺序有问题(“ painter”问题)
在Qt5 +上,您不应该在新程序中使用旧的QGLWidget,它依赖固定的管道,并且与旧代码兼容,因此QOpenGLWidget是您的朋友。助手中有以下示例:
class MyGLWidget : public QOpenGLWidget
{
public:
MyGLWidget(QWidget *parent) : QOpenGLWidget(parent) { }
protected:
void initializeGL()
{
// Set up the rendering context,load shaders and other resources,etc.:
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
f->glClearColor(1.0f,1.0f,1.0f);
...
}
void resizeGL(int w,int h)
{
// Update projection matrix and other size related settings:
m_projection.setToIdentity();
m_projection.perspective(45.0f,w / float(h),0.01f,100.0f);
...
}
void paintGL()
{
// Draw the scene:
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
f->glClear(GL_COLOR_BUFFER_BIT);
...
}
};
您正在使用的QGlWidget具有以下虚拟方法:
virtual void glDraw ()
virtual void glInit ()
virtual void initializeGL ()
virtual void initializeOverlayGL ()
virtual void paintGL ()
virtual void paintOverlayGL ()
virtual void resizeGL ( int width,int height )
virtual void resizeOverlayGL ( int width,int height )
您几乎从不需要覆盖前两个,并且通常必须覆盖其他两个。 glDraw
在致电glPaintGL
后致电makeCurrent