Search code examples
pythoncheckboxpyqtpysideqgridlayout

PySide: Creating a list of checkboxes


I'm trying to create a list of checkboxes in PySide. These checkboxes will reside within a grid which is inside of a frame.

Because I need over a hundred checkboxes, I thought it would be best to store these checkboxes in a list. Within class Ui_MainWindow(object): there is def setupUi(self, MainWindow): and within that I'm calling my method myChanges with self.myChanges(MainWindow, self.customCheckBoxes, self.frame, self.gridLayout). Right above that I create an empty list to try to store the objects in with self.customCheckBoxes = []

Outside of the Ui_MainWindow class, I have a separate class named CreateCheckbox which tries to make a checkbox under a frame, set the object's name, add it to a spot in the grid and set it's text. From what I can tell, it can execute the first two perfectly fine, the problem arrises with the line self.grid.addWidget(self.checkBox, gridPlace, 1, 1, 1). More specifically, it has a problem with grid and throws this error: AttributeError: 'CreateCheckbox' object has no attribute 'grid'

My questions are:

  1. Am I using grid in the wrong way?
  2. Am i not allowed to use dots around grid when I pass it in?
  3. How do I fix this problem so the checkboxes all go down a single file line down the grid?
  4. Is my CreateCheckbox class or my myChanges method in the wrong place / where would I put them instead?

Edit: I think I found what I did wrong. in class CreateCheckbox, there should be no self. in self.grid.addWidget(self.checkBox, gridPlace, 1, 1, 1) because the grid is not an instance of the CreateCheckbox class

Edit 2: Just in case anyone wants to get the text working, put quotes around MainWindow in self.checkBox.setText(QtGui.QApplication.translate(MainWindow, text, None, QtGui.QApplication.UnicodeUTF8)) so you have self.checkBox.setText(QtGui.QApplication.translate("MainWindow", text, None, QtGui.QApplication.UnicodeUTF8))

Here's the full code:

from PySide import QtCore, QtGui


class Ui_MainWindow(object):
    def myChanges(self, MainWindow, checkboxes, frame, grid):
        for j in range(100):
            checkboxes.append(CreateCheckbox(MainWindow, frame, grid, "Test", j))

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.frame = QtGui.QFrame(self.centralwidget)
        self.frame.setGeometry(QtCore.QRect(180, 90, 371, 311))
        self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtGui.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.gridLayout_2 = QtGui.QGridLayout(self.frame)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.gridLayout = QtGui.QGridLayout()
        self.gridLayout.setObjectName("gridLayout")
        self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        # Create list holding checkbox objects
        self.customCheckBoxes = []
        self.myChanges(MainWindow, self.customCheckBoxes, self.frame, self.gridLayout)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))


class CreateCheckbox(object):
    def __init__(self, MainWindow, frame, grid, text, gridPlace):
        # 1. Create under appropriate frame
        self.checkBox = QtGui.QCheckBox(frame)
        # 2. Set its name
        #    Although the designer does this, pretty sure this is unneccesary
        self.checkBox.setObjectName(chr(gridPlace))
        # 3. Add it to the appropriate spot in the grid
        self.grid.addWidget(self.checkBox, gridPlace, 1, 1, 1)
        # 4. Set text that user sees
        # For now, I'm just sending 'Test'
        self.checkBox.setText(QtGui.QApplication.translate(MainWindow, text, None, QtGui.QApplication.UnicodeUTF8))

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Solution

  • The error is telling you that CreateCheckbox has no member named grid.

    I think that you meant to reference the grid variable that you passed into the class constructor (__init__)

    class CreateCheckbox(object):
        def __init__(self, MainWindow, frame, grid, text, gridPlace):
            #################
            self.grid = grid
            #################
    
            # 1. Create under appropriate frame
            self.checkBox = QtGui.QCheckBox(frame)
            # 2. Set its name
            #    Although the designer does this, pretty sure this is unneccesary
            self.checkBox.setObjectName(chr(gridPlace))
            # 3. Add it to the appropriate spot in the grid
            self.grid.addWidget(self.checkBox, gridPlace, 1, 1, 1)
            # 4. Set text that user sees
            # For now, I'm just sending 'Test'
            self.checkBox.setText(QtGui.QApplication.translate(MainWindow, text, None, QtGui.QApplication.UnicodeUTF8))