Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- classe TreeItem;
- class TreeModel : public QAbstractItemModel
- {
- Q_OBJECT
- public:
- explicit TreeModel(const QString &data, QObject *parent = 0);
- ~TreeModel();
- QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
- Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
- QVariant headerData(int section, Qt::Orientation orientation,
- int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
- QModelIndex index(int row, int column,
- const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
- QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE;
- int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
- int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
- QHash<int, QByteArray> roleNames() const;
- enum TreeRoles {
- titleRole,
- summaryRole
- };
- private:
- void setupModelData(const QStringList &lines, TreeItem *parent);
- TreeItem *rootItem;
- };
- //! [0]
- TreeModel::TreeModel(const QString &data, QObject *parent)
- : QAbstractItemModel(parent)
- {
- QList<QVariant> rootData;
- rootData << "Title" << "Summary";
- rootItem = new TreeItem(rootData);
- setupModelData(data.split(QString("n")), rootItem);
- }
- //! [0]
- //! [1]
- TreeModel::~TreeModel()
- {
- delete rootItem;
- }
- //! [1]
- //! [2]
- int TreeModel::columnCount(const QModelIndex &parent) const
- {
- if (parent.isValid())
- return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
- else
- return rootItem->columnCount();
- }
- //! [2]
- //! [3]
- //! [3]
- //! [4]
- Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
- {
- if (!index.isValid())
- return 0;
- return QAbstractItemModel::flags(index);
- }
- //! [4]
- //! [5]
- QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
- int role) const
- {
- if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
- return rootItem->data(section);
- return QVariant();
- }
- //! [5]
- //! [6]
- QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
- const
- {
- if (!hasIndex(row, column, parent))
- return QModelIndex();
- TreeItem *parentItem;
- if (!parent.isValid())
- parentItem = rootItem;
- else
- parentItem = static_cast<TreeItem*>(parent.internalPointer());
- TreeItem *childItem = parentItem->child(row);
- if (childItem)
- return createIndex(row, column, childItem);
- else
- return QModelIndex();
- }
- //! [6]
- //! [7]
- QModelIndex TreeModel::parent(const QModelIndex &index) const
- {
- if (!index.isValid())
- return QModelIndex();
- TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
- TreeItem *parentItem = childItem->parentItem();
- if (parentItem == rootItem)
- return QModelIndex();
- return createIndex(parentItem->row(), 0, parentItem);
- }
- //! [7]
- //! [8]
- int TreeModel::rowCount(const QModelIndex &parent) const
- {
- TreeItem *parentItem;
- if (parent.column() > 0)
- return 0;
- if (!parent.isValid())
- parentItem = rootItem;
- else
- parentItem = static_cast<TreeItem*>(parent.internalPointer());
- return parentItem->childCount();
- }
- //! [8]
- void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
- {
- QList<TreeItem*> parents;
- QList<int> indentations;
- parents << parent;
- indentations << 0;
- int number = 0;
- while (number < lines.count()) {
- int position = 0;
- while (position < lines[number].length()) {
- if (lines[number].at(position) != ' ')
- break;
- position++;
- }
- QString lineData = lines[number].mid(position).trimmed();
- if (!lineData.isEmpty()) {
- // Read the column data from the rest of the line.
- QStringList columnStrings = lineData.split("t", QString::SkipEmptyParts);
- QList<QVariant> columnData;
- for (int column = 0; column < columnStrings.count(); ++column)
- columnData << columnStrings[column];
- if (position > indentations.last()) {
- // The last child of the current parent is now the new parent
- // unless the current parent has no children.
- if (parents.last()->childCount() > 0) {
- parents << parents.last()->child(parents.last()->childCount()-1);
- indentations << position;
- }
- } else {
- while (position < indentations.last() && parents.count() > 0) {
- parents.pop_back();
- indentations.pop_back();
- }
- }
- // Append a new item to the current parent's list of children.
- parents.last()->appendChild(new TreeItem(columnData, parents.last()));
- }
- ++number;
- }
- }
- QVariant TreeModel::data(const QModelIndex & index, int role) const {
- if (!index.isValid())
- return QVariant();
- TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
- if (index.isValid()) {
- switch (role) {
- case titleRole:
- return item->data(0);
- case summaryRole:
- return item->data(0);
- }
- }
- return QVariant();
- }
- QHash<int, QByteArray> TreeModel::roleNames() const {
- QHash<int, QByteArray> roles;
- roles[titleRole] = "title";
- roles[summaryRole] = "summary";
- return roles;
- }
- class TreeItem
- {
- public:
- explicit TreeItem(const QList<QVariant> &data, TreeItem *parentItem = 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 *parentItem();
- private:
- QList<TreeItem*> m_childItems;
- QList<QVariant> m_itemData;
- TreeItem *m_parentItem;
- };
- //! [0]
- TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent)
- {
- m_parentItem = parent;
- m_itemData = data;
- }
- //! [0]
- //! [1]
- TreeItem::~TreeItem()
- {
- qDeleteAll(m_childItems);
- }
- //! [1]
- //! [2]
- void TreeItem::appendChild(TreeItem *item)
- {
- m_childItems.append(item);
- }
- //! [2]
- //! [3]
- TreeItem *TreeItem::child(int row)
- {
- return m_childItems.value(row);
- }
- //! [3]
- //! [4]
- int TreeItem::childCount() const
- {
- return m_childItems.count();
- }
- //! [4]
- //! [5]
- int TreeItem::columnCount() const
- {
- return m_itemData.count();
- }
- //! [5]
- //! [6]
- QVariant TreeItem::data(int column) const
- {
- return m_itemData.value(column);
- }
- //! [6]
- //! [7]
- TreeItem *TreeItem::parentItem()
- {
- return m_parentItem;
- }
- //! [7]
- //! [8]
- int TreeItem::row() const
- {
- if (m_parentItem)
- return m_parentItem->m_childItems.indexOf(const_cast<TreeItem*>(this));
- return 0;
- }
- //! [8]
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include <QtQml>
- #include <QQuickView>
- #include "QQuickWidget"
- #include <QFile>
- #include "treemodel.h"
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- QQuickWidget *view = new QQuickWidget;
- QQmlContext *ctxt = view->rootContext();
- QFile file("default.txt");
- file.open(QIODevice::ReadOnly);
- TreeModel model(file.readAll());
- file.close();
- ctxt->setContextProperty("myModel", &model);
- view->setSource(QUrl::fromLocalFile("main.qml"));
- view->setGeometry(0, 200, 600, 400);
- view->setResizeMode(QQuickWidget::SizeRootObjectToView);
- ui->dockWidget->setWidget(view);
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- }
- import QtQuick 2.0
- import QtQml.Models 2.2
- import QtQuick.Controls 1.5
- Rectangle {
- id : rootRectangle
- visible: true
- color:"red"
- TreeView {
- id: view
- anchors.fill: parent
- model: myModel
- TableViewColumn {
- title: "title"
- role: "title"
- resizable: true
- }
- TableViewColumn {
- title: "summary"
- role: "summary"
- resizable: true
- horizontalAlignment : Text.AlignRight
- width: 70
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement