alphanumeric
alphanumeric

Reputation: 19329

How to stretch QTableView last column header

The code below creates QTableView with a single column. How to make the header column stretch along the entire width of the QTableView view?

enter image description here

from PyQt4 import QtCore, QtGui
app=QtGui.QApplication(sys.argv)

class TableModel(QtCore.QAbstractTableModel):
    def __init__(self):
        QtCore.QAbstractTableModel.__init__(self)
    def rowCount(self, parent=QtCore.QModelIndex()):   
        return 0
    def columnCount(self, index=QtCore.QModelIndex()):
        return 1
    def headerData(self, column, orientation, role=QtCore.Qt.DisplayRole):
        if role!=QtCore.Qt.DisplayRole:   return QtCore.QVariant()
        if orientation==QtCore.Qt.Horizontal: return QtCore.QVariant('Column Name')      

class TableView(QtGui.QTableView):
    def __init__(self):
        super(TableView, self).__init__()
        model=TableModel()
        self.setModel(model)
        self.show()       

view=TableView()
sys.exit(app.exec_())

Upvotes: 5

Views: 5881

Answers (1)

AmusingTeebs
AmusingTeebs

Reputation: 252

What you're looking for is the QHeaderView::setResizeMode function. I would recommend checking out the docs, but here's the code

self.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)

or, if you want to only stretch the least header item:

self.horizontalHeader().setStretchLastSection(True)

Upvotes: 7

Related Questions