下面的代码会创建一个对话框窗口,其中包含QLabel
和单个QPushButton
。
单击该按钮会弹出第二个对话框,其中包含文本字段和Confirm
按钮。
用户在文本字段中输入文本,然后单击“确认”按钮。
第二个对话框关闭,返回用户输入的文本。
返回时,第一个对话框使用它来替换标签的“默认文本值”。
如何将用户文本值传递给第一个对话框?
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_()
答案 0 :(得分:4)
您可以从对话框发送信号并将其捕获到另一个对话框中。
使用pyqtSignal:
在发射对话框中定义信号class Modal(QDialog):
confirmed = pyqtSignal(str)
# ...
该信号有一个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
要捕获信号,另一个类需要一个插槽:
class Dialog(QDialog):
# ...
def changeText(self, t):
self.label.setText(t)
插槽功能将在其t
参数中接收文本,并相应地设置标签文本,但为此,信号和插槽必须连接。
首先,让我们编辑Modal
类构造函数,并删除最后两行:
self.setModal(True)
self.show()
在连接Dialog
广告位和showModal
changeText
信号后,让我们在Modal
的{{1}}中使用它们:
confirmed
答案 1 :(得分:1)
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_())
在确认中使用对话框的属性accept,在接受模态对话框之前,访问父对话框及其属性标签并设置其文本。我也使用了partial来传递带参数的Slot