Results 1 to 10 of 10

Thread: Multiple inheritance of QGraphicsView and QGraphicsItem

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

    Default Multiple inheritance of QGraphicsView and QGraphicsItem

    I'm writing a program that contains a table of buttons, each button has the ability to blink, and if it's clicked, all buttons underneath the clicked button (in the same column) should be moved down.

    Initially, I was able to do this if all buttons of one column are in the same QGraphicsView. However one blinking button requires re-painting of the whole view, which results in taking up ~17% CPU. So I changed my code to put one button in a QGraphicsView; then put all the ButtonViews in another QGraphicsView (i.e. the button itself is a graphics item for animation as well as a view). Now one blinking button only takes up 9% CPU, but the animation no longer works.

    I'm guessing there must be something wrong with my design, and hoping the Qt experts could help me with a better one.

    I've attached the code here. I use Qt 4.5.3 under Unix/X11
    Attached Files Attached Files
    Last edited by cookie1909; 4th May 2009 at 18:17. Reason: removed inline code

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

    Default Re: Multiple inheritance of QGraphicsView and QGraphicsItem

    You should surely not inherit from both a widget and a graphics item. In general I think your concept is wrong. You should group items in parent-child relationships instead of creating hierarchies of views. If you want to add widgets to a graphics scene, use QGraphicsScene::addWidget() which uses QGraphicsProxyWidget class.

    Please use the attachment feature of the forum instead of inlining large amounts of code into posts.
    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 (4th May 2009)

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

    Default Re: Multiple inheritance of QGraphicsView and QGraphicsItem

    I changed my code like you suggested to use QGraphicsProxyWidget. However the performance of the blinking button got a little worse.

    Attached is a screenshot (buttons.jpg) of what I'm trying to do: a table of QGraphicsProxyWidgets (acting like buttons) and each button can be animated up and down.

    Case 1: I have 4 views (1 view for each column), each view contains 8 proxy widgets, only the first button is blinking ==> my CPU consumption is about 17%

    Case 2: I have 1 big view, which contains 32 small views (1 view for each proxy widgets, and the view itself is a proxy widget), one blinking button ==> my CPU consumption goes up to 25%

    Do I understand this correctly: one blinking button will cause the parent view to be updated, and since this view belongs to another parent view, this makes the big view to be re-painted for every blink?

    If so, is there any way to improve the performance, i.e. avoiding repainting everything but only the blinking button?

    Thanks a lot for helping.
    Attached Images Attached Images
    Attached Files Attached Files

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

    Default Re: Multiple inheritance of QGraphicsView and QGraphicsItem

    You only need one view for everything. Only those items that change will be repainted. If you have buttons that look so custom, consider creating a custom item instead of using widget proxies. If you use stylesheets to get the custom look of the buttons, this is affecting your performance. With what you want to obtain you should have practically no cpu usage.
    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 (6th May 2009)

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

    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!

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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.


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

    cookie1909 (13th May 2009)

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

    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 

  11. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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.


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

    cookie1909 (14th May 2009)

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

    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

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

    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.