Results 1 to 2 of 2

Thread: QGLWidget crashes on frequent updates

  1. #1
    Join Date
    Dec 2009
    Location
    Kiev, Ukraine
    Posts
    16
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question QGLWidget crashes on frequent updates

    Hi. I draw in paintGL () on my Widget inherited from QGLWidget through QPainter functions, but after receiving a certain number of points the application crashes, the impression that somewhere something is overflowing, but I can not understand why. With 6 copies of the widget on the form it holds for 400-600 points for each graph, with 3 - to 900. I add 1 point and than update paintGL() and if i draw the graph with around 20000 points at once and update paintGl() it will be ok but when i start normal cycle at about 20600 it will crash in same way. I understand that something overflowing, help me please understand where is my problem.
    System: WinXp, Qt 4.6.3 (mingw 4.4.3) . Project in attachment.

    Qt Code:
    1. void GraphDraw::paintGL()
    2. {
    3. QPainter painter(this);
    4. if (points.count()>2 || pointsx.count()>2) {
    5. float maxXp;
    6. float minXp;
    7. float maxYp;
    8. float minYp;
    9. float maxXt;
    10. float minXt;
    11. float maxYt;
    12. float minYt;
    13. float stepX;
    14. float stepY;
    15.  
    16. painter.translate(0, float(height())/2);
    17.  
    18. if (!zoom) {
    19. maxXt = maxX;
    20. minXt = minX;
    21. maxYt = maxY;
    22. minYt = minY;
    23. }
    24. else {
    25. maxXt = zoomListMax[zoom-1].first;
    26. minXt = zoomListMin[zoom-1].first;
    27. maxYt = zoomListMax[zoom-1].second;
    28. minYt = zoomListMin[zoom-1].second;
    29. }
    30.  
    31. stepX = qAbs(maxXt - minXt)/10;
    32. stepY = qAbs(maxYt - minYt)/10;
    33. maxXp = maxXt + 0.4*stepX;
    34. minXp = minXt - 0.4*stepX;
    35. maxYp = maxYt + 0.4*stepY;
    36. minYp = minYt - 0.4*stepY; //
    37. wsx = float(width()) / qAbs(maxXp - minXp);
    38. wsy = float(height()) / qAbs(maxYp - minYp);
    39. wdx = float(width())/2 - (maxXp*wsx + minXp*wsx)/2;
    40. wdy = - (maxYp*wsy + minYp*wsy)/2;
    41.  
    42.  
    43. for (float i=minXt;i<maxXt+stepX;i+=stepX) {
    44. painter.setPen(palette().dark().color());
    45. painter.drawLine(QPointF(i*wsx + wdx, -float(height())/2), QPointF(i*wsx + wdx, float(height())/2));
    46. painter.setPen(QPen(Qt::darkGreen));
    47. painter.drawText(QPointF(i*wsx + wdx -float(fm->width(QString::number(i,'f',1))/2), float(25)/2), QString::number(i,'f',1));
    48. }
    49. for (float i=minYt;i<=maxYt+stepY;i+=stepY) {
    50. painter.setPen(palette().dark().color());
    51. painter.drawLine(QPointF(0, -(i*wsy + wdy)), QPointF(width(), -(i*wsy + wdy)));
    52. painter.setPen(QPen(Qt::darkGreen));
    53. painter.drawText(QPointF(5, -(i*wsy + wdy+4)),QString::number(i,'f',4));
    54. }
    55.  
    56. painter.setRenderHint(QPainter::Antialiasing, antialiasing);//сглаживание
    57. painter.setPen(QPen(Qt::red, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    58.  
    59. if (sorting) {
    60. QPointF *mpoints = new QPointF[points.count()];
    61. QList<float> pointsk(points.keys());
    62. for (int m = 0; m<points.count(); m++)
    63. mpoints[m]=QPointF(pointsk[m]*wsx + wdx, -(points.value(pointsk[m])*wsy + wdy));
    64. painter.drawPolyline(mpoints, points.count());
    65. delete []mpoints;
    66. }
    67. else {
    68. QPointF *mpoints = new QPointF[pointsx.count()];
    69. for (int m = 0; m<pointsx.count(); m++)
    70. mpoints[m]=QPointF(pointsx[m]*wsx + wdx, -(pointsy[m]*wsy + wdy));
    71. painter.drawPolyline(mpoints, pointsx.count());
    72. delete []mpoints;
    73. }
    74.  
    75. painter.translate(0, -float(height())/2);
    76.  
    77. painter.setPen(QPen(Qt::black, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    78. painter.drawRect(QRect(0, 0, width(), height()));
    79. // font.setStyleStrategy(QFont::OpenGLCompatible);
    80. // font.setStyleStrategy(QFont::PreferAntialias);
    81.  
    82. painter.drawText(float(width())-fm->width("<--- " + xlegend)-10, 15, "<--- " + xlegend);
    83.  
    84. painter.rotate(90);
    85.  
    86. painter.drawText(30, -width()+20, ylegend + " --->");
    87.  
    88. painter.rotate(-90);
    89.  
    90. if (mouseState)
    91. painter.drawRect(QRectF(clickPoint, currPoint));
    92. }
    93. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. GraphDraw::GraphDraw(QWidget *parent) //: QWidget(parent)
    2. : QGLWidget(QGLFormat(QGL::SampleBuffers/* | QGL::DoubleBuffer | QGL::DirectRendering*/), parent)
    3. {
    4. // setAttribute(Qt::WA_OpaquePaintEvent);
    5. setBackgroundRole(QPalette::Base);
    6. setAutoFillBackground(true);
    7. setMouseTracking(true);
    8. setCursor(Qt::CrossCursor);
    9. maxX = -(numeric_limits<float>::max());
    10. minX = numeric_limits<float>::max();
    11. maxY = -(numeric_limits<float>::max());
    12. minY = numeric_limits<float>::max();
    13. QFont fonttemp(font()); fonttemp.setPointSize(6); setFont(fonttemp);
    14. fm = new QFontMetrics(font());
    15. sorting = false;
    16. antialiasing = false;
    17.  
    18. mouseState = 0;
    19. zoom = 0;
    20. wsx = 0;
    21. wsy = 0;
    22. wdx = 0;
    23. wdy = 0;
    24. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGLWidget crashes on frequent updates

    Hi there!

    With 6 of your widgets you are creating 6 OpenGL contexts. And for each repaint you create a new painter on it. Thats quite expensive.

    Try the following: Make your GraphDraw a simple Widget like it was before. Move your paint calls to its paintEvent and use the passed painter.

    Add your 6 widgets to a big graphicsscene using addWidget. Setup the GraphicsView to use OpenGL. That way all drawing is done via OpenGL, but with only one bigger OpenGL context.

    Qt Code:
    1. scene = new QGraphicsScene();
    2.  
    3. graphicsView = new QGraphicsView(this);
    4.  
    5. graphicsView->setRenderHint(QPainter::Antialiasing, true);
    6. graphicsView->setOptimizationFlags(QGraphicsView::DontSavePainterState);
    7. graphicsView->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
    8. graphicsView->setResizeAnchor(QGraphicsView::AnchorViewCenter);
    9.  
    10. graphicsView->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
    11.  
    12. graphicsView->setScene(scene);
    13. graphicsView->centerOn(0,0);
    14. graphicsView->show();
    15.  
    16. scene->addWidget ..
    To copy to clipboard, switch view to plain text mode 
    HIH

    BTW: Have you checked QwtPlot http://qwt.sourceforge.net/

    Joh
    Last edited by JohannesMunk; 13th September 2010 at 10:40.

Similar Threads

  1. installing updates
    By uj in forum Installation and Deployment
    Replies: 3
    Last Post: 13th July 2012, 08:15
  2. QMenuBar Updates
    By johnmauer in forum Qt Programming
    Replies: 5
    Last Post: 20th January 2010, 13:35
  3. QFileDialog QGLWidget crashes
    By SenSej in forum Newbie
    Replies: 1
    Last Post: 17th October 2008, 17:06
  4. Painting updates
    By QbelcorT in forum Qt for Embedded and Mobile
    Replies: 6
    Last Post: 29th September 2008, 00:48
  5. Is QMap efficient in case of frequent read access ?
    By yellowmat in forum Qt Programming
    Replies: 4
    Last Post: 19th November 2006, 08:20

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.