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

    def initUI(self):
    names = ['Cls','Bck','','Close','7','8','9','/',

          grid = QtGui.QGridLayout
          j = 0
          pos = [(0,0),(0,1),(0,2),(0,3),
          for i in names:
            print i, j
            button = QtGui.QPushButton(i)
            if j == 2:
            j = j + 1


    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?

  • Lifetime Qt Champion


    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.

  • Lifetime Qt Champion

    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!

  • Lifetime Qt Champion

    You're welcome !
    Don't forget to set the thread's title as solved

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.