Need help; QPushButton to control OpenGL
-
Hi,
I'm absolutely new to programming, but I've been reading about OpenGL/Qt, and I learnt to render an OpenGL object using QGLWidget. Now I'm trying to create a GUI in which OpenGL objects are only rendered whenever QPushButton is clicked. I read about slot/signal, but I still dont understand where to place my functions, and how to implement it. Please I need help! -
You just implement your OpenGL code in the slot's body. Then connect QPushButton's clicked() signal to your new slot. I think it was you who sent me a PM about it? I've linked some handy docs there.
-
I find the best way to learn programming is to try - you'll learn as soon as you get it to work yourself. This way, it's your thinking that analyses the problem, finds a solution, and then finds a way to implement that solution in a given programming language.
-
I worked on a basic GLUT model in Qt, and it shows a white screen and crashes, I need help.
The program has unexpectedly finished.
C:\QtGL\myGL-build-desktop-Qt_4_7_4_for_Desktop-MinGW_4_4__Qt_SDK__Debug\debug\myGL.exe exited with code -1073741819Here're my codes:
GLWidget.cpp
@#include "glwidget.h"
#include<GL/glut.h>GLWidget::GLWidget(QWidget *parent) :
QGLWidget(parent)
{
}
void GLWidget::initializeGL()
{
glClearColor(0.0, 0.0, 0.0, 0); // Background Color}
void GLWidget::resizeGL(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float)width / (float)height, 0.1, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(
0.0, 0.0, 200.0, // eye location
0.0, 0.0, 0.0, // center location
0.0, 1.0, 0.0); // up vector
}void GLWidget::drawGL(){
glColor3f(1.0f, 0.0f, 0.0f); //set cube color
glutWireTeapot(30); //use the premade glut function to draw a wire cube of size 30
glutSwapBuffers();
}void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
drawGL();}
@GLWidget.h
@#ifndef GLWIDGET_H
#define GLWIDGET_H#include <QGLWidget>
class GLWidget : public QGLWidget
{
Q_OBJECTpublic:
explicit GLWidget(QWidget *parent = 0);protected:
void initializeGL();
void resizeGL(int width, int height);
void paintGL();private:
void drawGL();signals:
public slots:
};
#endif // GLWIDGET_H
@Mainwidget.h
@#ifndef MAINWIDGET_H
#define MAINWIDGET_H#include <QWidget>
namespace Ui {
class MainWidget;
}class MainWidget : public QWidget
{
Q_OBJECTpublic:
explicit MainWidget(QWidget *parent = 0);
~MainWidget();private:
Ui::MainWidget *ui;
};#endif // MAINWIDGET_H
@Main.cpp
@#include <QtGui/QApplication>
#include <GL/glut.h>
#include "mainwidget.h"int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWidget w;
w.show();return a.exec();
}
@Mainwidget.cpp
@#include "mainwidget.h"
#include "ui_mainwidget.h"MainWidget::MainWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWidget)
{
ui->setupUi(this);
}MainWidget::~MainWidget()
{
delete ui;
}
@