QRadioButton color change on Selected and deselected Qt

蹲街弑〆低调 提交于 2020-07-16 03:00:12

问题


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 could I change it color and without loading Image and change border color or styling radiobutton

the requirement is attached in the Image :

RadioButtonRequiredImage


回答1:


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;
}



回答2:


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.




回答3:


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");
        }
    }


来源:https://stackoverflow.com/questions/38714925/qradiobutton-color-change-on-selected-and-deselected-qt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!