使用角色更改 QAbstractTableModel headerData

发布于 2024-12-09 16:49:57 字数 1827 浏览 0 评论 0原文

我有一个子类

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 的标题::CurrencyRole。如何使用角色更改每个视图的 headerData。

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

I have a subclass

class TableModel : public QAbstractTableModel

I override the headerData method as follow:

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();
}

I have a method that set a QTableView as follow using TableModel* table

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)));
}

I have two QTableView objects ViewA and viewB. I need ViewA to have a header with role == Qt::DisplayRole and viewB to have a header with role == TableModel::CurrencyRole. How can I get the headerData to change for each view using the role.

Thanks, and please let me know if I left any detail out or or something is unclear in my question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

油饼 2024-12-16 16:49:57

首先,看起来要完全做你想做的事情会有点棘手。

快速浏览 Qt 源代码后,我们发现仅使用 API 无法更改传递给模型的 headerData() 函数的角色。

但是,您确实可以子类化 QHeaderView 并重写虚拟 paintSection() 函数,然后执行您想要的任何操作。您可能需要查看 Qt 的该函数的实现,以供参考如何正确实现它。

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

First of all, it looks like to do exactly what you're trying to to is going to be a bit tricky.

After a quick perusal of the Qt source code it looks like there's no way to change what role is passed to your model's headerData() function just using the API.

You do, however, have the ability to subclass QHeaderView and override the virtual paintSection() function and then do whatever you want. You'll probably need to look over Qt's implementation of this function for reference of how to implement it properly.

At this point you can set the header view on your views to your new custom one, and then set some internal flag from your view that tells it how to properly call headerData() with the role you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文