SoFunction
Updated on 2024-11-16

Example of Window Data Passing in Python PyQt5 in Detail

When developing an application, if there is only one window, you only need to be concerned about how to pass data between the controls inside this window. If the program has more than one window, you need to be concerned about how the data is passed between the different windows.

Single Window Data Transfer

For a single-window program, changes in one control affecting changes in another control are simply solved by the mechanism of signals and slots.

import sys
from  import QWidget, QLCDNumber, QSlider, QVBoxLayout, QApplication
from  import Qt

class WinForm(QWidget):
    def __init__(self, parent=None):
        super(WinForm, self).__init__(parent)
        ()

    def initUI(self):
        # Start by creating the slider and LCD controls
        lcd = QLCDNumber(self)
        slider = QSlider(, self)

        vBox = QVBoxLayout()
        (lcd)
        (slider)

        (vBox)
        # valueChanged() is a signal function for the QSlider that fires a signal whenever the value of the slider changes
        # Then connect to the signal receiving control lcd via connect
        ()

        # ()
        # ()
        # ()
        # ()

        # (2)
        # (4)
        # ()

        (300, 300, 350, 150)
        ("Signal and Slot: Connecting the Slider LCD")

if __name__ == "__main__":
    app = QApplication()
    win = WinForm()
    ()
    (app.exec_())

Multi-Window Data Transfer: Calling Properties

from  import *
from  import *
from  import *

class DateDialog(QDialog):
    def __init__(self, parent=None):
        super(DateDialog, self).__init__(parent)
        ("DataDialog")

        # Add controls to the layout
        layout = QVBoxLayout(self)
         = QDateTimeEdit(self)
        (True)
        (QDateTime().currentDateTime())
        ()

        buttons = QDialogButtonBox( | , , self)
        ()
        ()
        (buttons)


    # Get current datetime from dialog box
    def dateTime(self):
        return ()
    
    # Use static function to create dialog and return (date, time, accepted)
    @staticmethod
    def getDateTime(parent=None):
        dialog = DateDialog(parent)
        result = dialog.exec_()
        date = ()
        return ((), (), result == )

import sys
from  import *
from  import *
from  import *
from DateDialog import DateDialog

class WinForm(QWidget):
    def __init__(self, parent=None):
        super(WinForm, self).__init__(parent)
        (400, 90)
        ("Return value to main window when dialog box closes")

         = QLineEdit(self)
        self.button1 = QPushButton("Pop-up dialog box 1")
        self.(self.onButton1Click)
        
        self.button2 = QPushButton("Pop-up dialog box 2")
        self.(self.onButton2Click)

        gridLayout = QGridLayout()
        ()
        (self.button1)
        (self.button2)
        (gridLayout)

    def onButton1Click(self):
        dialog = DateDialog(self)
        result = dialog.exec_()
        date = ()
        (().toString())
        print('\n date dialog box return value')
        print('date=%s' % str(()))
        print('time=%s' % str(()))
        print('result=%s' % result)
        ()

    def onButton2Click(self):
        date, time, result = ()
        (())
        print('\n date dialog box return value')
        print('date=%s' % str(date))
        print('time=%s' % str(time))
        print('result=%s' % str(result))

if __name__ == "__main__":
    app = QApplication()
    win = WinForm()
    ()
    (app.exec_())

Multi-Window Data Transfer: Signals and Slots

The child window transmits the signal and the parent window receives it.

from  import *
from  import *
from  import *

class DateDialog(QDialog):
    Signal_OneParameter = pyqtSignal(str)

    def __init__(self, parent=None):
        super(DateDialog, self).__init__(parent)
        ("Sub-windows: used to transmit signals")

        # Add controls to the layout
        layout = QVBoxLayout(self)

         = QLabel(self)
        ("The former transmits built-in signals\n the latter transmits customized signals.")

        self.datetime_inner = QDateTimeEdit(self)
        self.datetime_inner.setCalendarPopup(True)
        self.datetime_inner.setDateTime(())

        self.datetime_eimt = QDateTimeEdit(self)
        self.datetime_eimt.setCalendarPopup(True)
        self.datetime_eimt.setDateTime(())

        ()
        (self.datetime_inner)
        (self.datetime_eimt)

        # Use two buttons (Ok and Cancel) to connect to the accept() and reject() slot functions, respectively.
        buttons = QDialogButtonBox( | , , self)
        ()
        ()

        (buttons)

        self.datetime_eimt.(self.emit_signal)

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

import sys
from  import *
from  import *
from  import  *
from DateDialog2 import DateDialog

class WinForm (QWidget):
    def __init__(self, parent=None):
        super(WinForm, self).__init__(parent)
        (400, 90)
        ("Examples of signaling and slot passing parameters.")

        self.open_btn = QPushButton("Getting Time")
        self.lineEdit_inner = QLineEdit(self)
        self.lineEdit_emit  = QLineEdit(self)
        self.open_btn.()

        self.lineEdit_inner.setText("Time to receive the built-in signal from the sub-window")
        self.lineEdit_emit.setText("Time to receive customized signals from the sub-window")
        
        grid = QGridLayout()
        (self.lineEdit_inner)
        (self.lineEdit_emit)
        (self.open_btn)
        (grid)

    def openDialog(self):
        dialog = DateDialog(self)
        '''Connect the built-in signals of a sub-window to the slot function of the main window'''
        dialog.datetime_inner.(self.deal_inner_slot)
        '''Connecting custom signals of a sub-window to a slot function of the main window'''
        dialog.Signal_OneParameter.connect(self.deal_emit_slot)
        ()

    def deal_inner_slot(self, date):
        print(date)
        self.lineEdit_inner.setText(())

    def deal_emit_slot(self, dateStr):
        self.lineEdit_emit.setText(dateStr)

if __name__ == "__main__":
    app = QApplication()
    form = WinForm()
    ()
    (app.exec_())

to this article on Python PyQt5 window data transfer example details of the article is introduced to this, more related PyQt5 window data transfer content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!