Results 1 to 13 of 13

Thread: Speeding up QPaint

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2006
    Location
    Australia
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    11
    Thanked 4 Times in 4 Posts

    Default Speeding up QPaint

    I think my computer swallowed my last posting of the same info... not sure tho...sorry if this is a double post

    Still trying to make my nice array of 100 pretty round buttons even nicer

    due to the fact that I often have to update the state of about 25-30 buttons at a time, the repaints that I am calling are kinda slow....

    I thought I could maybe speed up the repaints by not calling
    Qt Code:
    1. QPainter painter (this);
    To copy to clipboard, switch view to plain text mode 
    every single repaint, but instead creating the painter on initialisation and having a QPainter* as a member of the RoundButton class - but there are a couple of qns
    1. Will this actually make it that much faster?
    2. When I try to do this, it says that a painter can only be created for a QWidget (RoundButton is a derivative of QPushButton) during a paint event, so i can't initialize it during construction

    anybody tried anything similar? any success? am I barking up the wrong tree completely?

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

    Default Re: Speeding up QPaint

    Quote Originally Posted by georgie
    1. Will this actually make it that much faster?
    No.

    2. When I try to do this, it says that a painter can only be created for a QWidget (RoundButton is a derivative of QPushButton) during a paint event, so i can't initialize it during construction
    By default, you can't paint on widgets outside paint events in Qt4. You can pass a flag to a widget that you want to paint on it outside the paint event, but it won't help you much here.

    am I barking up the wrong tree completely?
    Yes, I think so. You'd better focus on optimising the paint event itself. Use pixmaps, redraw only the part of the widget which needs repainting, use faster routines, etc.

  3. #3
    Join Date
    May 2006
    Location
    Australia
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    11
    Thanked 4 Times in 4 Posts

    Default Re: Speeding up QPaint

    bummer, cause the only reason i ever need to redraw is to change the colour completely, so repainting only a little won't help me much
    and it is already a very simple routine which only does the bare minimum so i doubt i can make it much faster



    it's just because i always have to do so many at once that it's a prob....oh well....

    the only thing i can think of then, is instead of creating a new gradient every time i could draw from one of 3 predefined pixmaps (i want 3 diff colours)....but i don't think this will help either because even when i paint flat colours you can't notice any real speed increase over the gradients....

    thanks anyway

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

    Default Re: Speeding up QPaint

    But do you always need to redraw the whole widget? Could you show me your paintEvent routine?

  5. #5
    Join Date
    May 2006
    Location
    Australia
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    11
    Thanked 4 Times in 4 Posts

    Default Re: Speeding up QPaint

    for a wonderfully wacky and convoluted reason, the attached pic is what my buttons need to look like...the yellow is the selected, the definition of what a "hex" is follows the yellow one - i.e. if i select a diff button, the diff group of buttons becomes the hex centre so the extra circles get removed from one set of buttons and drawn on a diff set


    Qt Code:
    1. void RoundButton::paintEvent(QPaintEvent* event)
    2. {
    3. QRadialGradient grad(15, 15, 35, 0, 0);
    4. QPainter painter(this);
    5. grad.setColorAt(0.1, Qt::white);
    6. if(this->focus)
    7. grad.setColorAt(0.2, Qt::yellow); //the selected one is yellow
    8. if(this->arr->getHex()) //if the user has selected "display hex" the buttons get linked into
    9. //6's by another circle around the one in the centre of the hex
    10. {
    11. if(this->hexCentre)
    12. {
    13. grad.setColorAt(0.2, Qt::cyan);
    14. QPen pen(Qt::darkRed);
    15. painter.setPen(pen);
    16. painter.drawEllipse(0.5,0.5,29, 29);
    17. pen.setColor(Qt::black);
    18. painter.setPen(pen);
    19. }
    20. }
    21. grad.setColorAt(0.7, Qt::black);
    22.  
    23. QBrush brush(grad);
    24. painter.setBrush(brush);
    25. painter.drawEllipse(8,8,14,14);
    26. }
    To copy to clipboard, switch view to plain text mode 

    thanks for looking at this
    Attached Images Attached Images
    Last edited by georgie; 15th May 2006 at 11:58.

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

    Default Re: Speeding up QPaint

    Do you need all those buttons as separate buttons? Wouldn't it be easier to have a single widget which handles them all?

    I see your paint events shouldn't experience any slowdowns. The reason has to be somewhere else. The only thing you may improve here is not to generate the gradient every repaint -- make it a member of the button and modify it only when it changes.

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

    georgie (15th May 2006)

  8. #7
    Join Date
    May 2006
    Location
    Australia
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    11
    Thanked 4 Times in 4 Posts

    Default Re: Speeding up QPaint

    to a point, i guess it would be better....but there is quite a bit of associated "internal state" info which goes with each button.....which would get mighty confusing with one big widget :S

    thanks for your help though

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

    Default Re: Speeding up QPaint

    Quote Originally Posted by georgie
    to a point, i guess it would be better....but there is quite a bit of associated "internal state" info which goes with each button.....which would get mighty confusing with one big widget :S
    Why? You can keep states of each of the buttons in some data structure.

  10. #9
    Join Date
    May 2006
    Location
    Australia
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    11
    Thanked 4 Times in 4 Posts

    Default Re: Speeding up QPaint

    i never thought about it like that....how would you be able to register a click within a region - like if there was just one big widget but i wanted to click on button number 5, how would i know exactly where the user had clicked? I was using the mousePressEvent of each individual widget....

  11. #10
    Join Date
    May 2006
    Location
    Australia
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    11
    Thanked 4 Times in 4 Posts

    Default Re: Speeding up QPaint

    keeping the gradient as a "has a" member does speed stuff up quite a bit, but the first time i set the colour at 0.2, this cannot be changed in subsequent repaints (i.e. if i deselect something, it remains its selected colour....e.g. if it was the selected one, but then it becomes only the centre of a hex, instead of going from yellow to cyan it stays yellow for the remainder)

    there doesn't seem to be an "unsetColorAt()" function, so i think it is necessary to keep initializing a new grad....which is a bummer cause it made it so much better

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

    Default Re: Speeding up QPaint

    Quote Originally Posted by georgie
    i never thought about it like that....how would you be able to register a click within a region - like if there was just one big widget but i wanted to click on button number 5, how would i know exactly where the user had clicked? I was using the mousePressEvent of each individual widget....
    Qt Code:
    1. void MyWidget::mousePressEvent(QMouseEvent *e){
    2. QPoint clicked = e->pos();
    3. // now calculate which button was clicked (if any)
    4. // for example (simplified problem):
    5. // X X X X X
    6. // X X X X
    7. // X X X X X
    8. // X X X X
    9. // each X is 20x20px
    10. // row:
    11. int row = clicked.y()/20;
    12. // col:
    13. int col = clicked.x()/20;
    14. // check if position occupied and ignore otherwise:
    15. if(row % 2 && !(col%2)) return; // odd numbered row and even numbered column (starting from 0)
    16. if(!(row%2) && col%2) return; // even numbered row and odd numbered column
    17. // now it should be easy to map [row x column] to a button, for example:
    18. QPoint buttoncoords(row/2, col/2);
    19. emit buttonClicked(buttoncoords);
    20. }
    To copy to clipboard, switch view to plain text mode 

    keeping the gradient as a "has a" member does speed stuff up quite a bit, but the first time i set the colour at 0.2, this cannot be changed in subsequent repaints (i.e. if i deselect something, it remains its selected colour....e.g. if it was the selected one, but then it becomes only the centre of a hex, instead of going from yellow to cyan it stays yellow for the remainder)

    there doesn't seem to be an "unsetColorAt()" function, so i think it is necessary to keep initializing a new grad....which is a bummer cause it made it so much better
    So create a new one if it changes. Most of the time it won't change.

    Qt Code:
    1. void MyButton::paintEvent(QPaintEvent *e){
    2. if(buttonStateChanged){
    3. grad = QRadialGradient (15, 15, 35, 0, 0);
    4. grad.setColorAt(0.1, Qt::white);
    5. grad.setColorAt(0.2, ...);
    6. grad.setColorAt(..., ...);
    7. }
    8. QPainter p(this);
    9. drawButton(&p);
    10. }
    To copy to clipboard, switch view to plain text mode 

    or even predefine gradients:
    Qt Code:
    1. class MyButton : public ... {
    2. //...
    3. private:
    4. static QRadialGradient normalGradient;
    5. static QRadialGradient selectedGradient;
    6. static QRadialGradient someotherGradient;
    7. //...
    8. };
    9. //...
    10. QRadialGradient MyButton::normalGradient = QRadialGradient(15, 15, 35, 0, 0);
    11. //...
    To copy to clipboard, switch view to plain text mode 

  13. #12
    Join Date
    May 2006
    Location
    Australia
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    11
    Thanked 4 Times in 4 Posts

    Default Re: Speeding up QPaint

    i only ever call repaint if it changes, so this will make no difference....
    *BUT* I will definitely try your last idea....it sounds like it will do the trick....thankyou for your help

Similar Threads

  1. QTDesigner + QPaint [QT 4.4]
    By pippofrank in forum Qt Programming
    Replies: 10
    Last Post: 13th December 2008, 18:45
  2. QPaint Class and QGraphicsScene ....I am stuck !!
    By salmanmanekia in forum Qt Programming
    Replies: 7
    Last Post: 30th May 2008, 16:04
  3. Adding qpaint object to a Layout
    By Rooster in forum Newbie
    Replies: 6
    Last Post: 13th February 2008, 14:12
  4. Adding numbers to circles in QPaint
    By therealjag in forum Qt Programming
    Replies: 1
    Last Post: 12th February 2006, 11:21

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.