Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. setstylesheet makes custom QPushButton text invisible
Forum Updated to NodeBB v4.3 + New Features

setstylesheet makes custom QPushButton text invisible

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 691 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    brsr
    wrote on last edited by Chris Kawa
    #1

    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.
    button.jpg

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      B 1 Reply Last reply
      3
      • JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by JoeCFD
        #3

        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

        B 1 Reply Last reply
        0
        • JoeCFDJ JoeCFD

          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

          B Offline
          B Offline
          brsr
          wrote on last edited by
          #4

          @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

          1 Reply Last reply
          0
          • SGaistS SGaist

            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 ?

            B Offline
            B Offline
            brsr
            wrote on last edited by
            #5

            @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 !

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved