Qt World Summit: Submit your Presentation

QPushButton image with states.

  • Hello there, I was wandering if is it possible to have a pressed state in QPushButton. I have a QPushButton with a png as drawable and, when I click on it I want to change its background for 500ms and then restore it. Any idea?


    I found it:

    @ QPushButton#evilButton:pressed {
    background-color: rgb(224, 0, 0);
    border-style: inset;

    can this be done programmatically?

    I tried with this stylesheet:
    QPushButton { background: transparent; border: none;} QPushButton:pressed { background-image: url(\mypath\settings_press.png); }
    but i get this error:

    Could not parse stylesheet of widget


    EDIT 3: the issue is related to the slashes direction:
    Changing in this way works, but still i can not see any image.
    QPushButton { background-image: url(/mypath/settings.png);} QPushButton:pressed { background-image: url(/mypath/settings_press.png); }

    Any idea?

  • i think you can subclass QPushButton and reimplement paint function.
    state can be acquire from isChecked(), isDown(),..

    and,,, QTimer can be used for background to be restored after a while.

Log in to reply