我想制作一个带有标签和 QDialogButtonBox 的自定义非模态对话框类。我看过很多帖子,但似乎没有一个是真正的。我的代码如下。两个问题: 1. 为什么对话框类不显示任何内容?2. QDialogButtonBox 的连接看起来正确吗?
任何帮助,将不胜感激。谢谢!
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_())