Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] TypeError: 1st argument of QGridLayout.addWidget() must be a QGridLayout instance?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] TypeError: 1st argument of QGridLayout.addWidget() must be a QGridLayout instance?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 6.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SatelliteEyes
    wrote on last edited by
    #1

    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','/',
    '4','5','6','*','1','2','3','-',
    '0','.','=','+']

          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][0],pos[j][1])
            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

    @
    grid.addWidget(QtGui.QLabel(''),0,2)
    @

    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?
    Thoughts?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • CAD_codingC Offline
        CAD_codingC Offline
        CAD_coding
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @CAD_coding
          Python and C++ do not handle data the same way. In python, there is no pointer.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • CAD_codingC Offline
            CAD_codingC Offline
            CAD_coding
            wrote on last edited by
            #5

            oops didnt know that ;)

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SatelliteEyes
              wrote on last edited by
              #6

              Thanks, @SGaist! Of course it would be something embarrassing like that once I've wracked my brains and finally decided to post!

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved