PyQt:从对话框中访问主窗口的数据?

Ant*_*040 3 python pyqt qdialog qmainwindow

所以,我正在使用Python和PyQt.我有一个包含QTableWidget的主窗口,以及一个以模态方式打开并具有一些QLineEdit小部件的对话框......好了到目前为止,但我有两个问题:

  1. 对话框打开后,我的主窗口冻结了,我真的不喜欢那样......

  2. 当我完成编辑QLineEdit时,我想要的是程序将搜索QTableWidget,如果表中存在QLineEdit中的文本,则会出现一个对话框,并提供相关信息.这是一般的想法.但是,到目前为止,我似乎只能创建一个新的QTableWidget实例,而我无法使用现有的数据...

我能做些什么呢?

war*_*iuc 6

你写了:

和一个以模态方式打开的对话框

然后:

对话框打开后,我的主窗口冻结

文档:

int QDialog::exec () [slot]

将对话框显示为模式对话框,阻止直到用户关闭它.该函数返回一个DialogCode 结果.如果对话框是应用程序模式,则用户在关闭对话框之前无法与同一应用程序中的任何其他窗口进行交互.

如果对话框是窗口模式,则在对话框打开时仅阻止与父窗口的交互.默认情况下,对话框是应用程序模式.

关于无模式对话框:

无模式对话框是一个独立于同一应用程序中其他窗口的对话框.在字处理器中查找和替换对话框通常是无模式的,以允许用户与应用程序的主窗口和对话框进行交互.

使用模式显示无模式对话框show(),它立即将控制权返回给调用者.

一个例子:

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()
Run Code Online (Sandbox Code Playgroud)

单击"搜索"以打开搜索窗口(您可以打开其中几个).输入要搜索的文本,然后按Enter键.要搜索的文本将添加到主窗口中的日志中.