I am a newbie in QT and working with its openGL in the Ubuntu Platform
I have created a template as in :
My objective :
On clicking of the "Compute" button, two things are supposed to happen :
- A text will be printed in the text box
- A line will be drawn on expOutput, which is an object of Experimental, inherited from QGLWidget.
I am creating two signal slots. The codes of the program are given below :
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();
}
Problem :
Thanks to some great suggestions, I was able to remove the errors and the system compiled fine.
The output of the first screen is coming as
I am clicking on the "Compute" button, and the line should be drawn on the openGl widget. However, there are no outputs but after exiting the GUI mode, one can note that there are some relevant messages, which indicate that the functions have been visited.
Can somebody help me in solving the problem?