如果您正在使用QTreeView
,也许您必须创建自定义数据结构来对数据树进行建模。像这样的东西:
struct ModelItem
{
QString groupName;
QString name;
QString IPaddr;
ModelItem* parent;
std::vector< ModelItem* > childs;
ModelItem( const QString& a_name )
: name( a_name ),
parent( nullptr )
{ }
~ModelItem( )
{
for ( auto it = childs.begin( ); it != childs.end( ); ++it )
delete *it;
}
void AddChild( ModelItem* children )
{
childs.push_back( children );
children->parent = this;
}
};
当然,您需要子类化QAbstractItemModel
:
class CustomModel : public QAbstractItemModel
{
Q_OBJECT
public:
CustomModel( QObject* parent = nullptr );
~CustomModel( );
int columnCount( const QModelIndex& parent ) const override;
int rowCount( const QModelIndex& parent ) const override;
QVariant data( const QModelIndex& index,
int role = Qt::DisplayRole ) const override;
QModelIndex index ( int row,
int column,
const QModelIndex& parent ) const override;
QModelIndex parent( const QModelIndex & index ) const override;
void SetGroup( const QString& groupName,
const std::vector< std::pair< QString, QString > >& items );
void ResetModel( );
private:
ModelItem rootNode;
};
columnCount 和 rowCount 方法应返回模型的列数/行数:
int CustomModel::columnCount( const QModelIndex& /* parent */ ) const
{
return 1;
}
int CustomModel::rowCount( const QModelIndex& parent ) const
{
int to_return;
if ( parent.isValid( ) )
{
ModelItem* node = static_cast< ModelItem* >( parent.internalPointer( ) );
to_return = node->childs.size( );
}
else
to_return = rootNode.childs.size( );
return to_return;
}
data 方法应返回模型的内容:
QVariant CustomModel::data( const QModelIndex& index,
int role ) const
{
QVariant to_return;
if ( index.isValid( ) ) // if not valid, current index is root node
{
switch ( role )
{
case Qt::DisplayRole: // you can manage other roles to enrich the view
{
ModelItem* node = static_cast< ModelItem* >( index.internalPointer( ) );
to_return = node->name;
break;
}
}
}
return to_return;
}
index 将创建适当QModelIndex
的给定节点:
QModelIndex CustomModel::index ( int row,
int column,
const QModelIndex& parent ) const
{
QModelIndex to_return;
if ( ( row >= 0 && row < rowCount( parent ) )
&& ( column >= 0 && column <= columnCount( parent ) ) )
{
if ( parent.isValid( ) )
{
ModelItem* item = static_cast< ModelItem* >( parent.internalPointer( ) );
to_return = createIndex( row, column, item->childs.at( row ) );
}
else
{
to_return = createIndex( row, column, rootNode.childs.at( row ) );
}
}
return to_return;
}
parent 方法应返回给定节点的父节点的索引
QModelIndex CustomModel::parent( const QModelIndex & index ) const
{
QModelIndex to_return;
if ( index.isValid( ) )
{
ModelItem* node = static_cast< ModelItem* >( index.internalPointer( ) );
ModelItem* parent = node->parent;
ModelItem* parent2 = parent->parent;
if ( parent2 ) // node->parent can be root node
{
auto it = std::find_if( parent2->childs.begin( ), parent2->childs.end( ),
[&]( ModelItem* child ){ return child == parent; } );
if ( it != parent2->childs.end( ) )
{
int row = std::distance( parent2->childs.begin( ), it );
to_return = createIndex( row, 0, parent );
}
}
}
return to_return;
}
下一个方法:SetGroup。使用这种方法,我们可以将数据添加到模型中:
void CustomModel::SetGroup( const QString& groupName,
const std::vector< std::pair< QString, QString > >& items )
{
// Notify to view that we will insert a new group
beginInsertRows( QModelIndex( ), rootNode.childs.size( ), rootNode.childs.size( ) );
ModelItem* groupNode = new ModelItem( groupName );
rootNode.AddChild( groupNode );
for ( auto it = items.begin( ); it != items.end( ); ++it )
{
ModelItem* node = new ModelItem( it->first );
node->name = it->first;
node->IPaddr = it->second;
groupNode->AddChild( node );
}
endInsertRows( );
}
ResetModel 方法只是清理视图:
void CustomModel::ResetModel( )
{
beginResetModel( );
rootNode= ModelItem( "root" );
endResetModel( );
}
模型实现完成后,我们只需要向模型发送数据并链接模型和视图:
QTreeView* treeView_4 = new QTreeView( tab_10 );
CustomModel* model = new CustomModel( this );
std::vector< std::pair< QString, QString > > data;
data.push_back( std::make_pair( "node1", "" ) );
data.push_back( std::make_pair( "node2", "" ) );
model->SetGroup( "Group 1", data );
data.push_back( std::make_pair( "node3", "" ) );
model->SetGroup( "Group 2", data );
treeView4->setModel( model );