PyQt:从对话框访问主窗口的数据? [英] PyQt: Accesing Main Window's Data from a dialog?

查看:36
本文介绍了PyQt:从对话框访问主窗口的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So, I'm using Python and 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:

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

  1. When the dialog opens, my Main Window freezes, and I don't really like that...

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

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

我该怎么办?

推荐答案

您写道:

和一个模态打开的对话框

and a dialog that opens modally

然后:

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

When the dialog opens, my Main Window freezes

文档:

int QDialog::exec() [slot]

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

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.

关于无模式对话框:

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

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.

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

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.要搜索的文本将添加到主窗口的日志中.

Click 'Search' to open a search window (you can open several of them). Enter a text to search and press Enter. The text to search will be added to the log in the main window.

这篇关于PyQt:从对话框访问主窗口的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆