Results 1 to 9 of 9

Thread: QWidget update get so cpu usage

  1. #1
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QWidget update get so cpu usage

    hi everyone,

    i have a timer with 10 ms interval connected to a method that run update function of my widget and i havent paintEvent either, but i get 25 percent cpu usage for my application and 25 percent for xorg, why updating a fixed widget get such a horrible cpu, if i comment update function my cpu usage is 1percent,
    do we have any other function for updating widgets?

    tnx.

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: QWidget update get so cpu usage

    The problem is probably not in the update function, but the things that it has to do 100 times a second! Your widget is very busy updating itself. Without knowing anything about your application it is hard to understand why something should be updated 100 times a second for UI purposes.

  3. #3
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QWidget update get so cpu usage

    i want to create a smooth animation from a start point to end point in constant time like 4 seconds from 0 to 360, so after using Animation framework and some Gl painting i figured out that if myself paint this animation with timer and changing paintevent my cpu usage is less than other ways, but its not enough too, so i think maybe my painting functions get this much cpu and i comment all painting functions, but i realized my cpu usage havent any change (maybe only two percent), after all comments i have a white widget that update only itself in this time and nothing changed too, but it get this much cpu, as i saied if i comment update my application cpu usage get only 1 percent.
    after all, if you have any ideas about how i must painting a smotth animation in constant time without increase of my steps i would thank of you.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QWidget update get so cpu usage

    The human eye cannot detect anything that changes faster than about 30 times per second, and even then can't process the changes in the brain unless the changes are gradual (like a movie). So it makes no sense at all to try to animate anything faster than 10 - 20 steps per second (i.e a timer with timeout of 100 - 50 ms). Otherwise you're just burning up CPU cycles for no good reason and slowing everything else down.

    If your animation steps can be pre-computed (that is, drawn into a series of images once instead of being drawn each step in real time), then store the images as resources in your application and simply display each image one after the other using QImage or QPixmap. This will probably be much faster than a bunch of QPainter operations with pens, brushes, and shapes.
    Last edited by d_stranz; 19th August 2012 at 00:27.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QWidget update get so cpu usage

    i dont realy get it, i change everything my steps and my constant time too, but nothing change
    ok i think i cant solve my current situation with GraphicsView or widgets painting, any way this is a sample code like what i have
    Qt Code:
    1. MyHand::MyHand(QGraphicsItem *parent) :
    2. {
    3. handchanger = new QPropertyAnimation(this, "rotationAngle");
    4. connect(handchanger, SIGNAL(finished()), this, SLOT(reanimate()));
    5. handchanger->setDuration(20000);
    6. handchanger->setStartValue(0);
    7. handchanger->setEndValue(360);
    8. handchanger->setEasingCurve(QEasingCurve::Linear);
    9. setCacheMode(ItemCoordinateCache);
    10. }
    11.  
    12. void MyHand::setRotationValue(qreal angle)
    13. {
    14. if(m_angle != angle)
    15. {
    16. m_angle = angle;
    17. QPointF o = boundingRect().bottomLeft();
    18. QTransform t;
    19. t.translate(o.x(), o.y());
    20. t.rotate(m_angle, Qt::ZAxis);
    21. t.translate(-o.x(), -o.y());
    22. setTransform(t);
    23. }
    24. }
    25. void MyHand::reanimate()
    26. {
    27. handchanger->start();
    28. }
    29.  
    30. void handscene::drawBackground(QPainter *painter, const QRectF &rect)
    31. {
    32. if(hand->line() == QLineF(0,0,0,0))
    33. {
    34. hand->setPen(QPen(Qt::black, 2));
    35. hand->setLine(rect.width() / 2, rect.height() / 2, rect.width()/2, 0);
    36. addItem(hand);
    37. hand->start();
    38. hand->setZValue(10);
    39.  
    40.  
    41. back = new QGraphicsEllipseItem(0,0,rect.width(), rect.height());
    42. back->setBrush(Qt::yellow);
    43. back->setZValue(5);
    44. addItem(back);
    45. }
    46. }
    47.  
    48. int main(int argc, char *argv[])
    49. {
    50. QApplication a(argc, argv);
    51.  
    52. v.setScene(new radarcircle);
    53. // v.setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
    54. v.setGeometry(0, 0, 700, 700);
    55. v.setSceneRect(0,0 ,700,700);
    56. v.show();
    57.  
    58. return a.exec();
    59. }
    To copy to clipboard, switch view to plain text mode 

    in any rate and duration time i have 50% cpu usage total for xorg and my application,
    i m using QGLWIdget for my GraphicsView viewport and now my gpu usage is 90% and this is so bad too,
    anyone have any idea how can i reduce this much usages?
    i thought of XOR painting like old times in Ms Dos .
    this is a simple rotate animation and realy i think it should use only 5% of cpu in worst case.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QWidget update get so cpu usage

    How does this behave? It barely registers on my machine
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class RadarScene: public QGraphicsScene
    4. {
    5. Q_OBJECT
    6.  
    7. enum { Radius = 100, SweepTime = 20000 };
    8.  
    9. public:
    10. RadarScene(QObject *p = 0): QGraphicsScene(p), m_hand(0), m_tick(0)
    11. {
    12. QGraphicsEllipseItem *e = new QGraphicsEllipseItem(-Radius, -Radius, 2 * Radius, 2 * Radius);
    13. e->setBrush(Qt::yellow);
    14. addItem(e);
    15.  
    16. m_hand = new QGraphicsLineItem(0, 0, Radius, 0);
    17. m_hand->setPen(QPen(Qt::black, 2));
    18. addItem(m_hand);
    19.  
    20. m_timer = new QTimer(this);
    21. connect(m_timer, SIGNAL(timeout()), SLOT(tick()));
    22. m_timer->start(SweepTime / 360);
    23. }
    24.  
    25. private slots:
    26. void tick()
    27. {
    28. m_tick = (m_tick + 1) % 360;
    29. m_hand->setRotation(m_tick);
    30. }
    31.  
    32. private:
    33. int m_tick;
    34. QTimer *m_timer;
    35. };
    36.  
    37.  
    38. int main(int argc, char **argv)
    39. {
    40. QApplication app(argc, argv);
    41.  
    42. RadarScene r;
    43. w.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    44. w.show();
    45.  
    46. return app.exec();
    47. }
    48. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    If you are using an OpenGL renderer and there is no OpenGL accelerated video driver then you system may be falling back to a software OpenGL implementation. This will be CPU intensive.
    Last edited by ChrisW67; 20th August 2012 at 00:47.

  7. #7
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QWidget update get so cpu usage

    first thanks for yout answer and your code
    If you are using an OpenGL renderer and there is no OpenGL accelerated video driver then you system may be falling back to a software OpenGL implementation. This will be CPU intensive.
    yes i know that and i saied before when i m using gl rendering my GPU usage go to 90% and its bad too (we wouldnt implement a 3d game )

    and about your code, i test it and please change radius to 350 and your timer sweep to 5000 in my system cpu usage of xorg and our application totally is 60%

    and i dont think we impement it in more than 30 frames per second ???

    thanks again.

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QWidget update get so cpu usage

    5000 milliseconds to do 360 steps is over 70 updates per second. Even so, I don't a see a ridiculous CPU load; about 10% of one core between my application and X with default renderer. Increasing the steps to 3 degrees and doing 120 per revolution (24fps movie rate) drops that to < 5.

  9. #9
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QWidget update get so cpu usage

    Quote Originally Posted by ChrisW67 View Post
    5000 milliseconds to do 360 steps is over 70 updates per second. Even so, I don't a see a ridiculous CPU load; about 10% of one core between my application and X with default renderer. Increasing the steps to 3 degrees and doing 120 per revolution (24fps movie rate) drops that to < 5.
    ok i get your point, you want me to fixed timer interval to 50 ms and change my steps for increasing my speed, i did this before but what about smooth animating? it seems like a jump not continusely rounding,
    any way my cpu load is 20% in this manner, are you sure you change your radius?

Similar Threads

  1. Using QWidget::update() from non-GUI thread
    By some_birdie in forum Qt Programming
    Replies: 3
    Last Post: 21st June 2011, 09:11
  2. QWidget::update does not work!
    By sapali in forum Qt Programming
    Replies: 8
    Last Post: 17th March 2011, 16:56
  3. Unable to update QTableWidget which is on Different QWidget from MainWindow
    By sagar.mangalwedhekar in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2010, 07:07
  4. update() in QWidget
    By salmanmanekia in forum Qt Programming
    Replies: 5
    Last Post: 28th July 2008, 08:03
  5. QWidget::update() qeustion
    By tanki95 in forum Qt Programming
    Replies: 7
    Last Post: 14th March 2008, 02:29

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.