Results 1 to 9 of 9

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

Threaded View

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

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

    Hi everybody,

    I just managed to complete a long untouched work on a widget I want to use in one of my projects. It is a "circular progress bar", simply drawn with QPainter::drawPie().

    pie.png

    During development, I have learned that I cannot assign a pointer to a QBrush to QPainter::setBrush() and then change some values of the brush to use different painting styles (well, actually I can - but in this case, all the area of the ellipse is painted black instead of only the pie itself). Instead, I have to create an all-new QBrush everytime I want to change the drawing style.

    Here is some code to explain the problem:

    Qt Code:
    1. //[... somewhere in the widget's header ...]
    2.  
    3. QPen *_foreGround;
    4. QBrush *_backGround;
    5.  
    6. //[... somewhere in the widget's initialization process ...]
    7.  
    8. _foreGround = new QPen();
    9. _foreGround->setCosmetic(true);
    10. _foreGround->setWidth(3);
    11. _backGround = new QBrush();
    12. _backGround->setColor(QColor(qRgba(0,0,0,0)));
    13.  
    14. //[...]
    15.  
    16. void QtProgressPie::paintEvent(QPaintEvent *e)
    17. {
    18. // recalculate the value according to the conditions of QPainter::drawPie
    19. // step 1: subtract _minimum from _value (so the calculation base is always 0)
    20. // step 2: calculate the percentage of value (value/max * 100)
    21. // step 3: translate it to circular conditions (value/max * [100 * 3.6 * 16]) [...] = _FULLCIRCLE
    22. // step 4: take it times -1 (QPainter::drawPie works counter-clockwise with positive values)
    23.  
    24. int _painterValue = ((_value - _minimum) / double(_maximum - _minimum) * _FULLCIRCLE) * -1;
    25.  
    26. p.begin(this); // Begin drawing on the widget
    27.  
    28. // make sure that the widget appears always as a circle, not as an ellipse
    29. int _squaredSize = qMin(this->width(), this->height());
    30. p.setViewport((this->width() - _squaredSize) / 2,(this->height() - _squaredSize) / 2,
    31. _squaredSize, _squaredSize);
    32. // subtract the width of the pen ("the border width") from the widget's rect() to get the drawable area
    33. int _borderWidth = _foreGround->width();
    34. QRect drawableArea(0 + _borderWidth, 0 + _borderWidth,
    35. this->width() - _borderWidth * 2, this->height() - _borderWidth * 2);
    36.  
    37.  
    38. p.setRenderHint(QPainter::HighQualityAntialiasing, true);
    39. p.setPen(*_foreGround);
    40. //p.setBrush(*_backGround); <--- DOESN'T WORK
    41.  
    42. // draw surrounding circle
    43. // _backGround->setStyle(Qt::NoBrush); <--- DOESN'T WORK
    44. p.setBrush(QBrush(QColor(qRgba(0,0,0,0)),Qt::NoBrush)); // <--- WORKS!
    45. p.drawEllipse(drawableArea);
    46.  
    47.  
    48. // draw pie if value < 0
    49. if (_painterValue < 0)
    50. {
    51. //_backGround->setStyle(Qt::SolidPattern); <--- DOESN'T WORK
    52. p.setBrush(QBrush(QColor(qRgba(0,0,0,0)),Qt::SolidPattern)); // <--- WORKS!
    53. p.drawPie(drawableArea, _OFFSET, _painterValue);
    54. // _OFFSET = 1/4 of _FULLCIRCLE (transition from 3 o'clock (drawPie's base) to 12 o'clock)
    55. }
    56.  
    57. p.end(); // Drawing finished
    To copy to clipboard, switch view to plain text mode 

    So, my question is: why can't I use the "more elegant" solution with the pointer variable?

    Greetings,
    Markus

    EDIT: picture added, futher explanation of what goes wrong when using a pointer.
    Last edited by EMKAH; 14th May 2013 at 23:23.

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