博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
QT中QTreeView与QAbstractItemModel使用中QTreeViwe的美化
阅读量:4300 次
发布时间:2019-05-27

本文共 1737 字,大约阅读时间需要 5 分钟。

//QTreeView中节点展开过程中自动调整表头的列宽,使被展开的节点不会被遮住

ui->treeView_Comm_SCL->header()->setResizeMode(QHeaderView::ResizeToContents);

//在QAbstractItemModel的派生类中进行QTreeView显示的美化:

//在data中可以设置树节点使用的图标

QVariant ServerModel::data(const QModelIndex &index, int role) const

{
if(role != Qt::DisplayRole
&& role != Qt::DecorationRole)
return QVariant();
ServerNode* node = NodeFromIndex(index);
if(0 == node)
return QVariant();
if(Qt::DisplayRole == role)
{
switch(index.column())
{
case 0:
return node->NodeName();
case 1:
//            return node->NodeComment();
return node->NodeValue();
default:
return QVariant();
}
}
else if(Qt::DecorationRole == role)     //set icon for tree view
{
switch (index.column())
{
case 0:
return node->NodeIcon();
default:
return QVariant();
}
}
return QVariant();
}

//在headerData中可以设置表头的图标与表头每列所占的初始化宽度,避免树节点被遮挡

QVariant ServerModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
if(section == 0)
{
return QObject::trUtf8("Model Node Name");
}
else if(section == 1)
{
return QObject::trUtf8("Comment");
}
return QVariant();
}
//set Icon for QTreeView header
if(orientation == Qt::Horizontal && role == Qt::DecorationRole)
{
if(section == 0)
{
return QIcon(QPixmap(QString::fromUtf8(":/Widget/Catalogue/WidgetIcon/Catalogue/tree.png")));
}
else if(section == 1)
{
QIcon listIcon(QPixmap(QString::fromUtf8(":/Widget/Catalogue/WidgetIcon/Catalogue/list.png")));
return listIcon;
}
else
{
return QVariant();
}
}
//set Colume width
if(orientation == Qt::Horizontal && role == Qt::SizeHintRole)
{
if(section == 0)
{
return QSize(300,20);
}
else if(section == 1)
{
return QSize(100,20);
}
else
{
return QVariant();
}
}
return QVariant();
}

转载地址:http://njxws.baihongyu.com/

你可能感兴趣的文章
子网掩码
查看>>
第一天上班没精神
查看>>
启动eclipse报错:Failed to load the JNI shared library
查看>>
eclipse安装插件的两种方式在线和离线
查看>>
linux下源的相关笔记(suse)
查看>>
linux系统分区文件系统划分札记
查看>>
Linux(SUSE 12)安装Tomcat
查看>>
Linux(SUSE 12)安装jboss4并实现远程访问
查看>>
Neutron在给虚拟机分配网络时,底层是如何实现的?
查看>>
netfilter/iptables全攻略
查看>>
Overlay之VXLAN架构
查看>>
Eclipse : An error occurred while filtering resources(Maven错误提示)
查看>>
在eclipse上用tomcat部署项目404解决方案
查看>>
web.xml 配置中classpath: 与classpath*:的区别
查看>>
suse如何修改ssh端口为2222?
查看>>
详细理解“>/dev/null 2>&1”
查看>>
suse如何创建定时任务?
查看>>
suse搭建ftp服务器方法
查看>>
centos虚拟机设置共享文件夹并通过我的电脑访问[增加smbd端口修改]
查看>>
文件拷贝(IFileOperation::CopyItem)
查看>>