带按钮的PyQt动态添加选项卡 - 布局问题

时间:2015-01-15 20:41:12

标签: python pyqt pyqt4 qtabwidget qgridlayout

我正在尝试创建一个系统,根据产品类型自动添加标签,然后在相应的标签中自动添加按钮,但由于某些原因,所有标签都有与第一个标签,我很确定这与布局有关,但我不确定究竟是什么形象: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)

1 个答案:

答案 0 :(得分:1)

看起来您需要将布局添加到选项卡,而不是tabwidget:

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

        # typetab.setLayout(typetablayout)