自定义无模式对话框 class 不可见
Custom modeless dialog class not visible
我想制作一个带有标签和 QDialogButtonBox 的自定义非模式对话框 class。我看过很多帖子,但 none 似乎很正确。我的代码如下。两个问题:
1. 为什么对话框 class 不显示任何内容?
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_())
将此小部件的布局管理器设置为 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_())
我想制作一个带有标签和 QDialogButtonBox 的自定义非模式对话框 class。我看过很多帖子,但 none 似乎很正确。我的代码如下。两个问题: 1. 为什么对话框 class 不显示任何内容? 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_())
将此小部件的布局管理器设置为 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_())