How to use stylesheet colors in a custom QPushButton
-
Thanks for your answer.
To use 4 QLabels could help, I will give it a try.
Is there a posibility to read out the colors and other styles from a button to witch a stylesheet is asigned?QPushButton::setStyleSheet(QString("background-color: red ..."));
To set the styles is very easy. But how can I read back the background-color in hovered state for example?
QPushButton::getStyle(???); -
Hi,
Are you planning to let somebody change the style of that button ?
-
@SGaist
Yes, I want to change the colors für hover, focus, pressed with a style sheet. This would be the prefered solution. If I didn't get it work I catch the mouse events and draw the colors as wanted. Changes are a little bit more complicated and not an easy exchange of the stylesheet. Do you have another idea how to solve this problem?
@VRonin
I tried your proposal with the 4 Labels in a grid. It is the same behaviour. The text color of the label has always the color specified in QLabel { color: blue; ...}. The Pseudo-States :hover, :focus, :pressed are ignored. The background-color of the state :hover is correct. -
Thanks to all for your help.
I didn't get it solved with stylesheets, but I found a solution which works for me:class SettingsButton : public QPushButton { Q_OBJECT public: explicit SettingsButton(QWidget *parent = 0); void setText2(const QString &text1, const QString &text2); QString getText1(); QString getText2(); signals: public slots: protected: virtual void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE { QRect rect1(55,0,356,25); QRect rect2(55,25,356,25); QFont font; QPushButton::paintEvent(event); //To draw everything of a normal QPushButton m_color = QColor(Qt::white); //same as in stylesheet QPushButton (normal) if( QWidget::hasFocus() ) { m_color = QColor(Qt::red); //same as in stylesheet QPushButton:focus } if( QWidget::underMouse() ) { m_color = QColor(Qt::blue); //same as in stylesheet QPushButton:hover } if( QPushButton::isDown() ) { m_color = QColor(Qt::green); //same as in stylesheet QPushButton:pressed } //Display additional text QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing, true); painter.setPen(m_color); font = painter.font(); painter.setFont(QFont(font.family(), font.pointSize(), QFont::Bold)); painter.drawText(rect1, Qt::AlignLeft|Qt::AlignVCenter, firstText); painter.setFont(QFont(font.family(), font.pointSize(), QFont::Normal)); painter.drawText(rect2, Qt::AlignRight|Qt::AlignVCenter, secondText); } private: QString firstText; QString secondText; QColor m_color; };