You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求修改QTreeView样式:实现分支同时显示文件夹与展开折叠图标

QTreeView Style: Display Folder Icons Alongside Expand/Collapse (+/-) Controls

Current Setup

You're currently using this QTreeView stylesheet, which replaces the expand/collapse icons with folder images:

tree_style = """ 
QTreeView { 
    background: #EBFEF4; 
    show-decoration-selected: 1; 
} 
QTreeView::item { 
    border: 1px solid #d9d9d9; 
    border-top-color: transparent; 
    border-bottom-color: transparent; 
} 
QTreeView::item:hover { 
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e7effd, stop: 1 #cbdaf1); 
    border: 1px solid #bfcde4; 
} 
QTreeView::item:selected { 
    border: 1px solid #567dbc; 
} 
QTreeView::item:selected:active{ 
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6ea1f1, stop: 1 #567dbc); 
} 
QTreeView::item:selected:!active { 
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6b9be8, stop: 1 #577fbf); 
} 
QTreeView::branch:has-siblings:!adjoins-item { 
    border-image: url(vline.png) 0; 
} 
QTreeView::branch:has-siblings:adjoins-item { 
    border-image: url(:/.png/branch-more.png) 0; 
} 
QTreeView::branch:!has-children:!has-siblings:adjoins-item { 
    border-image: url(:/.png/branch-end.png) 0; 
} 
QTreeView::branch:has-children:!has-siblings:closed, QTreeView::branch:closed:has-children:has-siblings { 
    border-image: none; 
    image: url(:/.png/FolderClosed.png); 
} 
QTreeView::branch:open:has-children:!has-siblings, QTreeView::branch:open:has-children:has-siblings { 
    border-image: none; 
    image: url(:/.png/FolderOpen.png); 
} 
"""

The core issue here is that you're replacing the default expand/collapse (+/-) icons with folder images in the QTreeView::branch selector. To show both controls simultaneously, we need to separate the branch's expand/collapse UI from the folder icon.

Solution

We'll adjust the stylesheet to:

  • Keep the branch's expand/collapse icons (or use custom +/- icons)
  • Add folder icons to items that have children, positioned next to the branch controls

Here's the modified stylesheet:

tree_style = """ 
QTreeView { 
    background: #EBFEF4; 
    show-decoration-selected: 1;
    /* Increase indentation to make space for both branch and folder icon */
    indentation: 40px; 
} 
QTreeView::item { 
    border: 1px solid #d9d9d9; 
    border-top-color: transparent; 
    border-bottom-color: transparent;
    padding-left: 8px; /* Add spacing between folder icon and text */
} 
QTreeView::item:hover { 
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e7effd, stop: 1 #cbdaf1); 
    border: 1px solid #bfcde4; 
} 
QTreeView::item:selected { 
    border: 1px solid #567dbc; 
} 
QTreeView::item:selected:active{ 
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6ea1f1, stop: 1 #567dbc); 
} 
QTreeView::item:selected:!active { 
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6b9be8, stop: 1 #577fbf); 
} 

/* Keep branch line styling intact */
QTreeView::branch:has-siblings:!adjoins-item { 
    border-image: url(vline.png) 0; 
} 
QTreeView::branch:has-siblings:adjoins-item { 
    border-image: url(:/.png/branch-more.png) 0; 
} 
QTreeView::branch:!has-children:!has-siblings:adjoins-item { 
    border-image: url(:/.png/branch-end.png) 0; 
} 
/* Use custom +/- icons for expand/collapse (or omit to keep Qt's default symbols) */
QTreeView::branch:has-children:closed {
    image: url(:/.png/plus-icon.png); /* Replace with your plus icon path */
}
QTreeView::branch:has-children:open {
    image: url(:/.png/minus-icon.png); /* Replace with your minus icon path */
}

/* Add folder icons to items with children */
QTreeView::item:has-children {
    image: url(:/.png/FolderClosed.png); 
    image-position: left center;
    padding-left: 28px; /* Adjust based on your folder icon size */
}
QTreeView::item:has-children:open {
    image: url(:/.png/FolderOpen.png);
}
"""

Key Adjustments Explained

  • Indentation: Increased indentation on QTreeView to make room for both the branch control and folder icon.
  • Branch Styling: Restored the expand/collapse icons (using custom +/- images) instead of replacing them with folder icons. If you prefer Qt's default +/- symbols, simply remove the QTreeView::branch:closed/:open image rules.
  • Item Folder Icons: Added folder images to QTreeView::item:has-children and :open states, positioned to the right of the branch controls. Tweak padding-left and indentation values to match your icon sizes for proper alignment.

内容的提问来源于stack exchange,提问作者Binit Amin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:19:58