Python 如何将值从一个对话框传递到另一个对话框
下面的代码创建了一个带有Python 如何将值从一个对话框传递到另一个对话框,python,qt,dialog,pyqt,Python,Qt,Dialog,Pyqt,下面的代码创建了一个带有QLabel和单个QPushButton的对话框窗口。 单击按钮将弹出第二个对话框,其中包含文本字段和确认按钮。 用户在文本字段中输入文本,然后单击“确认”按钮。 第二个对话框关闭,返回用户输入的文本。 返回时,第一个对话框使用它替换标签的“默认文本值” 如何将用户文本值传递到第一个对话框 from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import * app = QA
QLabel
和单个QPushButton
的对话框窗口。
单击按钮将弹出第二个对话框,其中包含文本字段和确认按钮。
用户在文本字段中输入文本,然后单击“确认”按钮。
第二个对话框关闭,返回用户输入的文本。
返回时,第一个对话框使用它替换标签的“默认文本值”
如何将用户文本值传递到第一个对话框
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_()
您可以从一个对话框发送一个信号,然后在另一个对话框中捕捉它
使用以下命令在“发射”对话框中定义信号:
该信号有一个类型为str
的参数,从编辑用户输入的文本行读取后,将从确认
插槽发出:
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
要捕获信号,另一个类需要一个插槽:
class Dialog(QDialog):
# ...
def changeText(self, t):
self.label.setText(t)
slot函数将在其t
参数中接收文本,并相应地设置标签文本,但要实现这一点,必须连接信号和插槽
首先,让我们编辑Modal
类构造函数,并删除最后两行:
self.setModal(True)
self.show()
在连接changeText
插槽和Modal
确认信号后,让我们在对话框的showmodel
中使用它们:
def showModal(self):
modal_dialog = Modal(self)
modal_dialog.confirmed.connect(self.changeText) #connect signal and slot
modal_dialog.setModal(True)
modal_dialog.show()
全文参考:
在confirm中使用对话框的属性accept,在接受模式对话框之前,访问父对话框及其属性标签并设置其文本。此外,我还使用了partial来传递带有参数的插槽,使用exec()
,而不是show()
,然后只需执行self.label.setText(模式对话框.lineedit.text())
exec()
为我工作!谢谢
def showModal(self):
modal_dialog = Modal(self)
modal_dialog.confirmed.connect(self.changeText) #connect signal and slot
modal_dialog.setModal(True)
modal_dialog.show()
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_())