0

Before, I was used to using button.clicked.connect(function) to trigger a button and setting qss by setStyleSheet() (Eg: QPushButton:pressed{background:#EAEAEA;}). And the button will change background color when pressing button by clicking mouse and work as expected. However, Currently I'm using QShortcut to trigger the button (Eg: QtCore.Qt.Key_Return) but the button seems not to be affected by qss.

The code that I've used:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        Form.setStyleSheet(""
                        "#Form {\n"
                        "background-color: rgb(255, 255, 255);\n"
                        "}\n"
                        "QPushButton:hover {\n"
                        "color: white;\n"
                        "background-color: rgb(255, 85, 127);\n"
                        "border-radius: 6px;\n"
                        "font-size:16px;\n"
                        "}\n"
                        "\n"
                        " QPushButton:pressed{\n"
                        "background:#EAEAEA;\n"
                        "color: black;\n"
                        "font-size:16px;\n"
                        "}")
        self.frame = QtWidgets.QFrame(Form)
        self.frame.setGeometry(QtCore.QRect(20, 20, 351, 241))
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.pushButton = QtWidgets.QPushButton(self.frame)
        self.pushButton.setGeometry(QtCore.QRect(130, 100, 93, 28))
        self.pushButton.setObjectName("pushButton")

        # old (be affected by qss (QPushButton:pressed) when clicking mouse)
        # self.pushButton.clicked.connect(self.send_message)

        # new (not to be affected by qss (QPushButton:pressed) when using Enter key)
        send_message_shortcut = QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Return), Form)
        send_message_shortcut.activated.connect(self.send_message)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "PushButton"))

    def send_message(self):
        # do something here
        print("send message")


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())
leminhnguyen
  • 1,518
  • 2
  • 13
  • 19

1 Answers1

2

You are not seeing the "animation" because the button is not being pressed at all, but you can trigger it by using QAbstractButton.animateClick:

send_message_shortcut.activated.connect(self.pushButton.animateClick)

This sets the button pressed (as in setDown(True)), then restores it after a default 100ms interval and sends both released and clicked signals respectively. If you want a different time interval, use a lambda (or do it in your own method) and set the argument in ms.

musicamante
  • 41,230
  • 6
  • 33
  • 58
  • Thanks you for replying, I've tried your snippet of code, but the button seem not to be affected by `qss` ( Eg: the button will be changed background color as clicking left mouse, in my code above) – leminhnguyen Aug 28 '19 at 15:35
  • Here it works fine on Linux (I can't test on Windows right now). Have you tried setting a longer interval? – musicamante Aug 28 '19 at 16:45
  • yes, I've tried `send_message_shortcut.activated.connect(lambda: self.pushButton.animateClick(1000))`, but it doesn't work as expected. – leminhnguyen Aug 29 '19 at 02:29
  • What OS, Python and PyQt versions are you using? Does `send_message_shortcut.activated.connect(lambda: self.pushButton.setDown(True))` do anything? – musicamante Aug 29 '19 at 02:52
  • Python: 3.6.8, PyQt5 and OS: Windows. I've added your snippet of code, but it's do nothing when pressing `Enter`. – leminhnguyen Aug 29 '19 at 03:01
  • 1
    I just tried again with your original code on Windows implementation (but I've to say that it actually is PyQt4 and under Wine) using `self.pushButton.setDown(True)` within the send_message method, and it works perfectly. Are you sure you've not added something else? Can you try again using your posted code (exactly the same) and just add animateClick or even setDown at the end of the send_message method? – musicamante Aug 29 '19 at 03:13
  • thanks you, it works well now, it's probaly my ommision. – leminhnguyen Aug 30 '19 at 04:01