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. Change button color
QtWS25 Last Chance

Change button color

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt5.6
4 Posts 4 Posters 3.2k Views
  • 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.
  • N Offline
    N Offline
    neko lie
    wrote on last edited by
    #1

    How to change a button's color when a certain condition is met?

    Like initially the button is it's default color then when the condition is met, the button will turn to color red.

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

      Hi,

      What are you using ? QtWidgets ? QtQuick ?
      What kind of conditions do you have in mind ?

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

      1 Reply Last reply
      1
      • N neko lie

        How to change a button's color when a certain condition is met?

        Like initially the button is it's default color then when the condition is met, the button will turn to color red.

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #3

        @neko-lie
        I would suggest doing this via StyleSheets QWidgets have a varity of substates you can filter for and define a new style for. For Example QPushButtons have (among other things) the checked pressed and disabled state.

        If you want to expand that do other states that you define yourself, you can do that via sublcassing.
        Here is an example of a custom QFrame, that changes a state depending on the user pressing with the mouse on it or not.

        #include <QFrame>
        #include <QStyle>
        #include <QMouseEvent>
        
        class JhFrame : public QFrame
        {
            Q_OBJECT
            Q_PROPERTY(bool state READ isState WRITE changeState)
        
        public:
            explicit JhFrame(QWidget *parent = nullptr) : QFrame(parent){  }
            inline bool isState(){return state;}
        
        public slots:
            void changeState(const bool b){
                if(b != state){
                    state = b;
                    style()->unpolish(this);
                    style()->polish(this);
                    repaint();
                }
            }
        
        signals:
            void pressed();
            void clicked();
            void released();
        
        protected:
            void mousePressEvent(QMouseEvent *event)override{
                if(isEnabled()){
                    changeState(true);
        
                    emit pressed();
                }
                QFrame::mousePressEvent(event);
            }
        
            void mouseReleaseEvent(QMouseEvent *e)override {
                emit released();
                QFrame::mouseReleaseEvent(e);
        
                if(state && isEnabled()) {
                    changeState(false);
                    emit clicked();
                }
            }
        
            void mouseMoveEvent(QMouseEvent *e)override{
                if(state){
                    if(!rect().contains(e->pos())) {
                        changeState(false);
                    }
                }
            }
        
            void leaveEvent(QEvent *event) override{
                QFrame::leaveEvent(event);
                changeState(false);
            }
        
            bool state = false;
        };
        

        here a modified stylesheet to filter for state

        QFrame{background-color:white;}
        QFrame[state=true]{background-color:red;}
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        4
        • ValentinMicheletV Offline
          ValentinMicheletV Offline
          ValentinMichelet
          wrote on last edited by ValentinMichelet
          #4

          If the question is "How to change a button's color?" the answer is probably something like

           myButton->setStyleSheet("background-color: red;");
          

          If the question is how to "How to change the button's color WHEN some conditions" the answer could be

          [Somewhere inside a method]
            if (conditionsMet) {
              Q_EMIT(ChangeButtonColor());
            }
          
          [somewhere in constructor, most likely]
            connect(this, &MyClass::ChangeButtonColor, this, [m_button](){ m_button->setStyleSheet("background-color: red;"); });
          

          This is one way to connect signal/slot to actually change the button's color when some conditions are met. But the condition can be in another class than the one owning the button, the signal can be emitted by another component and not inside a method, and the code that changes the color can be in a method instead of a lambda.

          What might be useful would to copy/paste some code to help you more specifically.

          EDIT:
          @J.Hilk 's solution, tho a bit complicated for a beginner, is much more robust and pro. It depends on how serious your project is and if you have sufficient knowledge in Qt.

          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