0

我想制作一个带有标签和 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_())
4

1 回答 1

0

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

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_())

在此处输入图像描述

于 2018-05-29T17:18:36.710 回答