我有一个简单的例子:
from PyQt5.QtWidgets import QDialog,QFileDialog
from PyQt5.uic import loadUi
from PyQt5.QtCore import pyqtSlot, pyqtSignal
class LoadDialog(QDialog):
completed = pyqtSignal()
def __init__(self, parent=None):
super(LoadDialog, self).__init__(parent)
loadUi("trinity/loaddlg.ui", self)
@pyqtSlot()
def on_btnLoad_clicked(self):
print("Button Clicked")
self.completed.emit()
self.close()
class LoadController():
def __init__(self, parent=None):
super(LoadController, self).__init__(parent)
self.getLoaderFiles();
def getLoaderFiles(self):
loader = LoadDialog(self)
loader.completed.connect(self.loadData)
loader.show()
def loadData(self):
print("Should See Signal Here!)
我得到的是“单击按钮”输出,而不是“应该在这里看到信号”输出。似乎没有发出完成的信号,或者呼叫类未正确连接到事件。有人可以吗 告诉我这段代码是怎么回事?
编辑: 用户界面文件:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>loaddlg</class>
<widget class="QDialog" name="loaddlg">
<property name="windowModality">
<enum>Qt::WindowModal</enum>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>597</width>
<height>173</height>
</rect>
</property>
<property name="windowTitle">
<string>Trinity Pain Management Payment Summary</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item alignment="Qt::AlignTop">
<widget class="QLabel" name="label">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Select The Charge File:</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="txtChgFileName"/>
</item>
<item>
<widget class="QToolButton" name="btnChgFileName">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item alignment="Qt::AlignTop">
<widget class="QLabel" name="label_2">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Select The Payment File:</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLineEdit" name="txtPayFileName"/>
</item>
<item>
<widget class="QToolButton" name="btnPayFileName">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item alignment="Qt::AlignRight">
<widget class="QPushButton" name="btnLoad">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Load</string>
</property>
</widget>
</item>
<item alignment="Qt::AlignRight">
<widget class="QPushButton" name="btnCancel">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Cancel</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
使用QT Creator设计对话框窗口。
答案 0 :(得分:1)
我在您的代码中看到了一些不一致之处:
LoadController不会从任何人继承,因此我不理解您在父级上花费了多少:super(LoadController, self).__init__(parent)
LoadDialog希望以父级形式接收QWidget,但LoadController不是QWidget,因此它应该引发错误。
考虑到上述情况,并且除非函数的作用域更大,否则函数完成时将消除局部变量,因此,对于该对话而言,持久存在的对话必须是该类的成员。
from PyQt5.QtWidgets import QDialog,QFileDialog, QApplication
from PyQt5.uic import loadUi
from PyQt5.QtCore import pyqtSlot, pyqtSignal
class LoadDialog(QDialog):
completed = pyqtSignal()
def __init__(self, parent=None):
super(LoadDialog, self).__init__(parent)
loadUi("trinity/loaddlg.ui", self)
@pyqtSlot()
def on_btnLoad_clicked(self):
print("Button Clicked")
self.completed.emit()
self.close()
class LoadController():
def __init__(self):
super(LoadController, self).__init__()
self.getLoaderFiles();
def getLoaderFiles(self):
self.loader = LoadDialog()
self.loader.completed.connect(self.loadData)
self.loader.show()
def loadData(self):
print("Should See Signal Here!")
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
controller = LoadController()
sys.exit(app.exec_())
防止窗口关闭的另一种方法是使用exec_()
阻止函数的输出:
def getLoaderFiles(self):
loader = LoadDialog()
loader.completed.connect(self.loadData)
loader.exec_()