Results 1 to 9 of 9

Thread: PixelMetric for QProgressBar gutter/margin spacing?

  1. #1
    Join Date
    Dec 2011
    Posts
    11
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question PixelMetric for QProgressBar gutter/margin spacing?

    I have a subclass of QProgressBar that draws its own progress bar groove and contents by overriding paintEvent(), but I've noticed that the height of the bar is apparently supposed to change depending on the graphical theme in use at the time. For example, it looks fine when I test in Linux, but note the difference in height between the top bar and the bottom bars in this Windows Classic theme screenshot:



    The top is the native QProgressBar. I want to duplicate that appearance. How do I get the correct vertical "gutter" spacing for the active style so that I can draw the bar at the correct distance from the borders of the groove?

    EDIT: I think what I'm looking for is a PixelMetric from the active style, but I'm not sure which one. The only one that seems to be specific to progress bars is QStyle::PM_ProgressBarChunkWidth, but that's obviously not what I need. Any ideas?

    --Colin
    Attached Images Attached Images
    Last edited by cmb; 1st March 2014 at 20:08. Reason: updated contents

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: PixelMetric for QProgressBar gutter/margin spacing?

    All widgets call into the current QStyle's API to delegate each part of the drawing.
    My suggestion is to look at the code of QProgressBar and see which methods it calls with which arguments and then repeat all those that you want not to change.

    Cheers,
    _

  3. #3
    Join Date
    Dec 2011
    Posts
    11
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: PixelMetric for QProgressBar gutter/margin spacing?

    Quote Originally Posted by anda_skoa View Post
    All widgets call into the current QStyle's API to delegate each part of the drawing.
    My suggestion is to look at the code of QProgressBar and see which methods it calls with which arguments and then repeat all those that you want not to change.

    Cheers,
    _
    I'm still confused -- could you point me toward where I should be looking? I've only overridden paintEvent(), as shown below. My derived class allows both positive and negative values for the bar (with zero at the center), so I'm drawing the bar separately from the groove. The bar therefore needs to be sized differently from the groove, but the difference changes depending on the style in use.

    I've looked in qprogressbar.cpp, but all I saw in paintEvent() was that it calls drawControl() once to handle the groove and the bar.

    Qt Code:
    1. /**
    2.  * Draws a progress bar that extends left or right from the center point.
    3.  */
    4. void FuelTrimBar::paintEvent(QPaintEvent *)
    5. {
    6. int currentVal = this->value();
    7. QStylePainter painter(this);
    8. bar.initFrom(this);
    9. bar.minimum = m_minimumVal;
    10. bar.maximum = m_maximumVal;
    11. bar.progress = (currentVal >= 0) ? qAbs(m_maximumVal) : qAbs(m_minimumVal);
    12.  
    13. style()->drawControl(QStyle::CE_ProgressBarGroove, &bar, &painter, this);
    14.  
    15. // compute the dimensions and location of the bar
    16. float percentOfWidth = (float)(qAbs(currentVal)) / (float)(m_maximumVal - m_minimumVal);
    17. int left = bar.rect.topLeft().x();
    18. int right = bar.rect.topRight().x();
    19. int top = bar.rect.topLeft().y();
    20. int height = bar.rect.bottomLeft().y() - top + 1;
    21. int barWidth = (right - left) * percentOfWidth;
    22. int midPoint = left + ((right - left) / 2);
    23. int startPoint = (currentVal >= 0) ? midPoint : midPoint - barWidth;
    24.  
    25. bar.rect = QRect(startPoint, top, barWidth + 2, height);
    26. style()->drawControl(QStyle::CE_ProgressBarContents, &bar, &painter, this);
    27. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: PixelMetric for QProgressBar gutter/margin spacing?

    Hmm. Looks like you could reuse most of the code, no?
    Just doing the progressbar contents differently.

    Cheers,
    _

  5. #5
    Join Date
    Dec 2011
    Posts
    11
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: PixelMetric for QProgressBar gutter/margin spacing?

    Quote Originally Posted by anda_skoa View Post
    Hmm. Looks like you could reuse most of the code, no?
    Just doing the progressbar contents differently.

    Cheers,
    _
    I completely agree. However, the height of the CE_ProgressBarContents cannot simply be the same as the height of the groove. If I make them the same height, I end up with the progress bar that I posted in the first screenshot. CE_ProgressBarContents must have a smaller vertical dimension, but this is sensitive to the active style. That's why I was looking for a PixelMetric that described the difference in height.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: PixelMetric for QProgressBar gutter/margin spacing?

    What I mean is this:

    the code of QProgressBar draws the groove, then calculates the rectange for the bar contents.

    Can't you use the exact same code? At least for height? Just changing startPoint and barWidth?

    Cheers,
    _

  7. #7
    Join Date
    Dec 2011
    Posts
    11
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: PixelMetric for QProgressBar gutter/margin spacing?

    The code that I showed above is not in QProgressBar, it is in my subclass. The QProgressBar code looks like this:

    Qt Code:
    1. void QProgressBar::paintEvent(QPaintEvent *)
    2. {
    3. QStylePainter paint(this);
    4. initStyleOption(&opt);
    5. paint.drawControl(QStyle::CE_ProgressBar, opt);
    6. d_func()->lastPaintedValue = d_func()->value;
    7. }
    To copy to clipboard, switch view to plain text mode 

    As you can see, it just depends on the style to draw the entire control, which happens like this:

    Qt Code:
    1. case CE_ProgressBar:
    2. = qstyleoption_cast<const QStyleOptionProgressBar *>(opt)) {
    3. subopt.rect = subElementRect(SE_ProgressBarGroove, pb, widget);
    4. proxy()->drawControl(CE_ProgressBarGroove, &subopt, p, widget);
    5. subopt.rect = subElementRect(SE_ProgressBarContents, pb, widget);
    6. proxy()->drawControl(CE_ProgressBarContents, &subopt, p, widget);
    7. if (pb->textVisible) {
    8. subopt.rect = subElementRect(SE_ProgressBarLabel, pb, widget);
    9. proxy()->drawControl(CE_ProgressBarLabel, &subopt, p, widget);
    10. }
    11. }
    12. break;
    To copy to clipboard, switch view to plain text mode 

    It doesn't change the rectangle for the progress bar contents -- unless that's done somewhere in one of the QStyle subclasses. Still not sure what I should be looking for.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: PixelMetric for QProgressBar gutter/margin spacing?

    Ah, I see, sorry.

    Hmm. Doesn't this line calculate the content rectangle?
    Qt Code:
    1. subopt.rect = subElementRect(SE_ProgressBarContents, pb, widget);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

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

    cmb (2nd March 2014)

  10. #9
    Join Date
    Dec 2011
    Posts
    11
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: PixelMetric for QProgressBar gutter/margin spacing?

    Quote Originally Posted by anda_skoa View Post
    Ah, I see, sorry.

    Hmm. Doesn't this line calculate the content rectangle?
    Qt Code:
    1. subopt.rect = subElementRect(SE_ProgressBarContents, pb, widget);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _
    Yes! That was it. Thank you for pointing that out; it was right in front of me but I didn't see it.

    I now call subElementRect() for the progress bar contents before adjust the start and end point, and the bar is draw with the correct height for the style in use.

Similar Threads

  1. Replies: 1
    Last Post: 11th July 2012, 12:47
  2. drawing triangle with gradient based on vertices
    By marc2050 in forum Qt Programming
    Replies: 5
    Last Post: 4th August 2011, 12:45
  3. Replies: 3
    Last Post: 11th December 2009, 17:13
  4. QprogressBar Style Sheets: double linear gradient
    By Beppe in forum Qt Programming
    Replies: 6
    Last Post: 8th May 2009, 14:41
  5. Custom Style based on Cleanlooks
    By Tux-Slack in forum Qt Programming
    Replies: 4
    Last Post: 21st September 2007, 10:22

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.