[SOLVED] TypeError: 1st argument of QGridLayout.addWidget() must be a QGridLayout instance?
Hi, I am going through the zetcode tutorial on layout management (http://zetcode.com/tutorials/pyqt4/layoutmanagement/) and I am getting a Type Error trying to use QGridLayout. I am using PyQt 4.4.4 and upgrading my version is not an option for a lot of annoying reasons that I cant control.
names = ['Cls','Bck','','Close','7','8','9','/',
grid = QtGui.QGridLayout j = 0 pos = [(0,0),(0,1),(0,2),(0,3), (1,0),(1,1),(1,2),(1,3), (2,0),(2,1),(2,2),(2,3), (3,0),(3,1),(3,2),(3,3), (4,0),(4,1),(4,2),(4,3)] for i in names: print i, j button = QtGui.QPushButton(i) if j == 2: grid.addWidget(QtGui.QLabel(''),0,2) else: grid.addWidget(button,pos[j],pos[j]) j = j + 1 self.setLayout(grid) self.move(300,150) self.setWindowtitle('Calculator') self.show()
When this runs I get an error: TypeError: first argument of unbound method QGridLayout.addWidget() must be a QGridLayout instance. If I set j = 2 to force
to execute I get the same error.
I have no idea what this means. I haven't been able to find anyone on this or any other forum with the same error. My first argument is 'button' and it's a QPushButton instance. I've seen plenty of examples of people who've done very similar things. There is nothing in the documentation that suggests that the first argument must be a QGridLayout instance, only that it is a QWidget, which the QPushButton is.
Is this a problem with the way I've declared 'grid'?
Does QGridLayout create an matrix of boxes? Should I be iterating over grid as well as over names and pos?
IIRC from my pythonic days, your grid object is not an instance of QGridLayout. You are simply missing a set of parenthesis
@grid = QtGui.QGridLayout()@
And you should be good
I think creating widgets in stack memory is not a good idea especially if you have many of them.
I always create widgets in heap & manage the memory using QList<QWidget *>.
Personal preference though.
Python and C++ do not handle data the same way. In python, there is no pointer.
oops didnt know that ;)
Thanks, @SGaist! Of course it would be something embarrassing like that once I've wracked my brains and finally decided to post!
You're welcome !
Don't forget to set the thread's title as solved