[英]PyQt: Accesing Main Window's Data from a dialog?
So, I'm using Python and PyQt. 所以,我正在使用Python和PyQt。 I have a Main Window that contains a QTableWidget, and a dialog that opens modally and has some QLineEdit widgets... All right so far, but I have 2 problems:
我有一个包含QTableWidget的主窗口,以及一个以模态方式打开并有一些QLineEdit小部件的对话框......好了到目前为止,但我有两个问题:
When the dialog opens, my Main Window freezes, and I don't really like that... 对话框打开后,我的主窗口冻结了,我真的不喜欢那样......
What I want, when I finish editing a QLineEdit, is that the program will search the QTableWidget, and if the text from the QLineEdit exists in the table, a dialog will come up and informe about that. 当我完成编辑QLineEdit时,我想要的是程序将搜索QTableWidget,如果表中存在QLineEdit中的文本,则会出现一个对话框,并提供相关信息。 That's the general idea.
这是一般的想法。 But, so far, I seem to only be able to create a new QTableWidget instance, and I can't use the data from the existing...
但是,到目前为止,我似乎只能创建一个新的QTableWidget实例,而我无法使用现有的数据...
What can I do about these? 我能做些什么呢?
You wrote: 你写了:
and a dialog that opens modally
和一个以模态方式打开的对话框
and then: 然后:
When the dialog opens, my Main Window freezes
对话框打开后,我的主窗口冻结
int QDialog::exec () [slot]
Shows the dialog as a modal dialog, blocking until the user closes it.
将对话框显示为模式对话框,阻止直到用户关闭它。 The function returns a
DialogCode
result.该函数返回
DialogCode
结果。 If the dialog is application modal, users cannot interact with any other window in the same application until they close the dialog.如果对话框是应用程序模式,则用户在关闭对话框之前无法与同一应用程序中的任何其他窗口进行交互。
If the dialog is window modal, only interaction with the parent window is blocked while the dialog is open.
如果对话框是窗口模式,则在对话框打开时仅阻止与父窗口的交互。 By default, the dialog is application modal.
默认情况下,对话框是应用程序模式。
About modeless dialogs : 关于无模式对话框 :
A modeless dialog is a dialog that operates independently of other windows in the same application.
无模式对话框是一个独立于同一应用程序中其他窗口的对话框。 Find and replace dialogs in word-processors are often modeless to allow the user to interact with both the application's main window and with the dialog.
在字处理器中查找和替换对话框通常是无模式的,以允许用户与应用程序的主窗口和对话框进行交互。
Modeless dialogs are displayed using
show()
, which returns control to the caller immediately.使用
show()
显示无模式对话框,它会立即将控制权返回给调用者。
An example: 一个例子:
import sys
from PyQt4 import QtCore, QtGui
class SearchDialog(QtGui.QDialog):
def __init__(self, parent = None):
QtGui.QDialog.__init__(self, parent)
self.setWindowTitle('Search')
self.searchEdit = QtGui.QLineEdit()
layout = QtGui.QVBoxLayout()
layout.addWidget(self.searchEdit)
self.setLayout(layout)
class MainWindow(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self, None)
self.resize(QtCore.QSize(320, 240))
self.setWindowTitle('Main window')
self.logText = QtGui.QPlainTextEdit()
searchButton = QtGui.QPushButton('Search')
layout = QtGui.QVBoxLayout()
layout.addWidget(self.logText)
layout.addWidget(searchButton)
self.setLayout(layout)
searchButton.clicked.connect(self.showSearchDialog)
def showSearchDialog(self):
searchDialog = SearchDialog(self)
searchDialog.show()
searchDialog.searchEdit.returnPressed.connect(self.onSearch)
def onSearch(self):
self.logText.appendPlainText(self.sender().text())
def main():
app = QtGui.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
app.exec_()
if __name__ == "__main__":
main()
Click 'Search' to open a search window (you can open several of them). 单击“搜索”以打开搜索窗口(您可以打开其中几个)。 Enter a text to search and press Enter.
输入要搜索的文本,然后按Enter键。 The text to search will be added to the log in the main window.
要搜索的文本将添加到主窗口中的日志中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.