gpt4 book ai didi

c++ - 使用角色更改 QAbstractTableModel headerData

转载 作者:行者123 更新时间:2023-11-30 03:05:21 25 4
gpt4 key购买 nike

我有一个子类

class TableModel : public QAbstractTableModel

我重写了 headerData 方法如下:

QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {...}
if (role == TableModel::CurrencyRole && orientation == Qt::Horizontal) {...}
return QVariant();
}

我有一个使用 TableModel* table 设置 QTableView 的方法

void A::SetDisplay(QTableView* table_view, QString filter, int role, int sort_role)
{
proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(table);
proxyModel->setDynamicSortFilter(true);
proxyModel->setSortRole(sort_role);
table_view->setModel(proxyModel);
table_view->setSortingEnabled(true);
table_view->setSelectionBehavior(QAbstractItemView::SelectRows);
table_view->horizontalHeader()->setStretchLastSection(true);
table_view->verticalHeader()->hide();
table_view->setEditTriggers(QAbstractItemView::NoEditTriggers);
table_view->setSelectionMode(QAbstractItemView::SingleSelection);
proxyModel->setFilterRegExp(QRegExp(filter, Qt::CaseInsensitive));
proxyModel->setFilterKeyColumn(1);
proxyModel->sort(0, Qt::AscendingOrder);
connect( table_view->selectionModel(),
SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
this, SIGNAL(selectionChanged(QItemSelection)));
}

我有两个QTableView 对象ViewAviewB。我需要 ViewA 有一个带有 role == Qt::DisplayRole 的标题和 viewB 有一个带有 role == TableModel 的标题::货币角色。如何使用该角色让 headerData 为每个 View 更改。

谢谢,如果我遗漏了任何细节或者我的问题中有什么不清楚的地方,请告诉我。

最佳答案

首先,看起来要完全按照您的意愿去做会有点棘手。

快速阅读 Qt 源代码后,似乎无法仅使用 API 更改传递给模型的 headerData() 函数的角色。

但是,您确实可以子类化 QHeaderView 并覆盖虚拟 paintSection() 函数,然后执行任何您想做的事情。您可能需要查看 Qt's implementation了解此功能以供引用如何正确实现。

此时您可以将 View 的标题 View 设置为新的自定义 View ,然后从您的 View 中设置一些内部标志,告诉它如何正确调用 headerData() 角色你想要的。

关于c++ - 使用角色更改 QAbstractTableModel headerData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7756834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com