Reputation: 1681
I have a simple example:
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!)
I am getting the "Button Clicked" output but not the "Should See Signal Here" output. It appears that the completed signal is either not emitted or the calling class is not properly connected to the event. Can someone tell me what's going on with this code?
EDIT: UI File:
<?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>
Using QT Creator to design the dialog window.
Upvotes: 0
Views: 4870
Reputation: 243897
I see several inconsistencies in your code:
LoadController does not inherit from anyone so I do not understand what you spend in parent: super(LoadController, self).__init__(parent)
LoadDialog expects to receive a QWidget as a parent but LoadController is not a QWidget so it should throw you an error.
Taking into account the above, and also that a local variable is eliminated when the function is finished unless its scope is greater, so for which the dialogue persists must be a member of the class.
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_())
Another way to prevent the window from closing is to block the output of the function using exec_()
:
def getLoaderFiles(self):
loader = LoadDialog()
loader.completed.connect(self.loadData)
loader.exec_()
Upvotes: 1