1、方法一:
#include <QStyleFactory>
ui.treeView->setStyle(QStyleFactory::create("windows"));
2、方法二:
QString strtyle2 = R"(
QTreeView::branch:has-siblings:!adjoins-item {
border-image: url(:/TreeViewDemo/res/vline.png);
}
QTreeView::branch:has-siblings:adjoins-item {
border-image: url(:/TreeViewDemo/res/branch-more.png);
}
QTreeView::branch:!has-children:!has-siblings:adjoins-item {
border-image: url(:/TreeViewDemo/res/branch-end.png);
}
QTreeView::branch:has-children:!has-siblings:closed,
QTreeView::branch:closed:has-children:has-siblings
{
border-image: none;
image: url(:/TreeViewDemo/res/branch-closed.png);
}
QTreeView::branch:open:has-children:!has-siblings,
QTreeView::branch:open:has-children:has-siblings
{
border-image: none;
image: url(:/TreeViewDemo/res/branch-open.png);
}
QTreeView::branch:open:has-children:!has-siblings:selected,
QTreeView::branch:open:has-children:has-siblings:selected {
border-image: none;
border-left:5px solid blue;
}
)";
ui.treeView->setStyleSheet(strtyle2);
样式表可参考:
Qt Style Sheets Examples | Qt Widgets 5.15.16
效果如下所示: