注意:
我正在python3.6上使用PyQt5。但是如果你的答案是C++中的QT5,那也很好。我可以在大多数时间里从C++到Python进行翻译。你知道吗
有时我想通过编程方式单击一个按钮。我喜欢animateClick()
创建的漂亮动画,但是按钮不应该触发信号。所以我尝试的是:
self.__myBtn.blockSignals(True)
self.__myBtn.animateClick()
self.__myBtn.blockSignals(False)
不幸的是,clicked
信号还是会触发。你知道吗
为了您的方便,我编写了一个独立的演示应用程序。运行一些测试可能会有所帮助。只需将下面的代码复制粘贴到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_())
''''''
一旦主窗口生成,演示应用程序将运行函数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"
。你知道吗
我做错了什么?你知道吗
如果您只想要视觉效果,那么可以使用
setDown()
方法和QTimer
:相关问题 更多 >
编程相关推荐