【问题标题】:Cannot prevent animateClick() from firing a signal with QPushButton.blockSignals(True)无法阻止 animateClick() 使用 QPushButton.blockSignals(True) 触发信号
【发布时间】:2018-10-07 14:04:36
【问题描述】:

注意:
我在 Python 3.6 上使用 PyQt5。但是,如果您的答案是针对 C++ 中的 Qt5,那也没关系。我大部分时间都可以从 C++ 翻译成 Python。

 

1。问题

有时我想以编程方式单击一个按钮。我喜欢animateClick() 创建的漂亮动画,但按钮不应该发出信号。所以我尝试的是这样的:

    self.__myBtn.blockSignals(True)
    self.__myBtn.animateClick()
    self.__myBtn.blockSignals(False)

不幸的是,clicked 信号还是会触发。

 

2。演示应用

为了您的方便,我编写了一个独立的演示应用程序。运行一些测试会很有帮助。只需将下面的代码复制粘贴到 python 文件中并运行它。你应该会看到这样的窗口:

代码如下:

import sys
import os

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


'''================================================================================'''
'''|                           CUSTOM MAIN WINDOW                                 |'''
'''================================================================================'''
class CustomMainWindow(QMainWindow):

    def __init__(self):
        super(CustomMainWindow, self).__init__()

        # -------------------------------- #
        #           Window setup           #
        # -------------------------------- #

        # 1. Define the geometry of the main window
        # ------------------------------------------
        self.setGeometry(100, 100, 800, 200)
        self.setWindowTitle("QPushbutton animateClick() test")

        # 2. Create frame and layout
        # ---------------------------
        self.__frm = QFrame(self)
        self.__frm.setStyleSheet("QWidget { background-color: #ffffff }")
        self.__lyt = QVBoxLayout()
        self.__lyt.setAlignment(Qt.AlignTop)
        self.__frm.setLayout(self.__lyt)
        self.setCentralWidget(self.__frm)

        # 3. Create QLineEdit
        # -------------------
        self.__myBtn = QPushButton("click me")
        self.__myBtn.clicked.connect(self.__btn_clicked)
        self.__myBtn.setFixedHeight(100)
        self.__myBtn.setFixedWidth(300)
        self.__lyt.addWidget(self.__myBtn)

        for i in range(10):
            QTimer.singleShot(100 + 500*i, self.__my_click_animation)

        self.show()

    ''''''

    def __my_click_animation(self):
        self.__myBtn.blockSignals(True)
        self.__myBtn.animateClick()
        self.__myBtn.blockSignals(False)

    ''''''


    def __btn_clicked(self):
        print("I'm clicked")

'''=== end Class ==='''


if __name__ == '__main__':
    app = QApplication(sys.argv)
    QApplication.setStyle(QStyleFactory.create('Fusion'))
    myGUI = CustomMainWindow()
    sys.exit(app.exec_())

''''''

 

3。 Demo 应用的输出

主窗口一出现,Demo 应用就会运行函数self.__my_click_animation() 10 次,每次运行间隔 500 毫秒:

    def __my_click_animation(self):
        self.__myBtn.blockSignals(True)
        self.__myBtn.animateClick()
        self.__myBtn.blockSignals(False)

我希望看到按钮被点击 10 次(视觉动画),但没有发出任何信号。不幸的是,信号确实触发了,我在终端上打印了 10 次 "I'm clicked"

我做错了什么?

【问题讨论】:

    标签: python qt pyqt qt5 pyqt5


    【解决方案1】:

    如果你只想要视觉效果,那么你可以使用setDown() 方法和QTimer

    def __my_click_animation(self):
        self.__myBtn.setDown(True)
        QTimer.singleShot(100, lambda: self.__myBtn.setDown(False))
    

    【讨论】:

    • 你好。你的咖啡怎么样?
    • 我确定你还喜欢喝咖啡。我又寄了 3 美元 ;-)
    猜你喜欢
    • 1970-01-01
    • 2013-08-19
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多