ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-08-26 06:59:13  阅读:372  来源: 互联网

标签:qdialog python pyqt qmainwindow


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

>当对话框打开时,我的主窗口冻结,我真的不喜欢……
>当我完成编辑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键.要搜索的文本将添加到主窗口中的日志中.

标签:qdialog,python,pyqt,qmainwindow
来源: https://codeday.me/bug/20190826/1727371.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有