Reputation: 1379
It's a well-known fact that Qt
uses a "parent-child relationship" to link widgets together. In order to maintain a (C++)Qt5
application, I'm searching for a tool able to somehow describe these relationships, something like :
QMainApplication
|
\----->QMainWindow
|
\----->QSplitter(as CentralWidget)
|
\--->QTextEdit
\--->QTextEdit
It may be a function which could be launched from the code with the QApplication
as a parameter; it may be too an external tool able to analyse the code.
Does such a function/tool exist ?
Upvotes: 2
Views: 754
Reputation: 18514
There are few solutions:
In Qt Designer
there is widget which do something similar.
But you can use this class to see relationship:
ObjectTreeModel.h
#ifndef OBJECTTREEMODEL_H
#define OBJECTTREEMODEL_H
#include <QAbstractItemModel>
class ObjectTreeModel : public QAbstractItemModel
{
Q_OBJECT
public:
ObjectTreeModel( QObject *root, QObject *parent = 0 )
{
m_root = root;
}
QVariant data( const QModelIndex &index, int role ) const;
QVariant headerData( int section, Qt::Orientation orientation,
int role = Qt::DisplayRole ) const;
int rowCount( const QModelIndex &parent = QModelIndex() ) const;
int columnCount( const QModelIndex &parent = QModelIndex() ) const;
QModelIndex index( int row, int column,
const QModelIndex &parent = QModelIndex() ) const;
QModelIndex parent( const QModelIndex &index ) const;
private:
QObject *m_root;
};
#endif // OBJECTTREEMODEL_H
*.cpp
#include "objecttreemodel.h"
QVariant ObjectTreeModel::headerData(int section, Qt::Orientation orientation, int role ) const
{
if( role != Qt::DisplayRole || orientation != Qt::Horizontal )
return QVariant();
switch( section )
{
case 0:
return QString( "Object" );
case 1:
return QString( "Class" );
default:
return QVariant();
}
}
QModelIndex ObjectTreeModel::index(int row, int column,const QModelIndex &parent ) const
{
QObject *parentObject;
if( !parent.isValid() )
parentObject = m_root;
else
parentObject = static_cast<QObject*>( parent.internalPointer() );
if( row >= 0 && row < parentObject->children().count() )
return createIndex( row, column, parentObject->children().at( row ) );
else
return QModelIndex();
}
int ObjectTreeModel::rowCount(const QModelIndex &parent ) const
{
QObject *parentObject;
if( !parent.isValid() )
parentObject = m_root;
else
parentObject = static_cast<QObject*>( parent.internalPointer() );
return parentObject->children().count();
}
int ObjectTreeModel::columnCount(const QModelIndex &parent ) const
{
return 2;
}
QVariant ObjectTreeModel::data( const QModelIndex &index, int role) const
{
if( !index.isValid() )
return QVariant();
if( role == Qt::DisplayRole )
{
switch( index.column() )
{
case 0:
return static_cast<QObject*>( index.internalPointer() )->objectName();
case 1:
return static_cast<QObject*>( index.internalPointer() )->metaObject()->className();
default:
break;
}
}
return QVariant();
}
QModelIndex ObjectTreeModel::parent(const QModelIndex &index) const
{
if( !index.isValid() )
return QModelIndex();
QObject *indexObject = static_cast<QObject*>( index.internalPointer() );
QObject *parentObject = indexObject->parent();
if( parentObject == m_root )
return QModelIndex();
QObject *grandParentObject = parentObject->parent();
return createIndex( grandParentObject->children().indexOf( parentObject ),0, parentObject );
}
Usage(in main.cpp):
#include "objecttreemodel.h"
//...
MainWindow w;
w.show();
ObjectTreeModel *model = new ObjectTreeModel(&w);
QTreeView tree;
tree.setModel(model);
tree.show();
Result:
Upvotes: 4