Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Not retrieving data from QDialog when clicking on Cancel button
Forum Updated to NodeBB v4.3 + New Features

Not retrieving data from QDialog when clicking on Cancel button

Scheduled Pinned Locked Moved Solved Qt for Python
4 Posts 3 Posters 704 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hachbani
    wrote on last edited by
    #1

    Hello,
    I'm using Pyside2, python 3.8

    I have a QMainWindow with a pushbutton, when the button is clicked, a QDialog shows up

    My Dialog form

    I want to retrieve the texts in the QLineEdits when the Add button is clicked. I've managed to do that using the following code:

    class Dialog(QtWidgets.QDialog, Ui_Dialog):
    	def __init__(self, parent=None):
    		super(Dialog, self).__init__(parent)
    		self.setupUi(self)
    		self.AddButton.clicked.connect(self.close)
    		self.CancelButton.clicked.connect(self.close)
    
    class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    	def __init__(self, parent=None):
    		super(MainWindow, self).__init__(parent)
    		self.setupUi(self)
    		self.ShowDialogButton.clicked.connect(self.showDialog)
    
    	def showDialog(self):
    		d = Dialog(self)
    		d.exec_()
    		self.Data = [d.LineEdit1.text(), d.LineEdit2.text(), d.LineEdit3.text()]
    		self.func(self.Data)
    
    	def foo(self, foo):
    		for txt in foo:
    			print(txt)
    
    if __name__== '__main__':
    	app = QtWidgets.QApplication(sys.argv)
    	mainWin = MainWindow()
    	mainWin.show()
    	sys.exit(app.exec_())
    

    As I said, this works, but the problem is that it also works when I hit cancel.

    I want the func function in my MainWindow class to run only if I hit the Add button and only that.

    How Can I achieve that ?

    Thanks;

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      exec() has a return value, maybe tuple in Python. Use that to determine what was pressed.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • H Offline
        H Offline
        hachbani
        wrote on last edited by
        #3

        Hi @SGaist ,

        How can I retrieve what button has been pressed ?

        JonBJ 1 Reply Last reply
        0
        • H hachbani

          Hi @SGaist ,

          How can I retrieve what button has been pressed ?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @hachbani
          As @SGaist said. Make sure your Add button calls accept(), or done() with a particular value, to exit the dialog. And Cancel button should call reject(). Not just close() as you have now. That is what QDialogs should do. Then the return result of the exec() will give that value to the caller, for you to examine.

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved