Results 1 to 8 of 8

Thread: Why the drawing cannot be shown when using paintGL() in the QGLWidget?

  1. #1
    Join Date
    May 2011
    Location
    North Carolina
    Posts
    23
    Thanks
    10
    Qt products
    Qt4

    Default Why the drawing cannot be shown when using paintGL() in the QGLWidget?

    I reimplemented paintGL() :

    Qt Code:
    1. void QGLWidget::paintGL()
    2. {
    3. QPainter ipainter(this);
    4. ipainter.setWindow(QRect(this->translate.X,this->translate.Y,1,1));
    5. ipainter.setRenderHint(QPainter::Antialiasing, true);
    6. CanvasDrawer->RunOpenGL(&ipainter);
    7. }
    To copy to clipboard, switch view to plain text mode 

    the OpenGL codes are inside the function RunOpenGL .
    The widget is black and none of the Opengl drawings is shown.

    However, after I changed paintGL() to paintEvent:

    Qt Code:
    1. void grCanvas::paintEvent(QPaintEvent* event)
    2. {
    3. QPainter ipainter(this);
    4. ipainter.setWindow(QRect(this->translate.X,this->translate.Y,1,1));
    5. ipainter.setRenderHint(QPainter::Antialiasing, true);
    6. CanvasDrawer->RunOpenGL(&ipainter);
    7. }
    To copy to clipboard, switch view to plain text mode 

    the drawing result will show on the widget well

    What's the problem? why the paintGL doesn't work? It is said to work like paintEvent

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Why the drawing cannot be shown when using paintGL() in the QGLWidget?

    Why are you using a QPainter to render with OpenGL ? It should be enough to reimplement paintGL() alone, and use OpenGL calls to render the scene. Can you post RunOpenGL code ?
    why the paintGL doesn't work? It is said to work like paintEvent
    Where it is said so ? paintEvent "will cause the virtual paintGL() function to be called.", they are not the same.

  3. The following user says thank you to stampede for this useful post:

    superwave (28th June 2011)

  4. #3
    Join Date
    May 2011
    Location
    North Carolina
    Posts
    23
    Thanks
    10
    Qt products
    Qt4

    Default Re: Why the drawing cannot be shown when using paintGL() in the QGLWidget?

    Thanks a lot.

    However, since the paintGL() is called by the QGLWidget automatically, why the OpenGL drawing will not be shown on widget, but paintEvent works?

  5. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Why the drawing cannot be shown when using paintGL() in the QGLWidget?

    It depends on what you draw in RunOpenGL, I can only guess, but if you are passing QPainter to that method, its possible that you are not using any OpenGL calls, just regular QPainter drawing code. Can you post the code ?

  6. #5
    Join Date
    May 2011
    Location
    North Carolina
    Posts
    23
    Thanks
    10
    Qt products
    Qt4

    Default Re: Why the drawing cannot be shown when using paintGL() in the QGLWidget?

    Here is the code in the function RunOpenGL:
    Qt Code:
    1. IRGraph *graph = docManager->GetIRGraph();
    2. int nodeNum = graph->GetNodeNum();
    3. int squareN = maxLevel + 1;
    4. double x1 = 0.0;
    5. double y1 = 0.0;
    6. double x2 = 1.0;
    7. double y2 = 1.0;
    8. double w = (x2 - x1) / (double)squareN;
    9. double h = y2 - y1;
    10.  
    11. int *nodeOrders = new int [nodeNum];
    12. CalNodeOrder(nodeOrders);
    13.  
    14. double *posX = new double [nodeNum];
    15. double *posY = new double [nodeNum];
    16.  
    17. CalDiagramSquareNodePos(w * 0.96, h, posX, posY);
    18.  
    19. glEnable(GL_BLEND);
    20. glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
    21.  
    22. int level, i, member, colorIdx, len, nodeId;
    23. // bool isCore;
    24. double squareX, squareY;
    25. float alpha;
    26.  
    27. // squareY = y1 + h * 0.05;
    28. squareY = y1;
    29. for (level=0; level<=maxLevel; level++)
    30. {
    31. // squareX = x1 + w * level + w * 0.05;
    32. squareX = x1 + w * level + w * 0.02;
    33.  
    34. // Draw nodeNum nodes
    35. for (i=0; i<nodeNum; i++)
    36. {
    37. nodeId = nodeOrders[i];
    38. if (nodeId == -1)
    39. continue;
    40. member = graph->GetNodeMembership(nodeId);
    41. // isCore = graph->IsClusterCore(nodeId);
    42. len = graph->GetDistToFocus(nodeId, this->focusKey);
    43. if ((level < maxLevel && len <= level) || (level == maxLevel && len >= level))
    44. {
    45. // glPointSize((float)(nodeSize*(this->GetIRGraph()->GetNodeCentrality(nodeId))));
    46. glPointSize(graph->IsNodeSelected(nodeId) ? (float)(nodeSize + distortionFactor) : (float)nodeSize);
    47. alpha = graph->IsNodeSelected(nodeId) ? 1.0f : 0.5f;
    48. }
    49. else
    50. {
    51. glPointSize(1.f);
    52. alpha = 0.f;
    53. }
    54. glBegin(GL_POINTS);
    55. colorIdx = member % IRGPixelDrawer::colorNum;
    56. glColor4f(qualitativeColorTable[colorIdx][0]/255.f, qualitativeColorTable[colorIdx][1]/255.f, qualitativeColorTable[colorIdx][2]/255.f, alpha);
    57. glVertex2d(squareX+posX[i], squareY+posY[i]);
    58. glEnd();
    59. }
    60.  
    61. // Draw a boundary rectangle
    62. colorIdx = 9;
    63. alpha = 1.0f - 0.8f/(float)maxLevel*(float)level;
    64. glColor4f(qualitativeColorTable[colorIdx][0]/255.f, qualitativeColorTable[colorIdx][1]/255.f, qualitativeColorTable[colorIdx][2]/255.f, alpha);
    65. glBegin(GL_LINE_LOOP);
    66. glVertex2d(squareX, squareY);
    67. glVertex2d(squareX + w * 0.96, squareY);
    68. glVertex2d(squareX + w * 0.96, squareY + h);
    69. glVertex2d(squareX, squareY + h);
    70. glEnd();
    71. }
    72.  
    73. glDisable(GL_BLEND);
    74.  
    75. delete [] nodeOrders;
    76. delete [] posX;
    77. delete [] posY;
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Why the drawing cannot be shown when using paintGL() in the QGLWidget?

    Ok, maybe I just can't see it but I think there is no QPainter drawing code inside that method, so why are you passing QPainter pointer to it ? Does it work if you do some "basic" OpenGL drawing inside, like drawing a line or few points ?

  8. #7
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why the drawing cannot be shown when using paintGL() in the QGLWidget?

    I Think the proble is creating a QPainter inside paintGL().

    You don't use the painter in RunOpenGL. Why create it??
    A camel can go 14 days without drink,
    I can't!!!

  9. #8
    Join Date
    May 2011
    Location
    North Carolina
    Posts
    23
    Thanks
    10
    Qt products
    Qt4

    Default Re: Why the drawing cannot be shown when using paintGL() in the QGLWidget?

    The RunOpenGL function is owned by 4 classes inherited from a same parent class. Some RunOpenGL functions have Qt drawing codes, some have only OpenGL codes.

    The RunOpenGL is a virtual function, some classes need qt drawing codes.

Similar Threads

  1. Replies: 0
    Last Post: 28th April 2011, 18:40
  2. QGLWidget tearing when drawing video
    By koan in forum Qt Programming
    Replies: 0
    Last Post: 9th October 2010, 16:24
  3. Replies: 0
    Last Post: 8th April 2010, 16:06
  4. qglwidget not always drawing
    By spraff in forum Qt Programming
    Replies: 3
    Last Post: 24th June 2009, 19:44
  5. QGLWidget, toolbox and paintGL()
    By mickey in forum Qt Programming
    Replies: 3
    Last Post: 21st March 2006, 01:05

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.