自定义无模式对话框类不可见

时间:2018-05-29 15:00:52

标签: parent-child pyqt5 non-modal

我想创建一个带有标签和QDialogBu​​ttonBox的自定义非模态对话框类。我已经查看了一些帖子,但似乎没有一点真正有用。我的代码如下。两个问题: 1.为什么对话框类没有显示任何内容? 2. QDialogBu​​ttonBox的连接看起来是否正确?

任何帮助将不胜感激。谢谢!

from PyQt5.QtWidgets import *

class Window(QWidget):
    def __init__(self):
        QWidget.__init__(self)

        cb = QCheckBox('Check me anytime', self)
        cb.move(20, 20)

        button = QPushButton('Open Dialog', self)
        button.move(20,50)
        self.resize(120, 90)

        button.clicked.connect(self.showDialog)

    def showDialog(self):
        self.dialog = ModelessDialog(self)
        self.dialog.show()

class ModelessDialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        layout = QVBoxLayout()
        lbl = QLabel("please show something ...")
        buttonBox = QDialogButtonBox(
            QDialogButtonBox.Cancel|QDialogButtonBox.Apply)
        layout.addWidget(lbl)
        layout.addWidget(buttonBox)
        self.resize(300, 200)        

        applyBtn = buttonBox.button(QDialogButtonBox.Apply)
        applyBtn.clicked.connect(self.apply)

        cancelBtn = buttonBox.button(QDialogButtonBox.Cancel)
        cancelBtn.clicked.connect(ModelessDialog.reject)

        self.setWindowTitle("Modeless")

    def apply(self):
        print("ModelessDialog: in apply")

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

1 个答案:

答案 0 :(得分:0)

将此窗口小部件的布局管理器设置为布局。

self.setLayout(layout) 

试一试:

from PyQt5.QtWidgets import *

class Window(QWidget):
    def __init__(self):
        QWidget.__init__(self)

        cb = QCheckBox('Check me anytime', self)
        cb.move(20, 20)

        button = QPushButton('Open Dialog', self)
        button.move(20,50)
        self.resize(150, 90)

        button.clicked.connect(self.showDialog)

    def showDialog(self):
        self.dialog = ModelessDialog(self)
        self.dialog.show()

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

        layout = QVBoxLayout()
        lbl    = QLabel("please show something ...")
        buttonBox = QDialogButtonBox(
            QDialogButtonBox.Cancel|QDialogButtonBox.Apply)
        layout.addWidget(lbl)
        layout.addWidget(buttonBox)
        self.resize(300, 200)  
        # Sets the layout manager for this widget to layout .
        self.setLayout(layout)                              # +++    

        applyBtn = buttonBox.button(QDialogButtonBox.Apply)
        applyBtn.clicked.connect(self.apply)

        cancelBtn = buttonBox.button(QDialogButtonBox.Cancel)
        #cancelBtn.clicked.connect(ModelessDialog.reject)   # ---
        # first argument of unbound method must have type 'QDialog'
        cancelBtn.clicked.connect(self.reject)              # +++

        self.setWindowTitle("Modeless")

    def apply(self):
        print("ModelessDialog: in apply")

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

enter image description here