如何拉伸QTableView最后一列标题 [英] How to stretch QTableView last column header

查看:74
本文介绍了如何拉伸QTableView最后一列标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

推荐答案

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

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

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

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

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

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

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

self.horizontalHeader().setStretchLastSection(True)

这篇关于如何拉伸QTableView最后一列标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆