所以,我正在使用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 aDialogCode
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.Modeless
dialogs are displayed usingshow()
, 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键.要搜索的文本将添加到主窗口中的日志中.