I am trying to create a checklist in PySide. These flags will be inside the grid, which is inside the frame.
Since I need more than a hundred flags, I thought it was best to keep these flags in a list. There class Ui_MainWindow(object):is def setupUi(self, MainWindow):inside, and inside this I call my method myChangeswith self.myChanges(MainWindow, self.customCheckBoxes, self.frame, self.gridLayout). Right above this, I am creating an empty list to try to save objects withself.customCheckBoxes = []
Outside the class Ui_MainWindow, I have a separate class with a name CreateCheckboxthat tries to check the box under the frame, set the name of the object, add it to the spot in the grid and set its text. From what I can say, he can perform the first two perfectly, a problem arises with the line self.grid.addWidget(self.checkBox, gridPlace, 1, 1, 1). In particular, the problem with gridand causes this error:AttributeError: 'CreateCheckbox' object has no attribute 'grid'
My questions:
- Do I use the grid in any other way?
- Am I not allowed to use the dots around the grid when I pass it?
- How to fix this problem so that all the flags go down one line of the file on the grid?
- Is my class
CreateCheckboxor my myChangesmethod in the wrong place / where would I put them?
Edit: I think I found what I did wrong. in class CreateCheckboxshould not be self.in self.grid.addWidget(self.checkBox, gridPlace, 1, 1, 1), because the grid is not an instance of the classCreateCheckbox
Edit 2: just in case someone wants the text to work put quotation marks around MainWindowin
self.checkBox.setText(QtGui.QApplication.translate(MainWindow, text, None, QtGui.QApplication.UnicodeUTF8))so you
self.checkBox.setText(QtGui.QApplication.translate("MainWindow", text, None, QtGui.QApplication.UnicodeUTF8))
Here is 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)
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):
self.checkBox = QtGui.QCheckBox(frame)
self.checkBox.setObjectName(chr(gridPlace))
self.grid.addWidget(self.checkBox, gridPlace, 1, 1, 1)
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_())