Results 1 to 9 of 9

Thread: Two different methods of using QBrush with QPainter - why is only one working?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Two different methods of using QBrush with QPainter - why is only one working?

    To add to that -- if you keep the brush as a member variable, then setting the brush on the painter with setBrush() does not increase memory use -- you have two "handles" to the brush but in fact only one instance of brush data. This works as long as you don't modify any of the "handles". If you do then a copy takes place so that each brush has its own instance of data. But since you modify the brush to apply the changes right away, you end up with a single instance of data again as the old data is deleted by QBrush. Same works for QPen, of course.
    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.


  2. #2
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Two different methods of using QBrush with QPainter - why is only one working?

    Thank you both for your valuable answers!

    Greetings,
    Markus

  3. #3
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Two different methods of using QBrush with QPainter - why is only one working?

    Hello again!

    As I'm advancing with my little widget, i just got another problem, which is also related with widget painting, so i decided to append my new problem right here in order to not flood the forum.

    I have subclassed my ProgressPie widget to create a timer-controlled progress pie. You give it a range and it counts down or up its limit values. I did this by implementing QObject::startTimer() and the timerEvent() method.

    I simply wanted to use my setValue() method from the parent widget which already includes a request to redraw itself after a value change:
    Qt Code:
    1. repaint(this->rect());
    To copy to clipboard, switch view to plain text mode 

    But when I call the function from the timerEvent() subclassed widged, nothing happens - unless i resize the window, which will trigger the resizeEvent (which is also implemented in the parent's code and which contains just the same line of code).

    When i put the line directly into my timerEvent() method of the subclass, the widget will repaint itself properly every time the timerEvent occurs.
    So - why's that?

    This is the whole code of both functions:

    The setValue() method of the parent class:

    Qt Code:
    1. void QtProgressPie::setValue(const int &val)
    2. {
    3. // Sets the given value which is checked against its bounds
    4. if (val != _value)
    5. {
    6. _value = qBound(_minimum, val, _maximum);
    7. repaint(this->rect());
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 


    The timerEvent() of the subclass:

    Qt Code:
    1. void QtTimedProgressPie::timerEvent(QTimerEvent *e)
    2. {
    3. // Modifies the current value of the timed ProgressPie.
    4. // By calling setValue of QtProgressPie, the widget will also be approprietely redrawn. // <--- No, it will not! :-(
    5. // Also, if the minimum or maximum value is arrived, the timerEvent will kill the timer
    6. // and emit the finished()-signal.
    7.  
    8. if (_countBackwards) // First case: counting towards _minimum
    9. {
    10. this->setValue(--_value); // dec and set value
    11. if (_value == _minimum) // check if value is now equal to the minimum
    12. { // if yes, kill timer and emit signal
    13. _timerIsRunning = false;
    14. killTimer(e->timerId());
    15. emit finished();
    16. }
    17. }
    18. else // Second case: counting towards _maximum
    19. {
    20. this->setValue(++_value); // inc and set value
    21. if (_value == _maximum) // check if value is now equal to the maximum
    22. { // if yes, kill timer and emit signal
    23. _timerIsRunning = false;
    24. killTimer(e->timerId());
    25. emit finished();
    26. }
    27. }
    28. repaint(this->rect()); // <--- this seems to be needed, but why?
    29. }
    To copy to clipboard, switch view to plain text mode 

    EDIT: typo
    Last edited by EMKAH; 19th May 2013 at 02:28.

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

    Default Re: Two different methods of using QBrush with QPainter - why is only one working?

    First of all use update() instead of repaint(). Second of all you don't have to pass rect() into the call as by default the widget updates its whole rect. Third of all the call that you mark as needed is not needed.
    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.


  5. #5
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Two different methods of using QBrush with QPainter - why is only one working?

    Thanks for your advice. I adapted the code according to your remarks, but I still need the call inside the timerEvent() method, otherwise the widget won't get updated.


    EDIT: i finally got it. Thie problem was not the update() call but the way I called the setValue()-method.

    I tried to call setValue() with ++_value or --_value. This of course manipulates _value directly before calling the method with the same new _value.

    But my setValue() method begins with a check if the given integer parameter is different to the actual _value (because I want to emit a valueChanged() signal from that method). Of course, this will never be true, because of the direct manipulation of _value with ++ and --.

    Anyway, thanks for you help!
    Last edited by EMKAH; 19th May 2013 at 15:21. Reason: found the solution by myself

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


Similar Threads

  1. QBrush pattern problem
    By ilovethisgame in forum Qt Programming
    Replies: 2
    Last Post: 4th October 2012, 22:07
  2. QPainter.drawImage() not working with resource image file
    By thiagoalencar22 in forum Qt Programming
    Replies: 4
    Last Post: 22nd April 2010, 21:07
  3. matrix for QBrush
    By navi1084 in forum Qt Programming
    Replies: 5
    Last Post: 5th February 2010, 11:27
  4. Is there any API to set QBrush instance as Cosmetic
    By navi1084 in forum Qt Programming
    Replies: 6
    Last Post: 3rd February 2010, 03:47
  5. QBrush pixmap tiling
    By rbp in forum Qt Programming
    Replies: 6
    Last Post: 19th June 2008, 12:54

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.