如何自动拉伸QTableView列并使其可调整

时间:2015-01-28 05:24:41

标签: python qt header pyqt qtableview

我非常喜欢使用以下内容自我调整到QTableView列宽度的干净结果:

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

但不幸的是,使用此标志后,列宽度不再保持可调(用户无法调整列宽度)。

我想知道是否有另一种方法可以将列宽设置为QTableView的宽度,同时保持列宽#34;用户可调节"?

enter image description here

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

class Model(QAbstractTableModel):
    def __init__(self, parent=None, *args):
        QAbstractTableModel.__init__(self, parent, *args)
        self.items = ['Item_A_001','Item_A_002','Item_B_001','Item_B_002']
        self.totalColumn=10

    def rowCount(self, parent=QModelIndex()):
        return len(self.items)       
    def columnCount(self, parent=QModelIndex()):
        return self.totalColumn
    def setColumnCount(self, number):
        self.totalColumn=number
        self.reset()

    def data(self, index, role):
        if not index.isValid(): return QVariant()
        elif role != Qt.DisplayRole:
            return QVariant()

        row=index.row()
        if row<len(self.items):
            return QVariant(self.items[row])
        else:
            return QVariant()


class MyWindow(QWidget):
    def __init__(self, *args):
        QWidget.__init__(self, *args)

        tableModel=Model(self)               

        self.view=QTableView(self) 
        self.view.setModel(tableModel)
        self.view.horizontalHeader().setResizeMode(QHeaderView.Stretch)

        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)

    def hideColumn(self):
        self.view.model().setColumnCount(1)

    def unhideColumn(self):
        self.view.model().setColumnCount(10)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyWindow()
    w.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:3)

所以这是我提出的一个小技巧。我修改了MyWindow的resize事件来调整你的QTableWidget的大小。

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

    def hideColumn(self):
        self.view.model().setColumnCount(1)

    def unhideColumn(self):
        self.view.model().setColumnCount(10)

    #Added a reimplementation of the resize event
    def resizeEvent(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 columns

        for columnNum in range( self.tableModel.columnCount()): #For each column
            self.view.setColumnWidth(columnNum, int(tableSize/numberOfColumns) ) #Set the width = tableSize / nbColumns
        super(MyWindow, self).resizeEvent(event) #Restores the original behaviour of the resize event

唯一的缺点是滚动条轻弹。我认为这可以通过一些调整来解决。

<强>更新 调整大小,不再轻弹,禁用滚动条时,可以获得更清晰的外观。

修改了QTableView的初始化:

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

修改了resizeEvent:

def resizeEvent(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 in range( self.tableModel.columnCount()):
        if remainingWidth > 0:
            self.view.setColumnWidth(columnNum, int(tableSize/numberOfColumns) + 1 )
            remainingWidth -= 1
        else:
            self.view.setColumnWidth(columnNum, int(tableSize/numberOfColumns) )