简体   繁体   English

隐藏它后如何重新显示QDialog?

[英]How to re-display a QDialog after hiding it?

I am working with python and pyqt. 我正在使用python和pyqt。 I have a dialog that I want to temporarily hide. 我有一个对话框要暂时隐藏。 After calling 打电话后

dlg.hide()

I try calling 我尝试打电话

dlg.show()

but nothing happens. 但是什么也没发生。 It is never re-displayed. 它永远不会重新显示。

I am new to pyqt so any help is greatly appreciated. 我是pyqt的新手,因此非常感谢您的帮助。

Thanks in advance. 提前致谢。

You are looking for the exec_ method that makes the dialog modal, see how this works: 您正在寻找使对话框exec_模态的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_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM