简体   繁体   English

如何将值从一个对话框传递到另一个对话框

[英]How to pass value from one dialog to another

The code below creates a dialog window with a QLabel and a single QPushButton . 下面的代码创建一个带有QLabel和单个QPushButton的对话框窗口。 Clicking the button brings up a second dialog with a text field and Confirm button. 单击该按钮将弹出第二个对话框,其中包含文本字段和“ Confirm按钮。 User enters a text in to the text field and clicks 'Confirm' button. 用户在文本字段中输入文本,然后单击“确认”按钮。 The second dialog closes returning the user entered text. 第二个对话框关闭,返回用户输入的文本。 When returned the first dialog uses it to replace label's 'Default text value'. 返回后,第一个对话框将使用它替换标签的“默认文本值”。

在此处输入图片说明

How to pass the user text value to the first dialog? 如何将用户文本值传递给第一个对话框?

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
app = QApplication([])


class Modal(QDialog):
    def __init__(self, parent):
        super(Modal, self).__init__(parent)
        self.setLayout(QVBoxLayout())
        self.lineedit = QLineEdit(self)
        self.layout().addWidget(self.lineedit)
        button = QPushButton(self)
        button.setText('Confirm')
        button.clicked.connect(self.close)
        self.layout().addWidget(button)
        self.setModal(True)
        self.show()


class Dialog(QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)
        self.setLayout(QVBoxLayout())
        button = QPushButton('Show modal dialog')
        button.clicked.connect(self.showModal)
        self.label = QLabel('Default text value')
        self.layout().addWidget(self.label)
        self.layout().addWidget(button)
        self.resize(200, 50)
        self.show()


    def showModal(self):
        dialog = Modal(self)


dialog = Dialog()
app.exec_()

You could send a signal from a dialog and catch it in the other. 您可以从对话框发送信号,然后将其捕获。

Define a signal in the emitting dialog, using pyqtSignal : 使用pyqtSignal在发射对话框中定义一个信号:

class Modal(QDialog):

    confirmed = pyqtSignal(str)
    # ...

The signal has one argument of type str and will be emitted from the confirm slot, after reading from the line edit the text entered by the user: 该信号具有一个类型为str自变量,将从行中读取用户输入的文本后,从confirm插槽中发出该信号:

def confirm(self):
    self.close()
    value = self.lineedit.text()
    print ('entered value: %s' % value)
    self.confirmed.emit(value) #emit the signal, passing the text as its only argument

To catch the signal, the other class need a slot: 为了捕获信号,另一个类需要一个插槽:

class Dialog(QDialog):
# ...
def changeText(self, t):
    self.label.setText(t)

The slot function will receive the text in its t argument, and set the label text accordingly, but for this to happen the signal and the slot must be connected . slot函数将在其t参数中接收文本,并相应地设置标签文本,但是要做到这一点,信号和slot必须连接

First, let's edit the Modal class constructor, and remove this last two lines: 首先,让我们编辑Modal类的构造函数,并删除最后两行:

    self.setModal(True)
    self.show()

Let's use them in the Dialog 's showModal , after connecting the changeText slot and the Modal confirmed signal: 连接changeText插槽和Modal confirmed信号后,让我们在DialogshowModal使用它们:

def showModal(self):
    modal_dialog = Modal(self)
    modal_dialog.confirmed.connect(self.changeText) #connect signal and slot
    modal_dialog.setModal(True)
    modal_dialog.show()

Full reference: Support for Signals and Slots 完整参考: 支持信号和插槽

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys
from functools import partial

class Modal(QDialog):
    def __init__(self, parent):
        super(Modal, self).__init__(parent)
        self.setLayout(QVBoxLayout())
        self.lineedit = QLineEdit(self)
        self.layout().addWidget(self.lineedit)
        button = QPushButton(self)
        button.setText('Confirm')
        button.clicked.connect(partial(self.confirm,parent)) #using partial to make a slot alog with parameters
        self.layout().addWidget(button)
        self.setModal(True)
        self.exec_()  #  Use exec if you want to really want to create modal dialog

    def confirm(self,parent):
        self.accept() #instead of close use its accept feature
        value = self.lineedit.text()
        parent.label.setText(value)  # acessing DialogClass object that you passed  while calling show modal
        print('entered value: %s' % value)


class Dialog(QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)
        self.setLayout(QVBoxLayout())
        button = QPushButton('Show modal dialog')
        button.clicked.connect(self.showModal)
        self.label = QLabel('Default text value')
        self.layout().addWidget(self.label)
        self.layout().addWidget(button)
        self.resize(200, 50)
        self.show()

    def showModal(self):
        modal_dialog = Modal(self)

app = QApplication([])
dialog = Dialog()
sys.exit(app.exec_())

Use the property accept of a dialog in confirm and before accepting the modal dialog,get access to parent dialog and its attribute label and set its text.Also I have used partial to pass a Slot with arguments 在确认时使用对话框的属性accept,然后在接受模式对话框之前,使用父对话框及其属性标签并设置其文本。此外,我还使用了partial来传递带有参数的Slot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中将值从一个HTML传递到另一个 - How can pass value from one HTML to another in python 如何将值/变量从一个 function 传递到 django 中的另一个 - 视图 - How to pass value/variable from one function to another in django - views 多个多处理队列如何将值从一个函数传递给另一个函数 - How multiple multiprocessing queues pass a value from one function to another 如何将条目的值从一个 class 传递到另一个 class? - How to pass value of Entry from one class to another class? 如何将值从一个 kivy 屏幕传递到另一个屏幕? - How to pass value from one kivy screen to another? PyQt:将值从一个类传递给另一个类 - PyQt: pass the value from one class to another 如何将变量值从一个文件中的一个类传递到另一个文件中的另一个类 python tkinter - How to pass variable value from one class in one file to another class in another file python tkinter 如何在一个 function 中将变量的值从一个 IF 传递到另一个 IF? - How do I pass the value of a variable from one IF to another, inside one function? 如何将数据从一个 function 传递到另一个? - How to pass data from one function to another? 如何将异常从一个进程传递到另一个进程? - How to pass exception from one process to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM