Change QPushButton Icon on hover and pressed

后端 未结 5 1367
攒了一身酷
攒了一身酷 2021-02-10 02:34

I\'m trying to change the Icon of a QpushButton on hover and pressed, I\'m using QtDesigner with stylesheets. I tried this

QpushButton{
       qproperty-icon:ur         


        
5条回答
  •  故里飘歌
    2021-02-10 03:23

    After reading this article and encountering similar issues. This is my work around in c++ not using style sheet from designer.

    1>I create Icons one for being pressed and one for normal. In your case we would address it as the hover condition.

    2>Add the icons to the resource file.

    3>Use the following code for reference...

    Where Add_PB is a QPushButton.

    Add_PB->setStyleSheet( "*{border-image: url(:/icons/maximize.bmp);}"  
    ":pressed{ border-image: url(:/icons/maximize_pressed.bmp);}"); 
    

    The key take away here is you can use setStyleSheet to set diffrent icons for different conditons. I couldnt get the above code to work until I used the * operator or "Universal Selector" in the CSS string.

    Reference: http://doc.qt.io/qt-5/stylesheet-syntax.html

提交回复
热议问题