Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Cannot draw in QGLWidget



  • Please be tolerant of typos as I cannot copy paste. Yes, this is Qt 4 and I will be pushing to go to 5. In my Qt app is a widget that will display some user controls and an Open GL display. Within that QT app is this:

    plotted_points = new C_GL_Strip_Chart( central_widget, argc, argv )
    QGridLayout  sc_grid_layout = new QGridLayout( central_widget );
    …
    sc_grid_layout->AddWiget( plotted_points,
       3,  // begin row
       0, // begin column
       3, // span this many rows
       5, // span this many columns
       QT::AlignLeft );
    ...
    

    All the other stuff in that grid layout is where expected. From the constructor: C_GL_Strip_Chart

    C_GL_Strip_Chart:: C_GL_Strip_Chart( QWidget * parent, int argc, char ** argv )
         : QGLWidget( parent );
    {
    setMinimumWidth( 400 );
    setMinimumHeight( 100 );  // if these two not present the GL widget is not visible
    glClearColor( 1.0, 1.0, 1.0, 1.0 )  // white background
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) // tried with arg = 0, no change
    glColor3f( 1.0, 0.0, 0.0, 0.0 ); // draw in red
    glBegin( GL_POLYGON );
    glVertex2f( -0.5, -0.5 );
    glVertex2f( -0.5,  0.5 );
    glVertex2f(  0.5,  0.5 );
    glVertex2f(  0.5, -0.5 );
    glEnd();
    glFlush();
    show();
    // resize( 300, 100 );  // has no effect  ??
    

    The background is black with random pixels scattered about. Nothing approaches the concept of a drawn polygon. I was hoping for a white background with a red polygon, square. The only things correct about the QGLWidget is that it exists and in the expected location.
    Is this sufficient information to detect the error?


  • Lifetime Qt Champion

    Hi,

    You should take a look at the QGLWidget documentation. It explains and shows the various methods you have to re-implement in order to initialise the OpenGL context, paint at the right time and handle resizing properly.



  • I was forced to back up to Qt3 because we have too much code to change and too few people. Now QGroupBox is cutting some widget too short. It ignores SetFixedHeight( 200 ) and SetMinimumHeight( 200 ); I will get back to this one when I can get the widgets all displayed again rather than being chopped off at the waist.
    Gave up on that for a while, I did revisit the QGLWidget docs, and now have the QGLWidget such that it is displayed and can draw a polygon in the center of it. In that widget code is:

    glClearColor( 1.0, 1.0, 1.0, 0.0 ); // white background preferred
    glClear( 0 );
    

    All the area outside of the drawn polygon is random bits, black background with many pixels set to various colors.
    What is needed to get the glClear() to work?
    The GL widget does not resize. It is contained within a QGridLayout that does resize. Most of the items within the grid are fixed sizes. Is there any way to get the size of a single cell within a grid layout as it is resized?
    Edit: Is there any way to get the size of the grid layout during resize? I don't see anything I can use for that.



  • Trying to do OpenGL drawing in the constructor of the widget isn't expected to work. It wasn't expected to work in Qt4 either. At that point, the OpenGL context hasn't been set up, the Window isn't necessarily on screen yet, so it doesn't necessarily have anywhere to draw into. If there's some other OpenGL context, the draw commands may go there instead of the widget whose constructor you are in, etc.

    You really need to only draw in a paintGL() override.



  • @wrosecrans Trying to do OpenGL drawing in the constructor of the widget isn't expected to work.
    That was not expected, but does make good sense. So far I am just building the static opening dialogs. I will now start adding things to cause updates and repaints.

    Still, something is rather puzzling. The GL widget is within a QGridlayout. When the parent widget is resized, the background of the GL widget changes. The random bits move around but never go completely black. The drawn polygon within the GL widget never changes. The size of the parent changes but not the GL widget. I will be thinking about that more.

    Edit: There is now a paintGL function. A variable has been added and the paintGL function will change colors every time it is called. When the main widget is resized the drawn polygon does change colors. and I just now added the glClear() to the painGL function and now it works. The background is indeed black.

    Now the final part: What must be done to get the GL widget to change size as its parent is resized?

    Oops, minor but significant change.
    The GL widget is within a QGridLayout. It spans several cells. It resizes vertically but not horizontally.



  • Before you added the paint function, the contents of the widget were undefined. So as the layout told it to resize, it was told to repaint, but nothing was intentionally written to the framebuffer because the paint method wasn't doing anything. So you were just seeing whatever junk was in some video memory.



  • @wrosecrans said in Cannot draw in QGLWidget:

    Before you added the paint function, the contents of the widget were undefined. So as the layout told it to resize, it was told to repaint, but nothing was intentionally written to the framebuffer because the paint method wasn't doing anything. So you were just seeing whatever junk was in some video memory.

    I understand that now.
    And I finally resolved the problem with resize. The GL widget had an AlignLeft. Eventually I read that any align option other than zero prohibits resizing. That was removed, a bit more attention paid to the exact cells being assigned, that now it all seems to be working.

    There is a line code that has no effect:

    sc_grid_layout->setRowStretch( 0,0 );
    sc_grid_layout->setCol_Stretch( 6,0 );
    

    The intent is to prohibit the top row and the far right column from stretching/resizing. Those lines have no effect.
    I do notice from the Qt Assistance:

    If the stretch factor is 0 and no other column in this table can grow at all, the column may still grow.

    That does not make sense to me. Seems like it would state that "if any" other column can grow the column can grow. I am guessing this is a typo that was fixed later.



  • This post is deleted!

Log in to reply