使用 PyQt 从孩子那里获取数据 [英] Getting data from child using PyQt

查看:70
本文介绍了使用 PyQt 从孩子那里获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个简单的应用程序,它打开一个 QDialog,从属于孩子的 QComboBox 中,我可以选择一个项目并查看一些信息.我需要做的是从comboBox中获取所选项目显示的一些信息(或这个孩子的其他数据).

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()

这是孩子的代码:

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")

现在,从 Selected 方法中,我需要获取文本:Option 1 selected"并将其发送给父级(QMainWindow)以使用此信息做另一件事.

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.

推荐答案

一般来说,当你使用临时对话框从用户那里获取信息时,对话框应该是modal,这样你就可以阻止所有其他操作,直到用户完成对话框.它还允许您像调用函数一样调用对话框,并从中获取结果.

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

这篇关于使用 PyQt 从孩子那里获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆