Python 3 pyqt signal and slot, relation between forms, relation between main form and subform

When using pyqt, data transfer between subform and main form is needed.

The name of the new subform is DateDialog2.py

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class DateDialog(QDialog):
    Signal_OneParameter = pyqtSignal(str)
    
    def __init__(self,parent =None):
        super(DateDialog,self).__init__(parent)
        self.setWindowTitle('Child window: used to transmit signals')
        
        #Add controls to layout
        layout = QVBoxLayout(self)
        
        self.label = QLabel(self)
        self.label.setText('The former transmits the built-in signal\n The latter sends a custom signal')
        
        self.datetime_inner = QDateTimeEdit(self)
        self.datetime_inner.setCalendarPopup(True)
        self.datetime_inner.setDateTime(QDateTime.currentDateTime())
        
        self.datetime_emit = QDateTimeEdit(self)
        self.datetime_emit.setCalendarPopup(True)
        self.datetime_emit.setDateTime(QDateTime.currentDateTime())
        
        layout.addWidget(self.label)
        layout.addWidget(self.datetime_inner)
        layout.addWidget(self.datetime_emit)
        
        #Use two buttons to connect the accept() and reject() slot functions respectively
        buttons = QDialogButtonBox(
            QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
            Qt.Horizontal,self)
        buttons.accepted.connect(self.accept)
        buttons.rejected.connect(self.reject)
        
        self.datetime_emit.dateTimeChanged.connect(self.emit_signal)
        layout.addWidget(buttons)
        
    def emit_signal(self):
        date_str = self.datetime_emit.dateTime().toString()
        self.Signal_OneParameter.emit(date_str)

The name of the main form is CallDialogMainWin2.py

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
from DateDialog2 import DateDialog
                   
class WinForm(QWidget):
    def __init__(self, parent =None):
        super(WinForm,self).__init__(parent)
        self.resize(400,90)
        self.setWindowTitle('Example of signal and slot transfer parameters')
        
        self.open_btn =  QPushButton('Acquisition time')
        self.lineEdit_inner = QLineEdit(self)
        self.lineEdit_emit = QLineEdit(self)
        self.open_btn.clicked.connect(self.openDialog)
        
        self.lineEdit_inner.setText('Time to receive the built-in signal of the subwindow')
        self.lineEdit_emit.setText('Time to accept custom signals from child windows')
        
        grid = QGridLayout()
        grid.addWidget(self.lineEdit_inner)
        grid.addWidget(self.lineEdit_emit)
        
        grid.addWidget(self.open_btn)
        self.setLayout(grid)
        
    def openDialog(self):
        dialog = DateDialog(self)
        '''Connect the built-in signal of the sub window with the slot function of the main window'''
        dialog.datetime_inner.dateTimeChanged.connect(self.deal_inner_slot)
        '''Connect custom signal of child window with slot function of main window'''
        dialog.Signal_OneParameter.connect(self.deal_emit_slot)
        dialog.show()
        
    def deal_inner_slot(self,date):
        self.lineEdit_inner.setText(date.toString())
        
    def deal_emit_slot(self,dateStr):
        self.lineEdit_emit.setText(dateStr)
        
if __name__=='__main__':
    app = QApplication(sys.argv)
    form = WinForm()
    form.show()
    sys.exit(app.exec_())
        

Operation effect

When you change the date on a subform, it is automatically passed to the main form

self.datetime_emit.dateTimeChanged.connect(self.emit_signal)

def emit_signal(self):
        date_str = self.datetime_emit.dateTime().toString()
        self.Signal_OneParameter.emit(date_str)

The key is how to realize the transmission of customized signals that meet specific conditions?

When the time of control datetime  exit changes, the slot function exit  Signal of the sub window will be triggered. In this slot function, the user-defined Signal signal -- OneParameter will be sent. This Signal function is used to pass the date  STR parameter to the slot function of the main function.

def openDialog(self):
        dialog = DateDialog(self)
        '''Connect the built-in signal of the sub window with the slot function of the main window'''
        dialog.datetime_inner.dateTimeChanged.connect(self.deal_inner_slot)
        '''Connect custom signal of child window with slot function of main window'''
        dialog.Signal_OneParameter.connect(self.deal_emit_slot)
        dialog.show()

The key of the main window is to bind the built-in signal and custom signal of the sub window to the slot function of the main window.

###Reference pyqt5 rapid development and Practice

Keywords: Qt Windows

Added by remmargorp on Tue, 19 Nov 2019 19:57:20 +0200