在PyQt列表中添加标题

7
我希望能在PyQt中为列表添加标题和索引,无论是QT的哪个列表(qlistwidget、qlistview、qtablewidget、qtreeview)都不重要。简而言之,我想要像PyQt演示中旋转框委托示例一样的东西...但是我希望列标题中的索引是字符串而不是数字。希望这个想法足够清晰明了。谢谢!
1个回答

9

QTableWidget可能是您的最佳选择 - 它使用setHorizontalHeaderLabels()setVerticalHeaderLabels()让您控制两个轴。

from PyQt4 import QtGui

class MyWindow(QtGui.QMainWindow):

    def __init__(self, parent):
        QtGui.QMainWindow.__init__(self, parent)

        table = QtGui.QTableWidget(3, 3, self)  # create 3x3 table
        table.setHorizontalHeaderLabels(('Col 1', 'Col 2', 'Col 3'))
        table.setVerticalHeaderLabels(('Row 1', 'Row 2', 'Row 3'))
        for column in range(3):
            for row in range(3):
                table.setItem(row, column, QtGui.QWidget(self))  # your contents

        self.setCentralWidget(table)
        self.show()

当然,如果你想完全控制标题的内容和格式,则可以使用 .setHorizontalHeaderItem() 和 .setVerticalHeaderItem() 方法为每个标题定义一个 QTableWidgetItem...
请查看官方文档以获取完整细节。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接