将垂直标题添加到 QTreeView

发布于 2024-08-16 16:40:54 字数 762 浏览 11 评论 0原文

我有一个 QTreeView 子类(和 QAbstractItemModel 子类),它有一个漂亮的水平标题。我想添加垂直标题(从左侧向下)以匹配。但与 QTableView 不同,它具有单独的垂直方向(setVerticalHeader())和水平标题(setHorizo​​ntalHeader()),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 技术交流群。

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

发布评论

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

评论(1

断桥再见 2024-08-23 16:40:55

您可能是对的,您必须“假装”,但您应该能够将其绘制得与水平标题一样好。

您需要做的是负责这些单元格的paintEvent,然后使用当前样式绘制自定义控件。类似的事情:

//QWidget* w is the widget who's style you want
QPainter painter(this);
QStyleOptionHeader opt;
opt.initFrom(this);
opt.state = QStyle::State_None;
opt.orientation = Qt::Vertical;
opt.state |= QStyle::State_Vertical;
if (w->isEnabled())
    opt.state |= QStyle::State_Enabled;
opt.state |= QStyle::State_Active;
w->style()->drawControl(QStyle::CE_Header, &opt, &painter, w);

负责您想要的单元格的绘制事件的最简单方法是创建一个自定义项目委托并重新实现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:

//QWidget* w is the widget who's style you want
QPainter painter(this);
QStyleOptionHeader opt;
opt.initFrom(this);
opt.state = QStyle::State_None;
opt.orientation = Qt::Vertical;
opt.state |= QStyle::State_Vertical;
if (w->isEnabled())
    opt.state |= QStyle::State_Enabled;
opt.state |= QStyle::State_Active;
w->style()->drawControl(QStyle::CE_Header, &opt, &painter, w);

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).

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