[英]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
信号后,让我们在Dialog
的showModal
使用它们:
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.