QRadioButton color change on Selected and deselected Qt

后端 未结 3 776
-上瘾入骨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");
            }
        }
    

提交回复
热议问题