2013-10-24 44 views
2

我的目標是在單個QMainWindow以顯示10個或更多QTabWidget,每個選項卡保持一個唯一的QLabelQTableWidget。事情是這樣的:PyQt的多個tablewidgets和tabwidgets

enter image description here

即使我設法通過使用下面的代碼預期的結果,我想知道是否有更有效的方式或短的方式來達到同樣的效果。

Tab = QtGui.QTabWidget() 
    Tab1 = QtGui.QWidget() 
    Tab2 = QtGui.QWidget() 
    Tab3 = QtGui.QWidget() 
    Tab4 = QtGui.QWidget() 
    Tab5 = QtGui.QWidget() 
    Tab6 = QtGui.QWidget() 
    Tab7 = QtGui.QWidget() 
    Tab8 = QtGui.QWidget() 
    Tab9 = QtGui.QWidget() 
    Tab10 = QtGui.QWidget() 

    Tab.addTab(Tab1, QtCore.QString('SECTION A')) 
    Tab.addTab(Tab2, QtCore.QString('SECTION B')) 
    Tab.addTab(Tab3, QtCore.QString('SECTION C')) 
    Tab.addTab(Tab4, QtCore.QString('SECTION D')) 
    Tab.addTab(Tab5, QtCore.QString('SECTION E')) 
    Tab.addTab(Tab6, QtCore.QString('SECTION F')) 
    Tab.addTab(Tab7, QtCore.QString('SECTION G')) 
    Tab.addTab(Tab8, QtCore.QString('SECTION H')) 
    Tab.addTab(Tab9, QtCore.QString('SECTION I')) 
    Tab.addTab(Tab10, QtCore.QString('SECTION J'))  

    title1 = QtGui.QLabel('title') 
    title2 = QtGui.QLabel('title') 
    title3 = QtGui.QLabel('title') 
    title4 = QtGui.QLabel('title') 
    title5 = QtGui.QLabel('title') 
    title6 = QtGui.QLabel('title') 
    title7 = QtGui.QLabel('title') 
    title8 = QtGui.QLabel('title') 
    title9 = QtGui.QLabel('title') 
    title10 = QtGui.QLabel('title') 

    self.Table1 = QtGui.QTableWidget() 
    self.Table2 = QtGui.QTableWidget() 
    self.Table3 = QtGui.QTableWidget() 
    self.Table4 = QtGui.QTableWidget() 
    self.Table5 = QtGui.QTableWidget() 
    self.Table6 = QtGui.QTableWidget() 
    self.Table7 = QtGui.QTableWidget() 
    self.Table8 = QtGui.QTableWidget() 
    self.Table9 = QtGui.QTableWidget() 
    self.Table10 = QtGui.QTableWidget() 

    Layout1 = QtGui.QVBoxLayout() 
    Layout2 = QtGui.QVBoxLayout() 
    Layout3 = QtGui.QVBoxLayout() 
    Layout4 = QtGui.QVBoxLayout() 
    Layout5 = QtGui.QVBoxLayout() 
    Layout6 = QtGui.QVBoxLayout() 
    Layout7 = QtGui.QVBoxLayout() 
    Layout8 = QtGui.QVBoxLayout() 
    Layout9 = QtGui.QVBoxLayout() 
    Layout10 = QtGui.QVBoxLayout() 

    tablist = [Tab1,Tab2,Tab3,Tab4,Tab5,Tab6,Tab7,Tab8,Tab9,Tab10] 
    tablabellist = [title1,title2,title3,title4,title5,title6,title7,title8,title9,title10]  
    self.tablelist = [self.Table1,self.Table2,self.Table3,self.Table4,self.Table5,self.Table6,self.Table7,self.Table8,self.Table9,self.Table10] 
    layoutlist = [Layout1,Layout2,Layout3,Layout4,Layout5,Layout6,Layout7,Layout8,Layout9,Layout10]  
    headerlist = [ 'ID','Question','Answer 1','Answer 2','Answer 3','Difficulty','Statistics','Date Added','Added By','Date Modified'] 

    for item in self.tablelist: 
     item.setColumnCount(len(headerlist)) 
     item.setHorizontalHeaderLabels(headerlist) 
     item.setEditTriggers(QtGui.QTableWidget.NoEditTriggers) 
     item.setSelectionBehavior(QtGui.QTableWidget.SelectRows) 
     item.setSelectionMode(QtGui.QTableWidget.SingleSelection) 

    for i in range (len(layoutlist)): 
     layoutlist[i].addWidget(tablabellist[i]) 
     layoutlist[i].addWidget(self.tablelist[i]) 
     tablist[i].setLayout(layoutlist[i]) 

    CLayout = QtGui.QVBoxLayout() 
    CLayout.addWidget(Tab) 

    Cwidget = QtGui.QWidget() 
    Cwidget.setLayout(CLayout)  
    self.setCentralWidget(Cwidget) 

如果我是添加20 TabWidget我上面使用的相同的方式,這將是非常混亂和冗長。

是否可以縮短我的代碼,同時保持相同的輸出?

回答

4

像這樣的東西應該這樣做:

tablist = [] 
tablabellist = [] 
layoutlist=[] 
self.tablelist = [] 
Tab = QtGui.QTabWidget() 
headerlist = [ 'ID','Question','Answer 1','Answer 2','Answer 3','Difficulty','Statistics','Date Added','Added By','Date Modified'] 

num_tab_widgets = 10 

for i in range(num_tab_widgets): 
    tablist.append(QtGui.QWidget()) 
    Tab.addTab(tablist[i], QtCore.QString('SECTION %s'%chr(ord('A')+i))) 
    tablabellist.append(QtGui.QLabel('title')) 
    self.tablelist.append(QtGui.QTableWidget()) 
    setattr(self,'Table%d'%i,self.tablelist[i]) 
    layoutlist.append(QtGui.QVBoxLayout()) 

    self.tablelist[i].setColumnCount(len(headerlist)) 
    self.tablelist[i].setHorizontalHeaderLabels(headerlist) 
    self.tablelist[i].setEditTriggers(QtGui.QTableWidget.NoEditTriggers) 
    self.tablelist[i].setSelectionBehavior(QtGui.QTableWidget.SelectRows) 
    self.tablelist[i].setSelectionMode(QtGui.QTableWidget.SingleSelection) 

    layoutlist[i].addWidget(tablabellist[i]) 
    layoutlist[i].addWidget(self.tablelist[i]) 
    tablist[i].setLayout(layoutlist[i]) 

CLayout = QtGui.QVBoxLayout() 
CLayout.addWidget(Tab) 

Cwidget = QtGui.QWidget() 
Cwidget.setLayout(CLayout)  
self.setCentralWidget(Cwidget) 

注意,它可能沒有必要救你在列表中所擁有的一切,或作爲實例(個體經營)的一部分,但我不懷疑它因爲我不知道你的應用程序的其他部分是幹什麼的!希望這給你足夠的信息來自己簡化它。