0

I am building a simple application that opens a QDialog, and from a QComboBox that belongs to the child, I can select an item and see some information. What I need to do is to get some of the information shown by the selected item from the comboBox (or another data of this child).

This is the code that i am using to open the child widget:

class Window(QMainWindow):
  def __init__(self):
    #A lot of stuff in here

  #I connect a QPushButton to this method to open the child
  def Serial_connection(self, event):
    configurePort.ConfigurePort(self).show()

And this is the code of the child:

class ConfigurePort(QDialog):
  def __init__(self, parent = None):
    QDialog.__init__(self, parent)
    uic.loadUi("configurePort.ui", self)

    self.initUi()

  def initUi(self):
    self.comboBox.activated[str].connect(self.Selected)
    self.label.hide()

  def Selected(self, text):
    if text == "option 1":
      self.label.setText("Option 1 selected")

Now, from the Selected method, I need to get the text: "Option 1 selected" and send it to the parent (the QMainWindow) to use this information to do another thing.

How can I do this? How can I retrieve data from a child? Hope you can help me.

Pablo Flores
  • 667
  • 1
  • 13
  • 33
  • Is this a temporary dialog just to get user input, or does it stay open? Do you want to respond to the selection immediately as they make it? Or do you want to wait until they click a "save" button and close out the dialog. – Brendan Abel May 18 '16 at 17:13
  • Thank you for your comment. It is a temporary `QDialog`. When I select an item from a `QComboBox`, it shows some information. I need to take a variable from that info, and send it to the parent (from the `QDialog`), so i can use that info to do another thing later. – Pablo Flores May 18 '16 at 18:19

1 Answers1

1

Generally, when you're using a temporary dialog to get information from the user, the dialog should be modal, so you can block all other actions until the user is finished with the dialog. It also allows you to call the dialog much like a function, and get results back from it.

Here is an example of a modal dialog that returns the text results.

class ConfigurePort(QDialog):
    def __init__(self, parent = None):
        QDialog.__init__(self, parent)
        uic.loadUi("configurePort.ui", self)

        self.initUi()

    def initUi(self):
        self.comboBox.activated[str].connect(self.Selected)
        self.label.hide()

    def Selected(self, text):
        if text == "option 1":
            self.label.setText("Option 1 selected")

    def getLabelText(self):
        return self.label.text()

    @classmethod
    def launch(cls, parent=None):
        dlg = cls(parent)
        dlg.exec_()
        text = dlg.getLabelText()
        return text


class Window(QMainWindow):
    ...
    def Serial_connection(self, event):
        text = configurePort.ConfigurePort.launch(self)
        print text
Brendan Abel
  • 35,343
  • 14
  • 88
  • 118
  • Thank you for your answer, It helps me a lot. I have one question: what does this line do: `dlg = cls(parent)`? . I do not understand very well the `decorators`. Thank you again for your answer. – Pablo Flores May 19 '16 at 22:00
  • 1
    Normally, the first argument to a method is the class instance (`self`). If you decorate a method with `classmethod`, you can call the method using just the class and not an instance of the class, and the first argument will be the class (not an instance). So basically, you call this function, it creates an instance of the dialog class (`cls(...)`), and runs it modally (`cls.exec_()`). `cls.exec_()` will block until the dialog is closed. It then returns the dialog results back to the calling function in the main window. – Brendan Abel May 19 '16 at 22:03