Please nominate your Qt Champions for 2021! https://forum.qt.io/topic/132134/looking-for-the-2021-qt-champions

Change QPushButton opacity on hover and pressed states.



  • Well the title it's self explanatory. I am trying to achieve those results for buttons that are not created with the designer. They are generated via code.
    That behavior is most likely achievable true QSS - therefor how to set the style sheet most efficiently.
    What I am thinking of is setStyleSheet(blablalba) in the constructor - is there any way better than that?
    Can someone better than me with QSS (obviously) help me with the syntax ?
    Cheers.



  • as you suggested using setStyleSheet(blablalba) in the constructor is totally fine



  • @gde23 Any help with the states syntax ?



  • e.g.

    QPushButton:
    { 
        background: rgba(255, 0, 125, 125);
    }
    QPushButton:hover 
    {
        background: rgba(0, 0, 0, 125);
    }
    

    or in general take a look at the examples:
    https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-a-qpushbutton-using-the-box-model



  • @gde23 Thank you!
    Your comment led me to results. Thank you !

    QPushButton
    {
    
    	background-color: rgba(0, 172, 252, 30);
    
    	border:0px solid rgb(0,175,252);
    	border-style: outset;
    	border-width: 1px;
    	margin-bottom: 1px;
    }
    QPushButton:hover
    {
    	background-color: rgba(0, 172, 252, 50);
    }
    
    QPushButton:pressed
    {  
    	background-color: rgba(0, 120, 252, 150);
        border-style: inset;
        border-width: 2px;
    
    }
    
    

    During the process I've learned that you can also set border-image for each state of the button. So any rater complex design can be achieved true Photoshop as QSS supports .png with alpha.

    One more thing I am concerned right now and it's if I am able to support custom button shapes let's say I'll want my button to appear as an image of a house, but only be able to click inside the image area.
    Like here - Image


Log in to reply