我需要实现自己的 QAbstractTableModel 吗?
我发现这个问题:如何更改QTreeView 标题(又名 QHeaderView)的背景颜色?
我希望能够为每个标题部分设置颜色。所以上面看到的问题似乎是我的解决方案!
解决方案说“最简单的方法做到这一点可能是从 QAbstractItemModel 或另一个模型类派生一个新模型并重新实现 headerData( )”。我去了 查看 Qt 源代码树QTableWidget、QTableModel、QTableWidgetItem...这些类被认为是“默认模型”,所以我认为它们将是一个很好的例子,然后我会去实现我自己的QAbstractTableModel。
我看到的 3 个文件有多达 3300 行代码。在我看来,这绝对不是“最简单的方法”!
我想要 QTableWidget 的功能,但我想添加以下功能:
- horizontalHeader.setSectionColor(index,color)
- VerticalHeader.setSectionColor(index,color)
如果我想要的是,我真的需要继承/实现 QAbstractTableModel更改节标题的颜色?
更新:
我没有使用自己的自定义视图和模型类。我正在使用 现在是便利类 QTableWidget (它被称为便利类 b/c 它实现了视图和模型)。函数 headerData()
是一部分模型的。 模型类,QTableModel,无法通过 Qt lib/headers 访问,因此我无法从中继承。
更新:
我尝试使用背景画笔 QBrush(QColor(Qt::red))
创建一个新项目,然后使用新项目设置表格的标题(使用 QTableWidget::setHorizontalHeaderItem(int column) ,QTableWidgetItem *item)
。我还尝试继承QTableWidgetItem
并重写虚拟data()
方法:
QVariant HeaderItem::data(int role) const
{
if(role==Qt::BackgroundRole) {
return QVariant(QBrush(QColor(Qt::red)));
} else if(role==Qt::ForegroundRole) {
return QVariant(QBrush(QColor(Qt::green)));
} else {
return QTableWidgetItem::data(role);
}
}
我可以更改标题部分。但是当我尝试更改标题的背景画笔时...什么也没有发生...就像我为标题部分设置的 QTableWidgetItem 的背景画笔被忽略一样。
I found this question: How to change the background color for a QTreeView Header (aka QHeaderView)?
I want to be able to set the color for each header section. So the question seen above seems to be my solution!
The solution says "the easiest way to do that is probably to derive a new model from QAbstractItemModel or another model class and reimplement the headerData()". I went and looked at the Qt source tree for QTableWidget, QTableModel, QTableWidgetItem... these classes are supposedly "default models" so I thought they would be a good example and then I would go implement my own QAbstractTableModel.
The 3 files I saw are a whopping 3300 lines of code. That is definitely NOT "the easiest way" IMO!!!
I would like the functionality of QTableWidget but then I want to add the following ability:
- horizontalHeader.setSectionColor(index,color)
- verticalHeader.setSectionColor(index,color)
Do I really need to inherit/implement QAbstractTableModel if all I want is to change the color of a section header?
Update:
I am not using my own custom view and model classes. I am using the convenience class QTableWidget right now (it is called a convenience class b/c it implements the view and model). The function headerData()
is part of the model. The model class, QTableModel, is not accessible via Qt lib/headers so I can't inherit from it.
Update:
I tried creating a new item with background brush QBrush(QColor(Qt::red))
and then setting the table's header with the new item (using QTableWidget::setHorizontalHeaderItem(int column, QTableWidgetItem *item)
. I also tried inheriting QTableWidgetItem
and overriding the virtual data()
method with:
QVariant HeaderItem::data(int role) const
{
if(role==Qt::BackgroundRole) {
return QVariant(QBrush(QColor(Qt::red)));
} else if(role==Qt::ForegroundRole) {
return QVariant(QBrush(QColor(Qt::green)));
} else {
return QTableWidgetItem::data(role);
}
}
I can change the header sections foreground. But when I try to change the header's background brush... nothing happens... it's like the QTableWidgetItem's background brush that I set for the header section is ignored.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要从头开始使用自定义 headerData() 创建模型,而是创建 QTableWidgetItem 所需的 QTableWidgetItem::data() 并将此类的实例用于 QTableWidget::setHorizontalHeaderItem。
Instead of creating model with custom headerData() from scratch create subclass of QTableWidgetItem with desired implementation of QTableWidgetItem::data() and use the instances of this class for QTableWidget::setHorizontalHeaderItem.