Return values from QDialog to MainWindow
I've created a simple custom QDialog with three QLineEdits. When clicking the "OK" button I want to read the entered text and return them to the MainWindow object where the values will be stored in a list.
How do I return the values from the QDialog class so I can store them in the list in the MainWindow class, and how do I call it properly?
This is the code I have at the moment:
Code:
deviceList = []
def openAddDialog(self):
print "running openAddDialog..."
diag = AddDevice()
Code:
from PySide.QtGui import *
def __init__(self):
self.setWindowTitle("Add Device")
self.resize(400, 300)
iLabel
= QLabel("IPv4 address:")
okButton.setText("OK")
okButton.clicked.connect(self.add)
formLayout = QFormLayout(frame)
formLayout.addRow(iLabel, self.iText)
formLayout.addRow(uLabel, self.uText)
formLayout.addRow(pLabel, self.pText)
formLayout.addRow(okButton, okButton)
self.exec_()
def add(self):
ipv4 = self.iText.text()
username = self.uText.text()
password = self.pText.text()
print ipv4, username, password
Re: Return values from QDialog to MainWindow
The pattern for a modal dialog is usually this
1. create dialog instance
2. call exec() on that instance
3. read values using getter methods
Cheers,
_
Re: Return values from QDialog to MainWindow
Like this?
Code:
def openAddDialog(self):
print "running openAddDialog..."
diag = AddDevice()
diag.exec_()
print diag.getIPv4(), diag.getUsername(), diag.getPassword()
Does not seem like a good way to go, the values will be read if the user cancels/aborts the dialog window. This is exactly what I can't get my head around. I want the buttons and it's slots in the dialog class, but the values from the fields should be sent to or read from the MainWindow.
Re: Return values from QDialog to MainWindow
This is a brilliant way to do what you want :D
exec() returns if the dialog was accepted or rejected (= canceled) so just use this information and read or do not read the username etc.