Results 1 to 3 of 3

Thread: Low speed drawing with qpainter

  1. #1
    Join Date
    Apr 2018
    Posts
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo

    Default Low speed drawing with qpainter

    hi , i'm new to qt
    i have low speed in this app
    please help

    Qt Code:
    1. void MainWindow::LoadGages(void)
    2. {
    3. QImage sb_img(":/new/prefix1/spb");
    4. QImage tb_img(":/new/prefix1/tkb");
    5. QImage nd_img(":/new/prefix1/nd");
    6. QImage cn_img(":/new/prefix1/cen");
    7. QImage ov_img(":/new/prefix1/ov");
    8.  
    9. sb = sb.fromImage(sb_img.scaled(450,450,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
    10. tb = tb.fromImage(tb_img.scaled(450,450,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
    11. nd = nd.fromImage(nd_img.scaled(146,115,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
    12. cn = cn.fromImage(cn_img.scaled(41,41,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
    13. ov = ov.fromImage(ov_img.scaled(400,325,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
    14.  
    15. speed=103;
    16. takho=103;
    17.  
    18. qreal alpha=0.0;
    19.  
    20. for(qreal roty=270;roty<=360;roty+=5)
    21. {
    22. QPixmap Left=drawGauge(sb,speed,450,0,0,roty,225,225,alpha);
    23. QPixmap Right=drawGauge(tb,speed,450,0,0,-roty,225,225,alpha);
    24. alpha+=0.15;
    25. ui->label->setPixmap(drawBoth(Left,Right));
    26. QApplication::processEvents();
    27. }
    28.  
    29. for(qreal val=103;val<=370;val+=5)
    30. {
    31. QPixmap Left=drawGauge(sb,val,450,0,0,0,0,0,1.0);
    32. QPixmap Right=drawGauge(tb,val,450,0,0,0,0,0,1.0);
    33. ui->label->setPixmap(drawBoth(Left,Right));
    34. QApplication::processEvents();
    35. }
    36.  
    37. for(qreal val=370;val>=103;val-=5)
    38. {
    39. QPixmap Left=drawGauge(sb,val,450,0,0,0,0,0,1.0);
    40. QPixmap Right=drawGauge(tb,val,450,0,0,0,0,0,1.0);
    41. ui->label->setPixmap(drawBoth(Left,Right));
    42. QApplication::processEvents();
    43. }
    44.  
    45. QPixmap Left=drawGauge(sb,103,450,0,0,0,0,0,1.0);
    46. QPixmap Right=drawGauge(tb,103,450,0,0,0,0,0,1.0);
    47. ui->label->setPixmap(drawBoth(Left,Right));
    48.  
    49. //timerId=startTimer(1);
    50. update();
    51. }
    52.  
    53. QPixmap MainWindow::drawBoth(QPixmap g1,QPixmap g2)
    54. {
    55. QPixmap win(ui->label->size());
    56. win.fill(Qt::transparent);
    57. QRect winR(ui->label->x(),ui->label->y(),ui->label->width(),ui->label->height());
    58. p.begin(&win);
    59. p.setRenderHint(QPainter::Antialiasing);
    60. p.setRenderHint(QPainter::SmoothPixmapTransform);
    61. p.setRenderHint(QPainter::HighQualityAntialiasing);
    62.  
    63. p.drawPixmap(10,25,g1);
    64. p.drawPixmap(winR.x()+winR.width()-450-75,25,g2);
    65.  
    66. p.end();
    67. return win;
    68. }
    69.  
    70. QPixmap MainWindow::drawGauge(QPixmap Gauge,qreal value,int scale,qreal z,qreal x,qreal y,qreal tx,qreal ty,qreal alpha)
    71. {
    72. QPixmap win(Gauge.size());
    73. win.fill(Qt::transparent);
    74. p.begin(&win);
    75. p.setRenderHint(QPainter::Antialiasing);
    76. p.setRenderHint(QPainter::SmoothPixmapTransform);
    77. p.setRenderHint(QPainter::HighQualityAntialiasing);
    78. p.drawPixmap(0,0,Gauge);
    79. p.setTransform(GetRotation(value,Qt::ZAxis,(450/2)+5,(450/2)+5));
    80. p.setViewport(p.viewport().x()-5,p.viewport().y()-5,p.viewport().width(),p.viewport().height());
    81. p.drawPixmap(450/2,450/2,nd);
    82. p.resetMatrix();
    83. p.drawPixmap((450/2)-20,(450/2)-20,cn);
    84. p.setOpacity(0.2);
    85. p.drawPixmap(25,25,ov);
    86. p.end();
    87.  
    88. QPixmap gauge=win;
    89. win.fill(Qt::transparent);
    90. p.begin(&win);
    91. p.setRenderHint(QPainter::Antialiasing);
    92. p.setRenderHint(QPainter::SmoothPixmapTransform);
    93. p.setRenderHint(QPainter::HighQualityAntialiasing);
    94. p.setTransform(GetRotation(z,Qt::ZAxis,tx,ty));
    95. p.drawPixmap(0,0,gauge);
    96. p.end();
    97.  
    98. gauge=win;
    99. win.fill(Qt::transparent);
    100. p.begin(&win);
    101. p.setRenderHint(QPainter::Antialiasing);
    102. p.setRenderHint(QPainter::SmoothPixmapTransform);
    103. p.setRenderHint(QPainter::HighQualityAntialiasing);
    104. p.setTransform(GetRotation(x,Qt::XAxis,tx,ty));
    105. p.drawPixmap(0,0,gauge);
    106. p.end();
    107.  
    108. gauge=win;
    109. win.fill(Qt::transparent);
    110. p.begin(&win);
    111. p.setRenderHint(QPainter::Antialiasing);
    112. p.setRenderHint(QPainter::SmoothPixmapTransform);
    113. p.setRenderHint(QPainter::HighQualityAntialiasing);
    114. p.setTransform(GetRotation(y,Qt::YAxis,tx,ty));
    115. p.setOpacity(alpha);
    116. p.drawPixmap(0,0,gauge);
    117. p.end();
    118.  
    119. gauge=win;
    120. win.fill(Qt::transparent);
    121. p.begin(&win);
    122. p.setRenderHint(QPainter::Antialiasing);
    123. p.setRenderHint(QPainter::SmoothPixmapTransform);
    124. p.setRenderHint(QPainter::HighQualityAntialiasing);
    125. p.setOpacity(alpha);
    126. p.drawPixmap(0,0,gauge);
    127. p.end();
    128.  
    129. QImage _retval = win.toImage();
    130. QPixmap retval = retval.fromImage(_retval.scaled(scale,scale,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
    131.  
    132. return retval;
    133. }
    134.  
    135. QTransform MainWindow::GetRotation(qreal rotation,Qt::Axis axis,qreal dx,qreal dy)
    136. {
    137. QTransform qtr;
    138. qtr.translate(dx,dy);
    139. qtr.rotate(rotation,axis);
    140. qtr.translate(-dx,-dy);
    141. return qtr;
    142. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Low speed drawing with qpainter

    Well, there are many optimizations you can do.
    First, you are creating all the objects on the stack locally for each method, so they get destroyed and created for each method call - and creation and deletion are very expensive operations.
    Use members instead, this will have them created only once.

    Then, it seems you are emulating animation by drawing on the pixmaps and then settings these pixmaps into lables, which is also very expensive.
    Why don't you draw directly on to the label/widget?

    When you use toImage() you are copying that image in another QImage to use as input to fromImage() - if you use the output of toImage() directly in fromImage() you will save the copying of the image which is also very expensive.

    On top of that be sure to build with -O2 optimization level.

    I am sure there are many other such things that can be made better - maybe some other forum members can see and point them out.

    All these things will make your code faster, but in my opinion you are using the wrong solution to the problem you have - which animating graphics - and doing that in the widget world is not efficient no matter how well you trim your code.
    Doing this in QML will make your UI fast, and save you code as well.
    I strongly urge you to look in to QML and how to implement what you have here in QML.
    Or alternatively use the QGraphicsView framework (https://doc.qt.io/qt-5.10/graphicsview.html)
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Apr 2018
    Posts
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo

    Default Re: Low speed drawing with qpainter

    Thank you for your answare...

    now, i want rotate drawn qpixmap

    Qt Code:
    1. void MainWindow::paintEvent(QPaintEvent *)
    2. {
    3. QPainter painter(this);
    4. //painter.setPen(Qt::black);
    5. painter.setBrush(Qt::black);
    6. painter.drawRect(0,0,1360,512);
    7.  
    8. painter.setRenderHint(QPainter::Antialiasing);
    9. painter.setRenderHint(QPainter::SmoothPixmapTransform);
    10. painter.setRenderHint(QPainter::HighQualityAntialiasing);
    11.  
    12. int speedoX=25,speedoY=25;
    13.  
    14. painter.resetMatrix();
    15. painter.resetTransform();
    16. painter.setOpacity(1.0);
    17.  
    18. painter.drawPixmap(speedoX,speedoX,sb);
    19. painter.setTransform(GetRotation(speedo.value,Qt::ZAxis,speedoX+((450/2)+5),speedoY+((450/2)+5)));
    20. painter.setViewport(painter.viewport().x()-5,painter.viewport().y()-5,painter.viewport().width(),painter.viewport().height());
    21. painter.drawPixmap(speedoX+(450/2),speedoY+(450/2),nd);
    22. painter.resetMatrix();
    23. painter.drawPixmap(speedoX+((450/2)-20),speedoY+((450/2)-20),cn);
    24. painter.setOpacity(0.3);
    25. painter.drawPixmap(speedoX+25,speedoY+25,ov);
    26.  
    27.  
    28. int takhoX=1360-450-75,takhoY=25;
    29.  
    30. painter.resetMatrix();
    31. painter.resetTransform();
    32. painter.setOpacity(1.0);
    33.  
    34. painter.drawPixmap(takhoX,takhoY,tb);
    35. painter.setTransform(GetRotation(takho.value,Qt::ZAxis,takhoX+((450/2)+5),takhoY+((450/2)+5)));
    36. painter.setViewport(painter.viewport().x()-5,painter.viewport().y()-5,painter.viewport().width(),painter.viewport().height());
    37. painter.drawPixmap(takhoX+(450/2),takhoY+(450/2),nd);
    38. painter.resetMatrix();
    39. painter.drawPixmap(takhoX+((450/2)-20),takhoY+((450/2)-20),cn);
    40. painter.setOpacity(0.3);
    41. painter.drawPixmap(takhoX+25,takhoY+25,ov);
    42.  
    43. }
    44.  
    45. how can i do ?
    To copy to clipboard, switch view to plain text mode 
    Last edited by NavidMicro; 4th May 2018 at 05:20.

Similar Threads

  1. Drawing with QPainter in a QGLWidget
    By jshafferman in forum Qt Programming
    Replies: 1
    Last Post: 2nd May 2014, 20:09
  2. Text drawing with QPainter
    By Rambobino in forum Qt Programming
    Replies: 2
    Last Post: 1st November 2010, 17:26
  3. Replies: 5
    Last Post: 21st March 2009, 09:10
  4. Drawing speed: QPixmap vs QPicture
    By maverick_pol in forum Qt Programming
    Replies: 1
    Last Post: 2nd January 2008, 19:17

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.