Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. PaintGL() doesn't work
Qt 6.11 is out! See what's new in the release blog

PaintGL() doesn't work

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bear234
    wrote on last edited by
    #1

    i want to run opengl in Qt and i used QGLWidget to do this but it doesn't work.

    here is my code:
    @
    #include <QGLWidget>

    class GLWidget : public QGLWidget
    {
    Q_OBJECT
    public:
    explicit GLWidget(QWidget *parent = 0);

    virtual void initializeGL();
    virtual void resizeGL(int w, int h);
    virtual void paintGL();
    

    };
    @

    @
    #include "glwidget.h"
    #include <gl/glut.h>

    GLWidget::GLWidget(QWidget *parent) :
    QGLWidget(parent)
    {}

    void GLWidget::initializeGL()
    {
    setGeometry(300, 150, 640, 480);
    glShadeModel(GL_FLAT);
    glClearColor(0.5, 1.0, 0.2, 0);
    glClearDepth(1.0);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    }

    void GLWidget::resizeGL(int w, int h)
    {
    if(0 == h)
    h = 1;
    glViewport(0, 0, (GLint)w, (GLint)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    // gluPerspective(45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0);
    //glMatirxMode(GL_MODELVIEW);
    glLoadIdentity();
    }

    void GLWidget::paintGL()
    {
    //qglClearColor(QColor(0,0,255,128));
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glColor3f(0.0, 0.0, 1.0);
    glBegin(GL_TRIANGLES);
    glVertex3f(0.0,1.0,0.0);
    glVertex3f(-1.0,-1.0,0.0);
    glVertex3f(1.0,-1.0,0.0);
    glEnd();
    }
    @

    @
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    GLWidget *gl = new GLWidget();
    gl->updateGL();
    gl->show();
    
    return a.exec&#40;&#41;;
    

    }
    @

    it seems that the fonction initializeGL works because the color of background changes. but i cant see my triangle.
    could someone help me plz???

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bear234
      wrote on last edited by
      #2

      by the way, the version of my qt is 5.0.2

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        The background color is set by the glClear() call, not glClearColor(), so your paintGL() does work.

        Btw. "doesn't work" is not very descriptive.

        You didn't set a proper transformation matrix and you draw at depth 0.0 so your triangle falls outside of the view frustum.

        For drawing a flat 2D triangle you can try this:
        @
        void GLWidget::resizeGL(int w, int h) {
        glViewport(0, 0, w, h);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-1, 1, -1, 1, -1, 1);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        }

        void GLWidget::paintGL() {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glColor3f(0.0, 0.0, 1.0);
        glBegin(GL_TRIANGLES);
        glVertex2f(0.0,1.0);
        glVertex2f(-1.0,-1.0);
        glVertex2f(1.0,-1.0);
        glEnd();
        }
        @

        1 Reply Last reply
        0
        • B Offline
          B Offline
          bear234
          wrote on last edited by
          #4

          thx. i copied ur two functions.
          but i cant still see the triangle...

          [quote author="Chris Kawa" date="1417448239"]The background color is set by the glClear() call, not glClearColor(), so your paintGL() does work.

          Btw. "doesn't work" is not very descriptive.

          You didn't set a proper transformation matrix and you draw at depth 0.0 so your triangle falls outside of the view frustum.

          For drawing a flat 2D triangle you can try this:
          @
          void GLWidget::resizeGL(int w, int h) {
          glViewport(0, 0, w, h);
          glMatrixMode(GL_PROJECTION);
          glLoadIdentity();
          glOrtho(-1, 1, -1, 1, -1, 1);
          glMatrixMode(GL_MODELVIEW);
          glLoadIdentity();
          }

          void GLWidget::paintGL() {
          glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
          glColor3f(0.0, 0.0, 1.0);
          glBegin(GL_TRIANGLES);
          glVertex2f(0.0,1.0);
          glVertex2f(-1.0,-1.0);
          glVertex2f(1.0,-1.0);
          glEnd();
          }
          @[/quote]

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved