Solved setstylesheet makes custom QPushButton text invisible
-
In fact, this is what I think, the text color might have returned to invisible or smth. that makes it invisible when I clicked to my custom QPushButton, and the code inside mouseReleaseEvent run (Also text may be cleared out and this can be why it is unseen) here is my code:
this is how I crate them in mainwindow.cpp: (in this code there are 2 mypushbutton s, one of them is crated via form and promoted to mypushbutton, the other is created programmatically at the end of constructor of mainwindow. The result is the same for both)
//inside constructor of mainwindow //colorize myPushButtons ui->startButton->SetColor(QColor(132,204,232,255)); ui->startButton->setText("START"); myPushButton *pNewtestbutton = new myPushButton("TESTBUTTON",this); pNewtestbutton->setText("TestText"); pNewtestbutton->SetColor(QColor(132,204,232,255));
this is implementation pf class methods
myPushButton::myPushButton(QWidget *parent) : QPushButton(parent), m_colorAnimation(this, "color") { qDebug()<<"!!!!!!!myPushButton constructor1 called.."; // installEventFilter(this); } myPushButton::myPushButton(const QString &text, QWidget *parent) : QPushButton(text, parent) { qDebug()<<"!!!!!!!myPushButton constructor2 called.."; } void myPushButton::mouseReleaseEvent(QMouseEvent *mEv) { qDebug()<<"!!!!!!!myPushButton mouserelease called.."; QString myReleasedColorStr = QString("rgba(%1, %2, %3, %4)").arg(m_currentColor.red()).arg(m_currentColor.green()).arg(m_currentColor.blue()).arg(255); QString styleSheetStr = QString("background-color: %1; \ color: rgb(255, 0, 0); \ border-style: outset; \ border-width: 1px; \ border-radius: 10px; \ border-color: beige;").arg(myReleasedColorStr); setStyleSheet(styleSheetStr); /* */ setText("HELLO"); } void myPushButton::paintEvent(QPaintEvent *event) { QPushButton::paintEvent(event);//I both tried with this line commented and uncommented, the result was the same qDebug()<<"!!!!!!!myPushButton paintevent called.."; QPushButton::paintEvent(event); QStyleOption styleopt; styleopt.init(this); QPainter painter(this); style()->drawPrimitive(QStyle::PE_Widget, &styleopt, &painter, this); } void myPushButton::SetColor(const QColor& color) { qDebug()<<"!!!!!!!myPushButton setcolor called.."; m_currentColor = color; }
and here is my inherited class
#include <QPushButton> #include <QColor> #include <QPaintEvent> #include <QPropertyAnimation> class myPushButton : public QPushButton { Q_OBJECT Q_PROPERTY(QColor color READ GetColor WRITE SetColor) public: myPushButton(QWidget *parent = nullptr); myPushButton(const QString &text, QWidget *parent = nullptr); ~myPushButton(); void SetColor(const QColor &color); QColor myBackgroundColor; QPalette myPalette; protected: bool eventFilter(QObject *qObj, QEvent *qEv); void paintEvent(QPaintEvent *);// override; private: QColor m_currentColor; QPropertyAnimation m_colorAnimation; void mousePressEvent(QMouseEvent *mEv);// override; void mouseReleaseEvent(QMouseEvent *mEv);// override; const QColor& GetColor(void); // void StartHoverEnterAnimation(void); // void StartHoverLeaveAnimation(void); };
The button is shown as such before and after I click mouse over it.
-
Hi,
@brsr said in setstylesheet makes custom QPushButton text invisible:
style()->drawPrimitive(QStyle::PE_Widget, &styleopt, &painter, this);
Why are you overpainting a widget primitive element ?
-
You can try to reset style sheet to change the background color of the button, but not foreground color
pNewtestbutton->setStyleSheet("QPushButton { background-color: yellow }"); //on
pNewtestbutton->setStyleSheet("QPushButton { background-color: red }"); //off -
@JoeCFD Thx Joe for your reply but I don't want to reset stylesheet. Even I don't have a problem for setting the background color
-
@SGaist Hello, thx for your reply. Yes you are right when I commented out that line, it worked! Indeed, it was recommended by a website that for custom QPushButtons this block of code is necessary to make stylesheets work for custom QPushButtons. I tried many recommendations by searching over the internet, and stupidly, this piece of code has just remained there by accepting the suggestion that this was necessary for custom QPushButtons.
Thanks for your reply, you saved my day !