2015-01-15 94 views
0

我試圖創建一個系統,會根據有多少產品類型自動添加選項卡,然後自動添加按鈕在相應的選項卡中的項目,但由於某種原因所有的選項卡具有相同的按鈕,第一個選項卡,我敢肯定,這事做的佈局,但我不知道究竟是什麼形象:enter image description herePyQt動態添加按鈕的選項卡 - 佈局問題

 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) 
+0

由於問題解決了,錯誤是一個簡單的錯誤,不太可能幫助別人,所以我建議關閉這個問題。 – Trilarion

回答

1

它看起來像你需要添加布局到標籤,而不是tabwidget:

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

     # typetab.setLayout(typetablayout) 
+0

謝謝!這工作! – Inthuson