当前位置:网站首页>PyQt,PySide-槽函数被执行了两次
PyQt,PySide-槽函数被执行了两次
2022-06-27 11:46:00 【qqblack】
测试环境
pip list的输出信息如下Package Version ---------- ------- build 0.7.0 colorama 0.4.4 packaging 21.3 pep517 0.12.0 pip 21.3.1 pyparsing 3.0.6 PySide6 6.2.3 setuptools 60.5.0 shiboken6 6.2.3 tomli 2.0.0 wheel 0.37.1- Python 版本:3.9.9
- 操作系统:windows 10
问题描述
用designer创建了一个空的主窗体(Main Window),然后添加一个名为acceptBtn的按钮。然后用uic工具生成了common_signal_slot_ui.py文件。
然后,新建一个common_ones.py文件,代码内容如下。
from PySide6 import QtCore
from PySide6.QtCore import Signal,Slot
from PySide6.QtWidgets import QMainWindow, QGraphicsView, QFileDialog, QApplication, QMessageBox,QHBoxLayout
import sys
class MainWindow(QMainWindow):
def __init__(self, app):
super().__init__()
self.app = app
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
QtCore.QMetaObject.connectSlotsByName(self) # 开启自动连接信号和槽.
@QtCore.Slot()
def on_acceptBtn_clicked(self):
print("accept")
if __name__ == "__main__":
from common_signal_slot_ui import Ui_MainWindow
app = QApplication(sys.argv)
window = MainWindow(app)
window.show()
sys.exit(app.exec())
结果,程序运行起来后,点击一次该按钮,会print两次“accept”。说明槽函数on_acceptBtn_clicked被执行了两次
解决方案
- 槽函数被执行多次,通常是因为connect了多次。显然并没有
- 通过检查
uic工具生成的.py文件,发现它里面也有一行QtCore.QMetaObject.connectSlotsByName.- 说明,是因为
QtCore.QMetaObject.connectSlotsByName执行了两次,导致的这个问题 - 注释掉
common_ones.py文件中的此行代码后,问题解决了
- 说明,是因为
边栏推荐
- Detailed explanation of interprocess communication
- 解开C语言的秘密《关键字》(第六期)
- [tcapulusdb knowledge base] tcapulusdb doc acceptance - table creation approval introduction
- 56. Core principle of flutter - flutter startup process and rendering pipeline
- 星际争霸的虫王IA退役2年搞AI,自叹不如了
- 最大路径和问题(摘樱桃问题)
- StarCraft's Bug King ia retired for 2 years to engage in AI, and lamented that it was inferior
- 盘点一些好用且小众的 Markdown 编辑器
- Drive to APasS!使用明道云管理F1赛事
- Minimum editing distance (linear DP writing method)
猜你喜欢
![[tcapulusdb knowledge base] tcapulusdb doc acceptance - transaction execution introduction](/img/d9/f6735906a130834c4b3e28de2b2617.png)
[tcapulusdb knowledge base] tcapulusdb doc acceptance - transaction execution introduction

Unity Shader学习(二)第一个Shader

Excel中输入整数却总是显示小数,如何调整?

C/s architecture

Salesforce 容器化 ISV 场景下的软件供应链安全落地实践

Popular science of device review: popular science of innovative medical device series - sternum plate products

57. The core principle of flutter - layout process

c/s 架构

Unlock the secret of C language key words (issue 6)

面试突击60:什么情况会导致 MySQL 索引失效?
随机推荐
After Jerry's sleep, the regular wake-up system continues to run without resetting [chapter]
等等, 怎么使用 SetMemoryLimit?
On ticheck
MapReduce原理剖析(深入源码)
FileOutputStream
R language dplyr package arrange function sorts dataframe data, sorts dataframe data through multiple data columns, specifies the first field to be sorted in descending order, and does not specify the
alibaba jarslink
Research Report on the overall scale, major manufacturers, major regions, products and application segments of hydraulic torque in the global market in 2022
In 2021, the global carbon graphite brush revenue is about US $2366million, and it is expected to reach US $2701.8 million in 2028
StarCraft's Bug King ia retired for 2 years to engage in AI, and lamented that it was inferior
Xuri 3sdb, installing the original ROS
如何修改 node_modules 裏的文件
Hands on API development
How to find the movie and TV clips with the same lines? These 8 movies search for artifact, and find the corresponding segment in one line
[tcapulusdb knowledge base] Introduction to tcapulusdb data structure
I.MX6ULL启动方式
Wechat applet realizes five-star evaluation
In 2021, the global professional liability insurance revenue was about USD 44740million, and it is expected to reach USD 55980million in 2028. From 2022 to 2028, the CAGR was 3.5%
Jerry added an input capture channel [chapter]
Rxjs mergeMap 的使用场合