QTreeWidget 节点选中的图标怎么设置
发布网友
发布时间:2022-04-22 10:00
我来回答
共2个回答
热心网友
时间:2023-10-09 20:08
修改tree节点的图标的步骤如下:
首先,在src下建立一个assets文件夹,放几张图片;
其次,编写代码如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" fontSize="14" layout="absolute">
<mx:Script>
<![CDATA[
[Embed(source="assets/boy.jpg")]
public var boyIcon:Class;
[Embed(source="assets/girl.jpg")]
public var girlIcon:Class;
// 根据结点的属性设置节点图标
private function SetIcon(item:Object):*
{
var xml:XML = item as XML;
if(xml.attribute("sex")=="boy")
{
return boyIcon;
}
else
{
return girlIcon; }
}
// Tree控件的数据源
[Bindable]
public var departmentTree:XML=
<member name="小李" sex="boy">
<member name="王志" sex="boy">
<member name="赵强" sex="boy" />
<member name="李丽" sex="girl" />
</member>
<member name="韩梅梅" sex="girl">
<member name="孙艳" sex="girl"/>
</member>
</member>
]]>
</mx:Script>
<mx:Tree x="216" y="0" width="318"
height="450" id="treeXml"
dataProvider="{departmentTree}" labelField="@name"
showDataTips="true" iconFunction="SetIcon" />
</mx:Application>
最后,运行效果即可。
热心网友
时间:2023-10-09 20:09
QTreeWidgetItem* pTreeItem = nullptr;
pTreeItem = new QTreeWidgetItem(ui->treeWidget, QStringList(QString("000")));
QIcon icon;
icon.addPixmap(QPixmap(":/treeitem_screen_selected.png"), QIcon::Selected);
icon.addPixmap(QPixmap(":/treeitem_screen.png"), QIcon::Normal);
pTreeItem->setIcon(0, icon);