隐藏后如何重新显示QDialog?

时间:2013-01-09 13:56:12

标签: python pyqt qdialog

我正在使用python和pyqt。我有一个我想暂时隐藏的对话框。致电后

dlg.hide()

我试着打电话

dlg.show()

但没有任何反应。它永远不会重新显示。

我是pyqt的新手,所以非常感谢任何帮助。

提前致谢。

1 个答案:

答案 0 :(得分:0)

您正在寻找使对话框模态化的exec_方法,看看它是如何工作的:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PyQt4 import QtCore, QtGui

class myDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(myDialog, self).__init__(parent)

        self.dialog = None

        self.buttonShow = QtGui.QPushButton(self)
        self.buttonShow.setText("Show Dialog")
        self.buttonShow.clicked.connect(self.on_buttonShow_clicked)

        self.buttonHide = QtGui.QPushButton(self)
        self.buttonHide.setText("Close")
        self.buttonHide.clicked.connect(self.on_buttonHide_clicked)

        self.layout = QtGui.QVBoxLayout(self)
        self.layout.addWidget(self.buttonShow)
        self.layout.addWidget(self.buttonHide)

    @QtCore.pyqtSlot()
    def on_buttonHide_clicked(self):
        self.accept()

    @QtCore.pyqtSlot()
    def on_buttonShow_clicked(self):
        self.dialog = myDialog(self)
        self.dialog.exec_()

class myWindow(QtGui.QWidget):  
    def __init__(self, parent=None):
        super(myWindow, self).__init__(parent)

        self.buttonShow = QtGui.QPushButton(self)
        self.buttonShow.setText("Show Dialog")
        self.buttonShow.clicked.connect(self.on_buttonShow_clicked)

        self.layout = QtGui.QVBoxLayout(self)
        self.layout.addWidget(self.buttonShow)

        self.dialog = myDialog(self)

    @QtCore.pyqtSlot()
    def on_buttonHide_clicked(self):
        self.dialog.accept()

    @QtCore.pyqtSlot()
    def on_buttonShow_clicked(self):
        self.dialog.exec_()

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('myWindow')

    main = myWindow()
    main.show()

    sys.exit(app.exec_())