import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog
from PyQt5.QtCore import QCoreApplication
from PyQt5 import uic
qt_design_ui = "/home/lin/program/Qt/Notepad/notepad.ui"
Ui_Notepad, QtBaseClass = uic.loadUiType(qt_design_ui)
class Notepad(QMainWindow, Ui_Notepad):
def __init__(self):
super(Notepad, self).__init__()
self.setupUi(self)
def on_quitButton_clicked(self):
# QCoreApplication.quit();
print("h")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Notepad()
window.show()
sys.exit(app.exec_())
QtDesign生成的UI文件:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Notepad</class>
<widget class="QMainWindow" name="Notepad">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Notepad</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>70</x>
<y>10</y>
<width>258</width>
<height>222</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="quitButton">
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="text">
<string>Quit</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>19</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actionOpen">
<property name="text">
<string>Open</string>
</property>
</action>
<action name="actionSave">
<property name="text">
<string>Save</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
单击该按钮时,应输出“h”。
然而,两个'h'是真实输出的。
也许连接在某处绑定了两次。但我不知道它发生在哪里。
而且,如果我将self.quitButton.clicked.connect(self.on_quitButton_clicked)
添加到__init__
,则输出3'h。
用C ++编写时,没关系。
Python3的版本:
Python 3.5.2
PyQt5的版本:
Name: PyQt5
Version: 5.9
Summary: Python bindings for the Qt cross platform UI and application toolkit
Home-page: https://www.riverbankcomputing.com/software/pyqt/
Author: Riverbank Computing Limited
Author-email: info@riverbankcomputing.com
License: GPL v3
Location: /usr/local/lib/python3.5/dist-packages
Requires: sip
答案 0 :(得分:1)
发生此问题是因为您使用的是Connecting Slots By Name功能。如果信号有多个过载,则会为每个过载进行自动连接。 clicked信号有两个重载:一个发送默认的 checked
参数,另一个不发送。要仅选择其中一个,您需要像这样定义插槽:
class Notepad(QMainWindow, Ui_Notepad):
...
@QtCore.pyqtSlot()
def on_quitButton_clicked(self):
print("hello world")