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!



  • Did you try to do some of the basic tutorials on Qt programming?



  • The ones I found were direct examples of slots/signals...nothing to do with OpenGL. I would appreciate any good ones that relate or has the same idea as what I want to achieve


  • Moderators

    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.



  • yeah, its me :)


  • Moderators

    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'm on it, thanks very much



  • 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 -1073741819

    Here'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_OBJECT

    public:
    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_OBJECT

    public:
    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&#40;&#41;;
    

    }
    @

    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;
    }
    @



  • I found the problem, I'm using a single buffer, not double buffer. I commented glutSwapBuffers() out and it works fine.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.