Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding a header to pyqt list

i want to add a headers and index to a list in pyqt , it's really not important what list of QT (qlistwidget , qlistview , qtablewidget, qtreeview)

in short .. i want something like the spin box delegate example in the pyqt demo ... but instead of the index in the column headers i want a strings ...



hope the idea is clear enough
thanx in advance

like image 978
Moayyad Yaghi Avatar asked May 01 '10 10:05

Moayyad Yaghi


1 Answers

QTableWidget is likely your best choice - it uses setHorizontalHeaderLabels() and setVerticalHeaderLabels() to let you control both axes.

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()

Of course, if you want full control over the contents and formatting of the headers, then you could use the .setHorizontalHeaderItem() and .setVerticalHeaderItem() methods to define a QTableWidgetItem for each header...

See the official documentation for full details.

like image 160
Rini Avatar answered Oct 12 '22 01:10

Rini