gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:49:46 25 4
gpt4 key购买 nike

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

  1. 当对话框打开时,我的主窗口卡住了,我不太喜欢这样...

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

我该怎么办?

最佳答案

你写道:

and a dialog that opens modally

然后:

When the dialog opens, my Main Window freezes

文档 say :

int QDialog::exec () [slot]

Shows the dialog as a modal dialog, blocking until the user closes it. The function returns a DialogCode result. 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.

关于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.

一个例子:

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()

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

关于python - PyQt:从对话框访问主窗口的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11920401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com