PyQt多个tablewidgets和tabwidgets [英] PyQt multiple tablewidgets and tabwidgets

查看:112
本文介绍了PyQt多个tablewidgets和tabwidgets的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在单个QMainWindow中显示10个或更多的QTabWidget,每个选项卡均包含唯一的QLabelQTableWidget.像这样:

My objective is to display 10 or more QTabWidget in a single QMainWindow, each tab holding a unique QLabel and QTableWidget. Something like this:

尽管我通过使用以下代码设法获得了预期的结果,但我想知道是否有更有效的方法或更短的方法来实现相同的结果.

Even though i managed to get the intended result by using the following code, i am wondering if there is more efficient way or shorter way to achieve the same result.

    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,那将非常混乱且乏味.

If i were to add 20 TabWidget the same way i used above, it will be very messy and tedious.

是否可以在保持相同输出的同时缩短代码?

Is is possible to shorten my code while maintaining the same output?

推荐答案

应该执行以下操作:

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)

请注意,可能没有必要将您拥有的所有内容保存在列表中或作为实例(自身)的一部分保存,但是我并不是在质疑它,因为我不知道您的应用程序的其余部分会做什么!希望这可以为您提供足够的信息,以便您自己进行精简.

Note that it may not be necessary to save everything you have in lists, or as part of the instance (self), but I'm not questioning it because I don't know what the rest of your application does! Hopefully this gives you enough information to streamline it yourself.

这篇关于PyQt多个tablewidgets和tabwidgets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆