Inthu
Inthu

Reputation: 1029

PyQt dynamic adding tab with buttons - layout issue

I'm trying to create a system that will automatically add tabs according to how many product types there are and then automatically add buttons within their corresponding tab for the items, but for some reason all the tabs have the same button as the first tab, I'm pretty sure this has something to do with the layout but I'm not sure what exactly image: enter image description here

        typetab = QtGui.QTabWidget(self)
        types = producttypes() ##returns a tuple with type names e.g. [('Drinks',), ('Food',)]

        for name in types:
            tab = QtGui.QWidget()
            typetab.addTab(tab, name[0])
            products = typeitems(name[0]) ## returns items of that product type [('Coke',), ('Pepsi',)]
            typetablayout = QtGui.QGridLayout()
            for length in range(math.floor(len(products)/5) + 1):
                for width in range(5):
                    try:
                        button = QtGui.QPushButton(products[width][0])
                        button.setObjectName(products[width][0])
                        typetablayout.addWidget(button,length, width)
                    except IndexError:
                        break
                    print([length,width])
            typetab.setLayout(typetablayout)

Upvotes: 0

Views: 4857

Answers (1)

ekhumoro
ekhumoro

Reputation: 120768

It looks like you need to add the layout to the tab, rather than the tabwidget:

    for name in types:
        tab = QtGui.QWidget()
        typetab.addTab(tab, name[0])
        typetablayout = QtGui.QGridLayout(tab)
        ...

        # typetab.setLayout(typetablayout)

Upvotes: 1

Related Questions