Qt VerticalButton drawControl不绘制文本

时间:2017-10-09 19:28:07

标签: qt qpainter

我正在尝试创建一个垂直按钮。我遵循了12中提到的样式选项。但是我没有在按钮上看到任何可见的文字。

class VerticalPushButton : public QPushButton{
  public:
    VerticalPushButton(QWidget* parent=0x0);
    VerticalPushButton(const QString& text, QWidget* parent=0x0);
    VerticalPushButton(const QIcon& icon, const QString& text, QWidget* parent=0x0);
    virtual QSize sizeHint() const;
    virtual QSize minimumSizeHint() const;
  protected:
    virtual void paintEvent(QPaintEvent* event);
  private:
    QStyleOption styleOptions() const;
};
VerticalPushButton::VerticalPushButton(QWidget* parent): QPushButton(parent){
    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
}

VerticalPushButton::VerticalPushButton(const QString& text, QWidget* parent): QPushButton(text, parent){
    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
}

VerticalPushButton::VerticalPushButton(const QIcon& icon, const QString& text, QWidget* parent): QPushButton(icon, text, parent){
    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
}

QSize VerticalPushButton::sizeHint() const{
    QSize size = QPushButton::sizeHint();
    size.transpose();
    return size;
}

QSize VerticalPushButton::minimumSizeHint() const{
    QSize size = QPushButton::minimumSizeHint();
    size.transpose();
    return size;
}

void VerticalPushButton::paintEvent(QPaintEvent* event){
    QStylePainter painter(this);
    painter.rotate(90);
    painter.translate(0, -width());
    painter.drawControl(QStyle::CE_PushButton, styleOptions());
}

QStyleOption VerticalPushButton::styleOptions() const{
    QStyleOptionButton options;
    options.initFrom(this);
    QSize size = options.rect.size();
    size.transpose();
    options.rect.setSize(size);
    options.features = QStyleOptionButton::None;

    if(isFlat())                                              options.features |= QStyleOptionButton::Flat;
    if(menu())                                                options.features |= QStyleOptionButton::HasMenu;
    if(autoDefault() || isDefault())                          options.features |= QStyleOptionButton::AutoDefaultButton;
    if(isDefault())                                           options.features |= QStyleOptionButton::DefaultButton;
    if(isDown() || (menu() && menu()->isVisible()))           options.state    |= QStyle::State_Sunken;
    if(isChecked())                                           options.state    |= QStyle::State_On;
    if(!isFlat() && (!isDown()))                              options.state    |= QStyle::State_Raised;

    options.palette = palette();

    options.text = text();
    options.icon = icon();
    options.iconSize = iconSize();
    return options;
}

1 个答案:

答案 0 :(得分:0)

更改QStyleOption styleOptions() const;

中的QStyleOptionButton styleOptions() const ;