Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

python - How to add table widget in a QtoolBox item?

I'm working in a simple gui with a QtoolBox can increase the item dynamically (with button "add item"). I need to add, for each item, a new widget like table (for example TableView class) but I can't understand how can I do it. Could anybody help me? (follow the code I wrote until now)

from PyQt5.QtWidgets import *
import sys

data = {'col1':['1','2','3','4'],
        'col2':['1','2','1','3'],
        'col3':['1','1','2','1']}


class TableView(QTableWidget):
    def __init__(self, data, *args):
        QTableWidget.__init__(self, *args)
        self.data = data
        self.setData()
        self.resizeColumnsToContents()
        self.resizeRowsToContents()
 
    def setData(self): 
        horHeaders = []
        for n, key in enumerate(sorted(self.data.keys())):
            horHeaders.append(key)
            for m, item in enumerate(self.data[key]):
                newitem = QTableWidgetItem(item)
                self.setItem(m, n, newitem)
        self.setHorizontalHeaderLabels(horHeaders)


class Window(QWidget):

    def __init__(self):
        super().__init__()
        self.layout = QGridLayout()
        self.setLayout(self.layout)
        self.numAddWidget = 1
        self.initUi()
        # Add toolbar and items
        
    def initUi(self):
        self.add_button = QPushButton("Add Widget")
        self.layout.addWidget(self.add_button, 0, 0)
        self.add_button.clicked.connect(self.addPage)
        self.toolbox = QToolBox()
        self.layout.addWidget(self.toolbox, 1, 0)
        
    def addPage(self):
        self.numAddWidget += 1
        label = QLabel()
        a=self.toolbox.addItem(label, "item {}".format(self.numAddWidget))
        self.table=TableView(data, 4, 3)
        self.toolbox.addWidget(self.table)
        print(a)

app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())
question from:https://stackoverflow.com/questions/65868831/how-to-add-table-widget-in-a-qtoolbox-item

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...