如何给QTreeView的节点显示图标
展开全部
要显示节点图标,就在重写模型的时候在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);
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);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询