//
隐藏根节点项前的图标(展开折叠图标)
ui.treeWidget->setRootIsDecorated(
false
);
//
如果只是隐藏根点的展开折叠图标而子节点保留,则只需要上面语句
ui.treeWidget->setStyleSheet(
"
QTreeView::branch:has-children:!has-siblings:closed,\
QTreeView::branch:closed:has-children:has-siblings{border-
image: none; image: none;}\
QTreeView::branch:open:has
-children:!has-
siblings,\
QTreeView::branch:open:has
-children:has-siblings{border-image: none; image: none;}
"
);
//
如果想把展开折叠图标(三角图标)换成自己的图标,可以用以下代码
ui.treeWidget->setStyleSheet(
"
QTreeView::branch:has-children:!has-siblings:closed,\
QTreeView::branch:closed:has-children:has-siblings{border-image: none; image: url(:/QtXmlTest/Resources/image/
plus.png);}\
QTreeView::branch:open:has
-children:!has-
siblings,\
QTreeView::branch:open:has
-children:has-siblings{border-image: none; image: url(:/QtXmlTest/Resources/image/minus.png);}
"
);
原文链接:https:
//
blog.csdn.net/jxbinwd/article/details/77507627
//2--自定义TreeWidgetItem
调用QTreeWidget::setItemWidget就可以了,并且,不会丧失QTreeWidgetItem跟鼠标事件的各默认联动功能,依旧存在。
//3--子项Item与顶层TopLevelItem顶端对其
treeWidget->setIndentation(0);
在我们设置QTreeWidgetItem的QSS时候,默认Item和QTreeWidget左边边框之间是两个部分。看起来很不好看。或者是,想设置当前激活的、或选中的Item的左边框QSS时, Item的边框效果,距离QTreeWidget的左边框是有间距的,很不好看。
调用QTreeWidget::setIndentation(0), 然后设置不同层级Item的Padding-left,让Text保持不同层级的效果。非常好的方案。