PyQt: Accesing Main Window's Data from a dialog?

╄→гoц情女王★ 提交于 2019-12-06 03:53:11

You wrote:

and a dialog that opens modally

and then:

When the dialog opens, my Main Window freezes

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

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.

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. The text to search will be added to the log in the main window.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!