带有 qtreeview::item 的 Qt 样式表仅适用于子项? [英] Qt stylesheets with qtreeview::item only applies to child items?

查看:48
本文介绍了带有 qtreeview::item 的 Qt 样式表仅适用于子项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 QTreeView 的项目设置样式,即稍微增加它们的顶部/底部填充.这是我的片段:

I want to style the items of a QTreeView, namely increase their top/bottom padding a bit. This is my snippet:

QTreeView::item
{
    padding-top:    8px;
    padding-bottom: 8px;
    color: red;
}

不幸的是,这将填充应用于子项,即.所有顶级项目都具有与以前完全相同的填充.我添加了颜色:红色;"仅用于测试目的,但另一方面,红色文本颜色适用于所有项目,无论是顶级项目还是子项目.

Unfortunately, this only applies the padding to child items, ie. all top-level items have exactly the same padding as before. I added the "color: red;" just for testing purposes, but the red text color, on the other hand, applies to all items no matter if top-level or child.

是否有一些我遗漏的特殊限定词?我尝试使用 :active:has-sibling,但每种情况的结果都是一样的.

Is there some special qualifier that I am missing? I tried with :active and :has-sibling, but the results were the same for each case.

我使用的是 Qt 4.6.3.我也试过 4.8,但结果是一样的.

I am using Qt 4.6.3. I also tried with 4.8, but results are the same.

推荐答案

问题出在我在视图上使用的 QStyledItemDelegate 子类上.委托只是为了为项目绘制水平线,我没有没有重新实现 sizehint 函数,但经过进一步检查,似乎子类返回了错误的 sizehint.

The problem was with a QStyledItemDelegate subclass I was using on the view. The delegate was only meant to draw horizontal lines for items, and I did not reimplement the sizehint function, but upon further inspection, it appeared that the subclass was returning the wrong sizehint.

因此,我接受了 DanielCastro 的建议,只是在委托子类中使用了虚拟 sizehint 函数.

I therefore went with DanielCastro's advice and just used the virtual sizehint function in the delegate subclass.

这篇关于带有 qtreeview::item 的 Qt 样式表仅适用于子项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆