Results 1 to 4 of 4

Thread: Return values from QDialog to MainWindow

  1. #1
    Join Date
    Apr 2015
    Posts
    28
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default 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:

    Qt Code:
    1. class MainWindow(QtGui.QMainWindow):
    2.  
    3. deviceList = []
    4.  
    5. def openAddDialog(self):
    6. print "running openAddDialog..."
    7. diag = AddDevice()
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. from PySide.QtGui import *
    2.  
    3. class AddDevice(QDialog):
    4.  
    5. def __init__(self):
    6. QDialog.__init__(self)
    7. self.setWindowTitle("Add Device")
    8. self.resize(400, 300)
    9.  
    10. iLabel = QLabel("IPv4 address:")
    11. self.iText = QLineEdit()
    12.  
    13. uLabel = QLabel("Username:")
    14. self.uText = QLineEdit()
    15.  
    16. pLabel = QLabel("Password:")
    17. self.pText = QLineEdit()
    18.  
    19. okButton = QPushButton()
    20. okButton.setText("OK")
    21. okButton.clicked.connect(self.add)
    22.  
    23. frame = QFrame(self)
    24.  
    25. formLayout = QFormLayout(frame)
    26. formLayout.addRow(iLabel, self.iText)
    27. formLayout.addRow(uLabel, self.uText)
    28. formLayout.addRow(pLabel, self.pText)
    29. formLayout.addRow(okButton, okButton)
    30.  
    31. self.exec_()
    32.  
    33. def add(self):
    34. ipv4 = self.iText.text()
    35. username = self.uText.text()
    36. password = self.pText.text()
    37.  
    38. print ipv4, username, password
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  3. #3
    Join Date
    Apr 2015
    Posts
    28
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Return values from QDialog to MainWindow

    Like this?

    Qt Code:
    1. def openAddDialog(self):
    2. print "running openAddDialog..."
    3. diag = AddDevice()
    4. diag.exec_()
    5. print diag.getIPv4(), diag.getUsername(), diag.getPassword()
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by ecce; 16th April 2015 at 20:43.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,004 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Return values from QDialog to MainWindow

    This is a brilliant way to do what you want

    exec() returns if the dialog was accepted or rejected (= canceled) so just use this information and read or do not read the username etc.

Similar Threads

  1. Containers and return values
    By tuamadre71 in forum Newbie
    Replies: 0
    Last Post: 15th January 2012, 12:04
  2. return-value and QDialog
    By homerun4711 in forum Newbie
    Replies: 4
    Last Post: 16th February 2011, 16:34
  3. Replies: 10
    Last Post: 10th February 2011, 19:45
  4. how to program custom Dialogs with return values
    By momesana in forum Qt Programming
    Replies: 2
    Last Post: 23rd December 2006, 00:37
  5. How to return something not int with a QDialog
    By SkripT in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2006, 17:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.