MarcinG
MarcinG

Reputation: 840

QProgressBar with StyleSheet

I would like to have a vertical progressbar with vertical (top-to-bottom) text inside it. I wonder if and how can this be achieved with style sheets. I can`t change style of the whole application or completely change it for the widget ( no "apply plastic style solution available). Could someone provide me with some example? If you know any other way to achieve this it will be helpful as well.

Upvotes: 0

Views: 1103

Answers (2)

Marek R
Marek R

Reputation: 38181

Proposed answer is to complicated. It can be done in much more simple way (less code).

First of all you should subclass QProxyStyle. This should be done more or less like that (override drawControl):

class MyProxyStyle : public QProxyStyle
{
  public:
    void QStyle::drawControl(ControlElement element, const QStyleOption * option, QPainter * painter, const QWidget * widget = 0) const
    {
        if (element == CE_ProgressBarLabel) {
            // create coppy of style option:
            QStyleOptionProgressBar op(*static_cast<QStyleOptionProgressBar*>(option));

            // prepare style option for rotation
            op.rect = QTransform().rotate(-90).mapRect(op.rect);
            // optional: op.orientation = (op.orientation==Qt::Horizontal)?Qt::Vertical:Qt::Horizontal;

            painter->rotate(90);  // rotate painter 
            QProxyStyle::drawControl(element, &op, painter, widget);
            painter->rotate(-90); // restore painter state - its a must

            return;
        }
        QProxyStyle::drawControl(element, option, painter, widget);
    }
};

It is possible that I've messed up with angles, but general concept should be clear.

Note that this is much better approach since:

  • code is simple
  • you do not break style, if style will be changed all should look an fill right (you are not using hard coded painting of label).

Upvotes: 1

Martin
Martin

Reputation: 930

I think the best way to do it is just to subclass QProgressBar, not using stylesheet.

You have a similar example here with QScrollbar, hope it will help ;)

Draw text on scrollbar

A small parenthesis, if you want to apply a stylesheet to only one component, you have to write something like this:

widgetType#widgetName
{
    ...
}

Upvotes: 1

Related Questions