Search code examples
qtbuttoniconsmousehover

change icon when mouse hover over push button in qt


I am trying to change the icon of a push button in qt when mouse hover over it. But i couldn't find any useful example about it. If anyone can help by posting a little example please. I've found examples on QEvent::MouseMove and hoverenter and hoverleave events concerning this subject but I didn't succed in using them


Solution

  • I think you can find those examples useful:

    1) Using QSS->

    QPushButton {
        border-image: url(:/icons/normal);
        background-repeat: no-repeat;
    }
    
    QPushButton:hover {
        border-image: url(:/icons/hover);
        background-repeat: no-repeat;
    }
    

    2) Update stylesheet from code->

    button->setStyleSheet( "*{border-image: url(:/icons/normal);} 
    :hover{ border-image: url(:/icons/hover);}"); 
    

    Don't forget to locate your images in the resources folder. Good luck :)