【问题标题】:How to stretch QTableView last column header如何拉伸QTableView最后一列标题
【发布时间】:2014-09-30 00:10:32
【问题描述】:

下面的代码使用单列创建QTableView。如何使标题栏沿QTableView 视图的整个宽度延伸?

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

【问题讨论】:

    标签: python qt pyqt


    【解决方案1】:

    您正在寻找的是QHeaderView::setResizeMode 函数。我建议查看docs,但这里是代码

    self.horizo​​ntalHeader().setResizeMode(QtGui.QHeaderView.Stretch)

    或者,如果您只想拉伸最小的标题项:

    self.horizo​​ntalHeader().setStretchLastSection(True)

    【讨论】:

    • self.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch) 和 self.horizontalHeader().setStretchLastSection(True) 相应地。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 2020-01-31
    • 2021-07-05
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    相关资源
    最近更新 更多