[英]Dynamically adding checkboxes with PyQt4
I have a simple GUI built using python and PyQt4. 我有一个使用python和PyQt4构建的简单GUI。 After the user enters something into the program, the program should then add a certain number of checkboxes to the UI depending on what the user's input was.
用户在程序中输入内容后,程序应根据用户输入的内容向UI添加一定数量的复选框。 For testing purposes, I have one checkbox existing in the application from start, and that checkbox is nested inside of a QVBoxLayout, which is nested inside of a QGroupBox.
出于测试目的,我从一开始就在应用程序中存在一个复选框,该复选框嵌套在QVBoxLayout中,而QVBoxLayout嵌套在QGroupBox中。 I have tried reading through the PyQt4 documentation for all of this, but I have struggled to make any progress.
我已经尝试通读PyQt4文档,但是我一直在努力取得任何进展。
Here is how I am making the initial checkbox (basic output from QtCreator): 这是我制作初始复选框(QtCreator的基本输出)的方式:
self.CheckboxField = QtGui.QGroupBox(self.GuiMain)
self.CheckboxField.setGeometry(QtCore.QRect(10, 70, 501, 41))
self.CheckboxField.setObjectName("CheckboxField")
self.verticalLayoutWidget = QtGui.QWidget(self.CheckboxField)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(0, 10, 491, 21))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.CheckboxLayout = QtGui.QVBoxLayout(self.verticalLayoutWidget)
self.CheckboxLayout.setSizeConstraint(QtGui.QLayout.SetMinimumSize)
self.CheckboxLayout.setObjectName("CheckboxLayout")
self.checkBox = QtGui.QCheckBox(self.verticalLayoutWidget)
self.checkBox.setObjectName("checkBox")
self.CheckboxLayout.addWidget(self.checkBox)
Then here is my initial attempt to add a new checkbox (in a seperate file): 然后,这是我最初的尝试(在单独的文件中)添加一个新复选框:
checkBox1 = QtGui.QCheckBox(self.window.CheckboxField)
checkBox1.setGeometry(QtCore.QRect(90, 10, 70, 17))
checkBox1.setText(QtGui.QApplication.translate("MainWindow", "Bob Oblaw", None, QtGui.QApplication.UnicodeUTF8))
checkBox1.setObjectName("checkBox1")
self.window.CheckboxLayout.addWidget(checkBox1)
self.window.CheckboxLayout.stretch(1)
self.window.CheckboxField.adjustSize()
self.window.CheckboxField.update()
There are no errors, the checkbox just doesn't show up. 没有错误,只是不显示该复选框。
I think you're making life hard for yourself by copying QtCreator's output style. 我认为您通过复制QtCreator的输出样式使自己的生活变得艰难。 I think it's important to manually code some UIs to see how it works.
我认为手动编码一些用户界面以查看其工作方式非常重要。 I suspect you're not adding the check box to the layout.
我怀疑您没有在布局中添加复选框。 Try something this (Import * used for clarity here):
尝试以下操作(在此处输入*以简化说明):
import sys from PyQt4.QtGui import * from PyQt4.QtCore import * class Window(QWidget): def __init__(self): QWidget.__init__(self) layout = QVBoxLayout() self.checks = [] for i in xrange(5): c = QCheckBox("Option %i" % i) layout.addWidget(c) self.checks.append(c) self.setLayout(layout) if __name__ == '__main__': app = QApplication(sys.argv) w = Window() w.show() app.exec_()
I ended up figuring it out myself. 我最终自己弄清楚了。 Part of it was my fault, and the other part is a little bit hacky (seeing as it probably doesn't use a Qt function it could be using).
部分原因是我的错,另一部分则有点hacky(看到它可能未使用可能正在使用的Qt函数)。 Here is my solution:
这是我的解决方案:
First, I needed to lay everything out on a grid layout, this made it so my check marks started showing up when I added them 首先,我需要将所有内容布置在网格布局上,这样就可以在添加它们时开始显示我的选中标记
def addCheckbox(self, name):
checkBox = QtGui.QCheckBox(self.window.CheckboxField)
self.window.CheckboxLayout.addWidget(checkBox)
checkBox.setText(name)
newHeight = self.geometry().height()+21#Compensate for new checkbox
self.resize(self.geometry().width(), newHeight)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.