views:

75

answers:

2

I have a simple GUI built using python and PyQt4. 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. 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. I have tried reading through the PyQt4 documentation for all of this, but I have struggled to make any progress.

Here is how I am making the initial checkbox (basic output from 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.

+1  A: 

I think you're making life hard for yourself by copying QtCreator's output style. 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_()
xioxox
A: 

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). 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

    • Sadly, the window didn't resize with the checkboxes, so I wrote a function like this to fix it:

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)
bball