why sub-dialog can't display after use setWindowFlags?

Viewed 203

I want to remove the flag which show help about dialog in top-right.** So I wrote code following:

from PyQt5.QtWidgets import QWidget, QDialog, QApplication, QVBoxLayout
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import Qt

class ChildWidget(QDialog):
    def __init__(self,parent=None):
        super().__init__(parent)
        self.resize(500,500)
        self.setWindowTitle('Child')
        self.setWindowFlags(Qt.CustomizeWindowHint|Qt.WindowCloseButtonHint)


class ParentWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.resize(600,600)
        self.setWindowTitle('Parent')
        self.btn = QPushButton('show childDialog')
        self.child_widget = ChildWidget(self)
        layout = QVBoxLayout()
        layout.addWidget(self.btn)
        self.setLayout(layout)
        self.btn.clicked.connect(self.onClickedBtn)

    def onClickedBtn(self):
        self.child_widget.show()





if __name__=='__main__':
    import sys
    app = QApplication(sys.argv)
    w = ParentWidget()
    w.show()
    sys.exit(app.exec_())

The sub-dialog can't display When I clicked button.

Could you tell me the reason ?

1 Answers

A widget to be a window must meet at least one of the following characteristics:

  • Not have a parent, OR
  • Have the Qt::Window flag enabled, OR
  • Have the Qt::Dialog flag enabled, OR
  • Have the Qt::Tool flag enabled.

In your case you do not meet any reason why it will not be a window but part of a widget. the best solution is to enable the Qt::Dialog flag:

class ChildWidget(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.resize(500, 500)
        self.setWindowTitle("Child")
        self.setWindowFlags(
            Qt.Dialog | Qt.CustomizeWindowHint | Qt.WindowCloseButtonHint
        )
Related