Если хотите использовать TreeView - то нужно наследовать несколько классов.
При работе необходимо использовать QAbstractItemModel
если использовать QAbstractItemModel необходимо определить собственную реализацию итема :
#ifndef TREEITEM_H
#define TREEITEM_H
#include <QList>
#include <QVariant>
class TreeItem
{
public:
TreeItem(const QList<QVariant> &data, TreeItem *parent = 0);
~TreeItem();
void appendChild(TreeItem *child);
TreeItem *child(int row);
int childCount() const;
int columnCount() const;
QVariant data(int column) const;
int row() const;
TreeItem *parent();
private:
QList<TreeItem*> childItems;
QList<QVariant> itemData;
TreeItem *parentItem;
};
#endif
#ifndef TREEMODEL_H
#define TREEMODEL_H
#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>
class TreeItem;
class TreeModel : public QAbstractItemModel
{
Q_OBJECT
public:
TreeModel(QObject *parent = 0);
~TreeModel();
QVariant data(const QModelIndex &index, int role) const;
// ЭТО ДЛЯ РЕДАКТИРОВАНИЯ Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const;
QModelIndex index(int row, int column,
const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &index) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
private:
void init(int id = 0, TreeItem *parent = 0);
TreeItem *rootItem;
};
#endif
Read-Only Access
To provide read-only access to data provided by a model, the following functions must be implemented in the model's subclass:
flags()
Used by other components to obtain information about each item provided by the model. In many models, the combination of flags should include Qt::ItemIsEnabled and Qt::ItemIsSelectable.
data()
Used to supply item data to views and delegates. Generally, models only need to supply data for Qt::DisplayRole and any application-specific user roles, but it is also good practice to provide data for Qt::ToolTipRole, Qt::AccessibleTextRole, and Qt::AccessibleDescriptionRole.
headerData()
Provides views with information to show in their headers. The information is only retrieved by views that can display header information.
rowCount()
Provides the number of rows of data exposed by the model.
These four functions must be implemented in all types of model, including list models (QAbstractListModel subclasses) and table models (QAbstractTableModel subclasses).
Additionally, the following functions must be implemented in direct subclasses of QAbstractTableModel and QAbstractItemModel:
columnCount()
Provides the number of columns of data exposed by the model. List models do not provide this function because it is already implemented in QAbstractListModel.
Тоесть определить :
При использовании ТОЛЬКО ДЛЯ ЧТЕНИЯ
void setRootNode(Node *node),
QmodelIndex index(int row, int column, const QmodelIndex &parent ) const,
QmodelIndex parent(const QmodelIndex &parent ) const,
int rowCount(const QmodelIndex &parent) const,
int columnCount(const QmodelIndex &parent) const,
QVariant data(const QmodelIndex &index, int role) const,
QVariant headerData(int section, Qt:Orientation orientation, int role) const,
и в приватных объявить:
Node *nodeFromIndex(const QmodelIndex &index) const;
Node *rootNode;
При редактировании : - сначала попробуй только чтение потом само
пойдет. (Только если трава хорошая )
:wink:
добавлено спустя 1 минуту: Фуууух ............ даже вспотел пока набирал