将垂直标题添加到 QTreeView
我有一个 QTreeView 子类(和 QAbstractItemModel 子类),它有一个漂亮的水平标题。我想添加垂直标题(从左侧向下)以匹配。但与 QTableView
不同,它具有单独的垂直方向(setVerticalHeader()
)和水平标题(setHorizontalHeader()
),
QTreeView
只允许单个标头(setHeader()
)。
我知道我可以假装最左边的列是标题并用不同的背景颜色渲染它(我不需要调整行大小或重新排列行的能力,所以我不必实现任何这些特殊行为)。但这并没有考虑到当地的风格。例如,在我的系统上,标题有轻微的渐变,使它们具有近乎 3D 的外观。
因此,我正在寻找一种方法来添加适当的垂直标题(我猜测它将是 QHeaderView 的子类),或者在最左边的列中渲染标题,但以正确的方式渲染它。
I have a QTreeView
subclass (and QAbstractItemModel
subclass) which has a nice horizontal header. I would like to add vertical headers (going down the left side) to match. But unlike QTableView
which has separate vertical (setVerticalHeader()
) and horizontal headers (setHorizontalHeader()
), QTreeView
only allows a single header (setHeader()
).
I know that I can just pretend that the leftmost column is the header and render it with a different background color (I don't need the ability to resize or rearrange the rows, so I wouldn't have to implement any of those special behaviors). But that doesn't take into account local styles. For example, on my system the headers have a slight gradient, giving them an almost 3D look.
I am therefore looking for either a way to add a proper vertical header (I am guessing it would be a subclass of QHeaderView
) or else rendering a header down the leftmost column, but rendering it the Right Way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能是对的,您必须“假装”,但您应该能够将其绘制得与水平标题一样好。
您需要做的是负责这些单元格的paintEvent,然后使用当前样式绘制自定义控件。类似的事情:
负责您想要的单元格的绘制事件的最简单方法是创建一个自定义项目委托并重新实现
virtual void Paint( QPainter * Painter, const QStyleOptionViewItem & option, const QModelIndex & ; 索引 ) const = 0 方法。您只需将您的绘画门控到您关心的索引,然后将其余的传递给超类(请参阅该类的 Qt 文档)。
Your probably right that you will have to "pretend" but you should be able to draw it to look as good as your horizontal headers.
What you need to do is be in charge of the
paintEvent
for those cells and then use the current style to draw a custom control. Something like:The easiest way to be in charge of a paint event for the cells you want would be to create a custom item delegate and reimplement the
virtual void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const = 0
method. You just gate your painting to the indexes you care about and pass the rest to the super class (see Qt's docs for this class).