如何给QTreeView的节点显示图标

 我来答
律北Ok
2017-09-02 · TA获得超过418个赞
知道小有建树答主
回答量:174
采纳率:0%
帮助的人:117万
展开全部
要显示节点图标,就在重写模型的时候在DecorationRole角色时返回要显示的图标就可以了
Node *node = nodeFromIndex(index);
if (role == Qt::DecorationRole){
switch(node->item){
case Node:: Root: return QIcon(":/images/ras.ico");
case Node::Downloading: return QIcon(":/images/Download.png");
case Node::Downloaded: return QIcon(":/images/Misc-Stuff.png");
case Node::Daily: return QIcon(":/images/Database.png");
case Node::Timespace:
return QIcon(":/images/Software.ico");
}
}

//如果设置 show-decoration-selected:1会导致点击选中后,前面branch区域背景为高亮的橙色.
ui->treeView->setVerticalScrollBarPolicy ( Qt::ScrollBarAlwaysOff ) ;
ui->treeView->setStyleSheet( \
" QTreeView { \
show-decoration-selected: 0; \
} \
\
QTreeView::item { \
show-decoration-selected: 0; \
background-image:url(image/list_bg02.png); \
} \
QTreeView::item:selected{ \
background-image:url(image/list_bg01a.png); \
} \
QTreeView::branch { \
selection-color: transparent; \
} \
QTreeView::branch:closed:has-children:has-siblings { \
image: url(image/icon_add.png); \
} \
\
QTreeView::branch:has-children:!has-siblings:closed { \
image: url(image/icon_add.png); \
} \
\
QTreeView::branch:open:has-children:has-siblings { \
image: url(image/icon_reduce.png); \
} \
\
QTreeView::branch:open:has-children:!has-siblings { \
image: url(image/icon_reduce.png); \
}");

ui->treeView->setAttribute(Qt::WA_MacShowFocusRect, 0);
ui->treeView->setFocusPolicy(Qt::NoFocus);
ui->treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式