您的位置:首页 > 编程语言 > Qt开发

QT中QTreeView与QAbstractItemModel使用中QTreeViwe的美化

2013-09-04 20:52 1341 查看
//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();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: