QRadioButton color change on Selected and deselected Qt

后端 未结 3 738
-上瘾入骨i
-上瘾入骨i 2021-01-20 12:09

i am trying to change the color of radiobutton on selected and deselected as QtStylesheet :Qt Stylesheet

but In this link it only refer to Loading a Image but how cou

相关标签:
3条回答
  • 2021-01-20 12:25

    If you want to change the background color of your radiobutton when he's selected you should use both slots and stylesheet.

    I will name your button MyButton.

    In your .h you will find :

      private :
            QRadioButton MyButton;
        private slots:
            void changeColorMyButton();
    

    and in your .cpp add in the setup of your Mainwindow :

    QObject::connect(MyButton,SIGNAL(clicked(bool)),this,SLOT(changeColorMyButton));
    

    Your button is now connected to the signal clicked and when you will click on your button, the slot changeColorMyButton will be executed. You can now customize your slot.

       void Mainwindow::changeColorMyButton()
        {
            if(this.MyButton.isChecked())
            {
             this.MyButton->setStyleSheet("background-color: black");
            }
            else
            {   
             this.MyButton->setStyleSheet("background-color: yellow");
            }
        }
    
    0 讨论(0)
  • 2021-01-20 12:40

    Setting style sheet to next works for me:

    QRadioButton:checked{
        background-color: red;
    }
    
    QRadioButton:unchecked{
       background-color: black;
    }
    

    Setting style sheet to QRadioButton::indicator:checked doesn't work, because this only changes the settings of the indicator.

    0 讨论(0)
  • 2021-01-20 12:44

    Read documentation carefully. It describes all you need. It even almost described your case, the only difference is images instead of colours.

    Style sheet for your case is like this:

    QRadioButton {
        background-color:       gray;
        color:                  white;
    }
    
    QRadioButton::indicator {
        width:                  10px;
        height:                 10px;
        border-radius:          7px;
    }
    
    QRadioButton::indicator:checked {
        background-color:       red;
        border:                 2px solid white;
    }
    
    QRadioButton::indicator:unchecked {
        background-color:       black;
        border:                 2px solid white;
    }
    
    0 讨论(0)
提交回复
热议问题