PyQt: Accesing Main Window's Data from a dialo

2019-05-16 22:05发布

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. When the dialog opens, my Main Window freezes, and I don't really like that...

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

What can I do about these?

1条回答
乱世女痞
2楼-- · 2019-05-16 22:38

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.

查看更多
登录 后发表回答