Results 1 to 1 of 1

Thread: [SOLVED] QGLWidget with QPainter commands: A drawing issue

  1. #1
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [SOLVED] QGLWidget with QPainter commands: A drawing issue

    I am trying to overlay QPainter commands inside of a custom QGLWidget class, called Plot. The QGLWidget is currently all OpenGL commands, but I would like to utilize QPainter calls to draw annotations and 2D on-top of the 3D scene. Pan, zoom and stretch is handled by moving the viewport of the scene.


    Please note that I hand-typed in this code and I cannot provide a compileable example.
    Qt Code:
    1. Plot::Plot( QWidget *parent ) : QGLWidget( parent )
    2. {
    3. setAutoFillBackground( false );
    4. setFocusPolicy( Qt::StrongFocus );
    5. setAttribute( Qt::WA_OpaquePaintEvent, false );
    6.  
    7. // More attribute setup
    8. }
    9.  
    10. void Plot::initializeGL()
    11. {
    12. QGLFormat myFormat = format();
    13.  
    14. myFormat.setDoubleBuffer( true );
    15. myFormat.setOverlay( 0 );
    16.  
    17. // Specifying the rendering context for this plot
    18. QGLContext *context = new QGLContext( myFormat );
    19. context->create();
    20. context->makeCurrent();
    21. }
    22.  
    23. void Plot::paintEvent( QPaintEvent *event )
    24. {
    25. makeCurrent();
    26.  
    27. // Box zoom temporarily disables auto swap to XOR the draw line
    28. if( (!mDrawBoxZoom || mDisableBox) && mScaleMode != G_SCALE )
    29. {
    30. // Ensure we are swapping buffers when not box zooming
    31. setAutoBurrerSwap( true );
    32. }
    33.  
    34. glMatrixMode( GL_MODELVIEW );
    35. glPushMatrix();
    36.  
    37. glEnable( GL_SCISSOR_TEST );
    38.  
    39. int viewportLeft, viewportRight, viewportTop, viewportBottom;
    40.  
    41. // mpCoord is an object that contains the viewport, ortho and window heights for
    42. // the entire Plot, so child object can position correctly.
    43. glViewport( 0, 0, mpCoord->getWindowWidth(), mpCoord->getWindowHeight() );
    44. glScissor( 0, 0, mpCoord->getWindowWidth(), mpCoord->getWindowHeight() );
    45.  
    46. qglClearColor( mBackgroundColor );
    47.  
    48. glClear( GL_COLOR_BUFFER_BIT );
    49.  
    50. // Clear the data section so each layer does not have to worry about
    51. // whether it should perform the clearing or not
    52. mpCoord->getViewport( viewportLeft, viewportRight, viewportBottom, viewportTop );
    53.  
    54. glViewport( viewportLeft, viewportBottom, viewportRight - viewportLeft,
    55. viewportTop - viewportBottom );
    56. glScissor( viewportLeft, viewportBottom, viewportRight - viewportLeft,
    57. viewportTop - viewportBottom );
    58.  
    59. qglClearColor( mPlotBackgroundColor );
    60. glClear( GL_COLOR_BUFFER_BIT );
    61.  
    62. if( !mDrawBoxZoom || mDisableBox || mScaleMode != G_SCALE )
    63. {
    64. // Draw each "Layer" of the Plot. Layers contain GL commands but are not
    65. // QGLWidgets themselves
    66. mpTitleLayer->paint();
    67. mpAxisLayer->paint();
    68. mpGridLayer->paint();
    69. mpDataLayer->paint();
    70. mpScaleLayer->paint();
    71.  
    72. //This code will eventually get put into QPainter commands, but for now, leave as is
    73. mpAnnotationLayer->paint();
    74. }
    75.  
    76. glMatrixMode( GL_PROJECTION );
    77. glLoadIdentity();
    78.  
    79. glOrtho( mpCoord->getXMin(), mpCoord->getXMax(),
    80. mpCoord->getYMin(), mpCoord->getYMax(),
    81. mpCoord->getZMin(), mpCoord->getZMax() );
    82.  
    83. glMatrixMode( GL_MODELVIEW );
    84. glLoadIdentity();
    85.  
    86. glViewport( viewportLeft, viewportBottom, viewportRight - viewportLeft,
    87. viewportTop - viewportBottom );
    88. glScissor( viewportLeft, viewportBottom, viewportRight - viewportLeft,
    89. viewportTop - viewportBottom );
    90.  
    91. if( mDrawBoxZoom && !mDisableBox )
    92. {
    93. drawBoxZoomXor();
    94. }
    95.  
    96. glFlush();
    97. glDisable( GL_SCISSOR_TEST );
    98.  
    99. // Ensure the state of the OpenGL
    100. glMatrixMode( GL_MODELVIEW );
    101. glPopMatrix();
    102.  
    103. // QPainter calls will go here, but not adding anything ATM
    104. }
    105.  
    106. void Plot::resizeGL( int width, int height )
    107. {
    108. mpCoord->resize( width, height );
    109.  
    110. mpTitleLayer->resize( width, height );
    111. mpAxisLayer->resize( width, height );
    112. mpGridLayer->resize( width, height );
    113. mpDataLayer->resize( width, height );
    114. mpScaleLayer->resize( width, height );
    115. }
    To copy to clipboard, switch view to plain text mode 

    I have followed the Overpainting example as best I could and from my understanding the "only" changes I should have to make to my class is to change paintGL to paintEvent(QPaintEvent*), put all QPainter commands after the OpenGL commands, and pop the matrix stack at the end.

    With these changes to my Plot, the initial image appears just fine, however the mousePressEvent, mouseMoveEvent and mouseReleaseEvents flickers HORRIBLY and the plot does not actually "move". All the values used to change the viewport variables are being set properly, but the image is not changing (the paintEvent is not being called). If I use QGLWidget::update()instead of QGLWidget::updateGL() during the mouse functions, the paintEvent is called, but only a white scene appears.

    Any thoughts or ideas?

    Qt v4.7.2
    VS2008 SP1


    Added after 1 39 minutes:


    Well apparently you have to call update(), not updateGL() AND have QPainter code at the end. No QPainter calls, nothing paints! Hope this helps someone else from loosing hair!
    Last edited by Rayven; 28th April 2011 at 18:50. Reason: Solved
    Every little child knows
    if you cant see dreams
    your eyes are blind

    Moxy Fruvous

  2. The following user says thank you to Rayven for this useful post:

    Guid (13th June 2011)

Similar Threads

  1. QGLWidget tearing when drawing video
    By koan in forum Qt Programming
    Replies: 0
    Last Post: 9th October 2010, 16:24
  2. qglwidget not always drawing
    By spraff in forum Qt Programming
    Replies: 3
    Last Post: 24th June 2009, 19:44
  3. QPainter on QGLWidget
    By h123 in forum Qt Programming
    Replies: 2
    Last Post: 17th November 2008, 12:51
  4. about QGLWidget and QPainter
    By showhand in forum Qt Programming
    Replies: 5
    Last Post: 12th November 2008, 10:45
  5. [qt4,win,g++] QPainter on a QGLWidget
    By jh in forum Qt Programming
    Replies: 4
    Last Post: 28th July 2008, 06:29

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.