- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个子类
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
对象ViewA
和viewB
。我需要 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/
试图更多地了解 QAbstractTableModel,我遇到了 rowCount 和 columnCount 的虚拟方法,这些方法在继承 QAbstractTableModel 时需要实现。 拿 i
出于测试目的,有一个函数要求输入并将其显示在 QListView 中。 display data . 这适用于一个 dim.data 我想扩展脚本显示二维数据 传入的数据应该插入第一行直到第 3 列,
最近又拿起了Qt,开始刷新内存了。为表创建自定义数据模型非常简单。 现在我正在尝试检索选定的数据。请注意,我使用自定义数据对象。 我的自定义模型示例: 平台.h class Platform { pu
如何使 QAbstractTableModel 的数据可检查 我想让下面代码中的每个单元格都可以被用户选中或取消选中,如何修改代码? 根据Qt文档:Qt::CheckStateRole和设置Qt::I
我用通常的方式实现了 QAbstractTableModel: class PrintIntervalTableModel : public QAbstractTableModel { private
我需要用 Qt 实现一个表。 我相信我会起诉一个 QAbstractTableModel,一个 QTableView 使用这个模型。 我知道我必须编辑模型的 rowCount()、columnCoun
我正在尝试显示来自 QAbstractTableModel 的图像。我尝试返回一个QPixmap作为data()的QVariant,但它只产生空单元格,而我期望第二个单元格中的每个单元格列有一个 20
我有一个使用 Q_GLOBAL_STATIC 实现的单例类,它包含一个必须从多个线程访问的数据结构,我在类中实现了访问器函数,它会在访问数据之前锁定互斥量,这样所有对共享数据的访问都是序列化的。 问题
我目前正在尝试制作一个基于QTableView 的vie。因此,我使用 QAbstractTableModel 来存储数据。我的问题是,一旦我从 QVariant headerData(int sec
我有一个子类 class TableModel : public QAbstractTableModel 我重写了 headerData 方法如下: QVariant TableModel::head
我从 QAbstractTableModel 派生了一个模型,现在我想通知一整行的数据已被更改。例如,如果更改了索引为 5 的行的数据(4 列),则使用以下代码将按预期工作。 emit dataCha
我有一个 PyQt QTableView,连接到一个 QAbstractTableModel,它本身连接到一个管理项目列表的自定义类。我可以在列表末尾插入一个项目,它会适本地通知我的模型,然后调用 b
我发现了这个问题:How to change the background color for a QTreeView Header (aka QHeaderView)? 我希望能够为每个标题部分设置
我创建了一个名为 tableModel 的 QAbstractTableModel 模型和一个名为 listModel 的 QAbstractListModel 模型,并将这些模型设置为各自的 Vie
我需要一个能够快速添加和显示大约 100'000 个项目的表格 GUI 控件。出于这个原因,需要“虚拟”表控件,它只加载和显示可见的项目。我尝试了 QTableView 和 QAbstractTabl
QTableView 由QAbstractTableModel 控制。它是填充其内容的模型。模型修改了 self.items 变量,结果是用户双击了 QTableView 的一个项目,然后输入了一个新
这是我在这里的第一篇文章,但自从我开始编程以来,我得到了你们所有人的很多帮助。 我是 Qt 的新手,目前正在尝试制作我的第一个项目。我的问题是关于模型、底层数据和 View 之间的通信。 我首先将后台
在我的应用程序中,我有一个用于保存项目列表的类: class Database : public QObject { Q_OBJECT public: Database(QObject
Qt.AlignRight 右对齐文本但将其放在右上角。 Qt.AlignRight | Qt.AlignVCenter 不起作用。将其放入左上角。 有没有办法让文本同时保持垂直居中和右对齐? 代码示
我有自己的表格模型 (DragDropTableModel),它是 QAbstractTableModel 的子类。在它里面,我希望最后一行始终保持空白。 我通过将 insertRows() 添加到
我是一名优秀的程序员,十分优秀!