【问题标题】:No output for OpenGLOpenGL 没有输出
【发布时间】:2021-02-08 10:22:15
【问题描述】:

我是 QT 的新手,在 Ubuntu 平台上使用它的 openGL

我已经创建了一个模板:

我的目标:

点击“计算”按钮后,应该会发生两件事:

  1. 文本框中将打印一个文本
  2. 会在expOutput上画一条线,expOutput是Experimental的一个对象,继承自QGLWidget。

我正在创建两个信号槽。程序代码如下:

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, &QPushButton::clicked, this,&NearestNeighbour::on_Compute_clicked);
}

NearestNeighbour::~NearestNeighbour()
{
    delete ui;
}


void NearestNeighbour::on_Compute_clicked()
{
    ui->textEdit->setText("Hello World !!");
}

experimental.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,0,600,300);
    glMatrixMode(GL_MODELVIEW);
    glOrtho(0,300,0,600,0,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模式后,可以注意到有一些相关的消息,表明函数已经被访问过。

谁能帮我解决这个问题?

【问题讨论】:

  • 为什么experimental.h首先包含ui_nearestneighbour.h?
  • 谢谢!!删除 ui_nearestneighbour.h 删除了错误。但是, QObject::connect(ui->Compute, SIGNAL(clicked()), ui->expOutput,SLOT(draw()));在最近的neighbour.cpp 中仍然无法正常工作!
  • 不要使用 qt4 风格的信号槽连接,如果这不能显示您需要显示的更多内容。
  • @Botje:我已经更新到 qt5 信号槽方法:QObject::connect(ui->Compute, &QPushButton::clicked, ui->expOutput,&Experimental::draw); QObject::connect(ui->Compute, &QPushButton::clicked, this,&NearestNeighbour::on_Compute_clicked);但是,我的 opengl 小部件中没有显示
  • 代码已更新,显示未看到任何输出

标签: c++ opengl qt5 qtgui


【解决方案1】:

如果您需要paintGL() 处理程序之外进行绘制,则必须通过调用makeCurrent() 使上下文成为当前的,并且Qt 真的不接受尝试在绘制之外进行绘制事件加上你会遇到输出顺序问题(“画家”问题)

在 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, 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 在调用makeCurrent 后调用glPaintGL

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    • 2018-08-20
    相关资源
    最近更新 更多