Results 1 to 10 of 10

Thread: Multiple inheritance of QGraphicsView and QGraphicsItem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2009
    Posts
    21
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    11

    Default Re: Multiple inheritance of QGraphicsView and QGraphicsItem

    I do not use any stylesheets, everything is being painted. In order to have animation, I need to use either QGraphicsItem or QGraphicsProxyWidget for my button. I choose proxy widget so that connect() function can be called to do blinking effect when its timer expires.

    I will try out putting all buttons in one view and hopefully the performance will be as good as you said

    Thanks again!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Multiple inheritance of QGraphicsView and QGraphicsItem

    Quote Originally Posted by cookie1909 View Post
    I choose proxy widget so that connect() function can be called to do blinking effect when its timer expires.
    It would be enough to inherit from both QGraphicsItem and QObject. Proxying through a widget just creates overhead.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    cookie1909 (13th May 2009)

  4. #3
    Join Date
    Apr 2009
    Posts
    21
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    11

    Default Re: Multiple inheritance of QGraphicsView and QGraphicsItem

    I'm back with my improved code!

    Like you suggested, I used one single view and have a table of QGraphicsItems within a scene. One blinking (every 0.5sec) button takes up about 10% CPU usage. Note that this is QtEmbedded I'm using, and we don't have a very powerful CPU, so 10% is acceptable.

    Now I put my view into my real program that contains a window with tab widgets and buttons, all static widgets. One blinking button now takes 60% CPU, if I increase the blinking timer to 1sec interval, it drops down to 30%, and of course 0% when there's no blinking.

    That tells me whenever the button blinks, more than just the button is being painted. I looked up the desc of QGraphicsItem::update()

    As a side effect of the item being repainted, other items that overlap the area rect may also be repainted.
    Can "other items that overlap" be the QGraphicsView that it belongs to, as well as the QWidget that the view belongs to? If so, it is redrawing a lot of unneccessary widgets.

    Here's how I implement my paint function and blink slot:

    Qt Code:
    1. QRectF LineButton::boundingRect() const
    2. {
    3. return QRectF(mPicX, mPicY, LINE_BUTTON_WIDTH, LINE_BUTTON_HEIGHT);
    4. }
    5.  
    6. void LineButton::paint(
    7. QPainter *painter,
    8. const QStyleOptionGraphicsItem *option,
    9. QWidget *)
    10. {
    11. painter->setClipRect(option->exposedRect);
    12. painter->drawPixmap(mPicX, mPicY, *mPic);
    13. }
    14.  
    15. void LineButton::blink()
    16. {
    17. if (mPic == mPixmapBlack)
    18. mPic = mPixmapYellow;
    19. else
    20. mPic = mPixmapBlack;
    21. QGraphicsItem::update(boundingRect());
    22. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Multiple inheritance of QGraphicsView and QGraphicsItem

    Quote Originally Posted by cookie1909 View Post
    Can "other items that overlap" be the QGraphicsView that it belongs to, as well as the QWidget that the view belongs to?
    No. Items means graphics items in the view (or scene actually). Remember that if you use an OpenGL viewport for your graphics view then the whole viewport will be redrawn.

    Here's how I implement my paint function and blink slot:

    Qt Code:
    1. QRectF LineButton::boundingRect() const
    2. {
    3. return QRectF(mPicX, mPicY, LINE_BUTTON_WIDTH, LINE_BUTTON_HEIGHT);
    4. }
    To copy to clipboard, switch view to plain text mode 
    This is probably wrong. I'd guess it should say QRectF(0,0,LINE_BUTTON_WIDTH, LINE_BUTTON_HEIGHT) and then you should use QGraphicsItem::setPos() to position the button on the scene.

    Qt Code:
    1. void LineButton::paint(
    2. QPainter *painter,
    3. const QStyleOptionGraphicsItem *option,
    4. QWidget *)
    5. {
    6. painter->setClipRect(option->exposedRect);
    7. painter->drawPixmap(mPicX, mPicY, *mPic);
    8. }
    To copy to clipboard, switch view to plain text mode 
    Don't clip the painter. It only slows you down. The architecture already sets up the clipper so you're doubling its work.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    cookie1909 (14th May 2009)

  7. #5
    Join Date
    Apr 2009
    Posts
    21
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    11

    Default Re: Multiple inheritance of QGraphicsView and QGraphicsItem

    I read the Graphics View Framework docs, updated the coordinates like below, but it didn't make any difference in CPU usage I'm running out of ideas of what else to try

    Qt Code:
    1. LineButton::LineButton(int x, int y)
    2. {
    3. setPos(x, y);
    4. mPixmapBlack = new QPixmap("black.png");
    5. mPixmapYellow = new QPixmap("yellow.png");
    6. mPic = mPixmapBlack;
    7. }
    8.  
    9. QRectF LineButton::boundingRect() const
    10. {
    11. return QRectF(0, 0, LINE_BUTTON_WIDTH, LINE_BUTTON_HEIGHT);
    12. }
    13.  
    14. void LineButton::paint(
    15. QPainter *painter,
    16. const QStyleOptionGraphicsItem *option,
    17. QWidget *)
    18. {
    19. painter->drawPixmap(0, 0, *mPic);
    20. }
    21.  
    22. void LineButton::blink()
    23. {
    24. if (mPic == mPixmapBlack)
    25. mPic = mPixmapYellow;
    26. else
    27. mPic = mPixmapBlack;
    28. QGraphicsItem::update(boundingRect());
    29. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  8. #6
    Join Date
    Apr 2009
    Posts
    21
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    11

    Default Re: Multiple inheritance of QGraphicsView and QGraphicsItem

    I finally found out what causes all of the repaintings!!!

    I loaded this test program onto a device with a touch screen that is smaller than the Qt Window size. Everytime the button blinks, turned out Qt tries to repaint that button, as well as all other widgets of the window that got cut off by the LCD.

    I fixed it so everything fits on the screen, one blinking button takes up 2% CPU, and all buttons blinking takes up 45% CPU, and am quite happy with that result.

    Thanks so much again for all the help!

Similar Threads

  1. QGraphicsView, QGraphicsItem, QGraphicsScene
    By Shuchi Agrawal in forum Newbie
    Replies: 10
    Last Post: 23rd March 2011, 20:50
  2. Replies: 5
    Last Post: 31st December 2010, 11:49
  3. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31
  4. Replies: 2
    Last Post: 12th February 2009, 09:53
  5. (QT4.2-RC1) QGraphicsScene QGraphicsView QGraphicsItem
    By antonio.r.tome in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 10:56

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
  •  
Qt is a trademark of The Qt Company.