Skip to content Skip to sidebar Skip to footer

How To Auto Stretch Qtableview Columns And Keep Them Being Adjustable

I really like a clean result of self-adjusting to the QTableView's width of the columns using: self.view.horizontalHeader().setResizeMode(QHeaderView.Stretch) But unfortunately wi

Solution 1:

so here is a little trick I came up with. I modified the resize event of MyWindow to resize your QTableWidget.

classMyWindow(QWidget):
    def__init__(self, *args):
        QWidget.__init__(self, *args)

        self.tableModel=Model(self) #Set the model as part of your class to access it in the event handler               

        self.view=QTableView(self) 
        self.view.setModel(self.tableModel) #Modified here too
        self.view.horizontalHeader().setResizeMode(QHeaderView.Interactive) #Mode set to Interactive to allow resizing

    hideButton=QPushButton('Hide Column')
    hideButton.clicked.connect(self.hideColumn)

    unhideButton=QPushButton('Unhide Column')
    unhideButton.clicked.connect(self.unhideColumn)

    layout = QVBoxLayout(self)
    layout.addWidget(self.view)
    layout.addWidget(hideButton)
    layout.addWidget(unhideButton)
    self.setLayout(layout)

    defhideColumn(self):
        self.view.model().setColumnCount(1)

    defunhideColumn(self):
        self.view.model().setColumnCount(10)

    #Added a reimplementation of the resize eventdefresizeEvent(self, event):
        tableSize = self.view.width() #Retrieves your QTableView width
        sideHeaderWidth = self.view.verticalHeader().width() #Retrieves the left header width
        tableSize -= sideHeaderWidth #Perform a substraction to only keep all the columns width
        numberOfColumns = self.tableModel.columnCount() #Retrieves the number of columnsfor columnNum inrange( self.tableModel.columnCount()): #For each column
            self.view.setColumnWidth(columnNum, int(tableSize/numberOfColumns) ) #Set the width = tableSize / nbColumnssuper(MyWindow, self).resizeEvent(event) #Restores the original behaviour of the resize event

The only downfall is that the scrollbar is flicking. I think this can be solved with some adjustment.

Update: Get a cleaner look when resizing, no more flicking, disabled the scrollbar.

Modified initialization of the QTableView:

self.view=QTableView(self) 
self.view.setModel(self.tableModel)
self.view.horizontalHeader().setResizeMode(QHeaderView.Interactive)
#Added these two linesself.view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.view.horizontalHeader().setStretchLastSection(True)

Modified resizeEvent:

defresizeEvent(self, event):
    super(MyWindow, self).resizeEvent(event)
    tableSize = self.view.width()
    sideHeaderWidth = self.view.verticalHeader().width()
    tableSize -= sideHeaderWidth
    numberOfColumns = self.tableModel.columnCount()

    remainingWidth = tableSize % numberOfColumns 
    for columnNum inrange( self.tableModel.columnCount()):
        if remainingWidth > 0:
            self.view.setColumnWidth(columnNum, int(tableSize/numberOfColumns) + 1 )
            remainingWidth -= 1else:
            self.view.setColumnWidth(columnNum, int(tableSize/numberOfColumns) )

Post a Comment for "How To Auto Stretch Qtableview Columns And Keep Them Being Adjustable"