X-Black...
X-Black...

Reputation: 1596

How to send data between Dialogs in python PyQt5

i try to send the value of the spinbox and state of the checkbox from the second dialog to the parent dialog....but the value returned is not the value from the second dialog here it the code

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

__appname__ = 'Dialog app '

class Dialog(QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)

        self.btn = QPushButton('open dialog')
        self.label1 = QLabel('Label 1 Result')
        self.label2 = QLabel('Label 2 result')

        layout = QVBoxLayout()
        layout.addWidget(self.btn)
        layout.addWidget(self.label1)
        layout.addWidget(self.label2)

        self.setLayout(layout)
        self.setWindowTitle(__appname__)


        self.btn.clicked.connect(self.dialogOpen)


    def dialogOpen(self):
        dialog = subDialog()

        self.sub = subDialog()

        self.check = str(self.sub.checkbox.isChecked())
        self.spin = str(self.sub.spinbox.value())


        if dialog.exec_():
            self.label1.setText('spinbox value is ' + self.spin)
            self.label2.setText('Checkbox is ' + self.check)


class subDialog(QDialog):
    def __init__(self, parent=None):
        super(subDialog, self).__init__(parent)

        self.setWindowTitle('Sub Dialog')

        self.checkbox = QCheckBox()
        self.spinbox = QSpinBox()
        self.buttonOK = QPushButton('Ok')
        self.buttonCancel = QPushButton('cancel')



        lay = QGridLayout()
        lay.addWidget(self.spinbox, 0, 0)
        lay.addWidget(self.checkbox, 0, 1,)
        lay.addWidget(self.buttonOK)
        lay.addWidget(self.buttonCancel)

        self.setLayout(lay)

        self.buttonOK.clicked.connect(self.accept)
        self.buttonCancel.clicked.connect(self.reject)

app = QApplication(sys.argv)
form = Dialog()
form.show()
app.exec_()

Upvotes: 1

Views: 706

Answers (2)

eyllanesc
eyllanesc

Reputation: 244301

exec_() is blocking so the data you get is data before the new window is displayed, you must obtain the data that closes the window, that is, after exec_():

def dialogOpen(self):
    sub = subDialog()

    if sub.exec_():
        check = str(sub.checkbox.isChecked())
        spin = str(sub.spinbox.value())
        self.label1.setText('spinbox value is ' + spin)
        self.label2.setText('Checkbox is ' + check)

Upvotes: 2

MrLeeh
MrLeeh

Reputation: 5589

The problem is that you read the values before you even opened the Dialog. This will only give you the initial values. Place the readings after you opened the Dialog and you will be fine.

def dialogOpen(self):
    self.sub = subDialog()

    if self.sub.exec_():
        check = str(self.sub.checkbox.isChecked())
        spin = str(self.sub.spinbox.value())

        self.label1.setText('spinbox value is ' + spin)
        self.label2.setText('Checkbox is ' + check)

Upvotes: 4

Related Questions