2014-09-28 13 views
1

Qt使用"parent-child relationship"將Widget鏈接在一起,這是衆所周知的事實。爲了保持(C++)Qt5應用程序,我在尋找能夠以某種方式描述這些關係的工具,是這樣的:獲取應用程序的Widgets樹

QMainApplication 
    | 
    \----->QMainWindow 
     | 
     \----->QSplitter(as CentralWidget) 
       | 
       \--->QTextEdit 
       \--->QTextEdit 

這可能是這可能從代碼中推出了具有QApplication作爲參數的函數;它可能是一個能夠分析代碼的外部工具。

這樣的功能/工具是否存在?

回答

3

有幾個解決方案:

Qt Designer有插件裏面做類似的事情。

enter image description here

但是你可以使用這個類來看看關係:

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); 
} 

使用(在main.cpp中) :

#include "objecttreemodel.h" 
//... 
MainWindow w; 
w.show(); 
ObjectTreeModel *model = new ObjectTreeModel(&w); 
QTreeView tree; 
tree.setModel(model); 
tree.show(); 

結果:

enter image description here

+0

很有意思:謝謝分享你的代碼! – suizokukan 2014-09-28 08:21:08

+0

如果您將添加一個事件過濾器來控制創建和刪除子對象,您的解決方案將是非常棒的:) – 2014-09-29 06:51:59