QTreeView QTableview设置相关

本文详细介绍了如何使用QAbstractItemView和QTableView进行高级属性配置,包括设置表头、选择模式、显示行为、以及获取编辑后的表格或treeview的值。同时,提供了一个自定义表头数据的示例,并展示了如何关闭编辑器并释放资源。

1 设置表头
QVariant QAbstractItemModel::​headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const

用法:

QVariant DockMessageModel::headerData(int section, Qt::Orientation orientation, int role /*= Qt::DisplayRole*/) const
{
    /*return QString("ABC %1").arg(section);*/

    if (role != Qt::DisplayRole)
        return QVariant();

    if (orientation == Qt::Horizontal)
        return QString("Column %1").arg(section);
    else
        return QString("Row %1").arg(section);
}

2 其它设置,

    this->setSelectionMode(QAbstractItemView::SingleSelection);  // 只能选择一行
    this->setSelectionBehavior(QAbstractItemView::SelectRows); //按行选
    this->horizontalHeader()->setVisible(true);  // 表头可见
    this->horizontalHeader()->setStretchLastSection(true);  // QTableview的函数,设置最后一列填充
    this->verticalHeader()->setVisible(false);

3 获取edit后的表格或treeview的值

void QAbstractItemView::​closeEditor(QWidget * editor, QAbstractItemDelegate::EndEditHint hint)

Closes the given editor, and releases it. The hint is used to specify how the view should respond to the end of the editing operation. For example, the hint may indicate that the next item in the view should be opened for editing.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值