Skip to content

Commit

Permalink
use custom type for data value in qml delegate
Browse files Browse the repository at this point in the history
We register a custom type (class) with the QVariant metasystem and pass
that back from the QAbstractItemModel into qml.

For demonstration purposes we chose to use this custom type to
additionally transport the indentation from the input file as an int.
  • Loading branch information
hvoigt committed Oct 19, 2016
1 parent eee1606 commit b0b8b9d
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 3 deletions.
11 changes: 11 additions & 0 deletions TreeDelegate.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import QtQuick 2.0
import hvoigt.net 1.0

Item {
Text {
anchors.fill: parent
color: styleData.textColor
elide: styleData.elideMode
text: styleData.value.indentation + ": " + styleData.value.text
}
}
37 changes: 37 additions & 0 deletions customtype.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "customtype.h"

CustomType::CustomType(QObject *parent) : QObject(parent), myIndentation(0)
{
}

CustomType::CustomType(const CustomType &other)
{
myText = other.myText;
myIndentation = other.myIndentation;
}

CustomType::~CustomType()
{
}

QString CustomType::text()
{
return myText;
}

void CustomType::setText(QString text)
{
myText = text;
emit textChanged();
}

int CustomType::indentation()
{
return myIndentation;
}

void CustomType::setIndentation(int indentation)
{
myIndentation = indentation;
emit indentationChanged();
}
35 changes: 35 additions & 0 deletions customtype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef CUSTOMTYPE_H
#define CUSTOMTYPE_H

#include <QObject>

class CustomType : public QObject
{
Q_OBJECT

Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
Q_PROPERTY(int indentation READ indentation WRITE setIndentation NOTIFY indentationChanged)

public:
explicit CustomType(QObject *parent = 0);
CustomType(const CustomType &other);
~CustomType();

QString text();
void setText(QString text);

int indentation();
void setIndentation(int indentation);

signals:
void textChanged();
void indentationChanged();

private:
QString myText;
int myIndentation;
};

Q_DECLARE_METATYPE(CustomType)

#endif // CUSTOMTYPE_H
3 changes: 3 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
****************************************************************************/

#include "treemodel.h"
#include "customtype.h"

#include <QGuiApplication>
#include <QQmlApplicationEngine>
Expand All @@ -58,6 +59,8 @@ int main(int argc, char *argv[])

QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("theModel",&model);
qmlRegisterType<CustomType>("hvoigt.net", 1, 0, "CustomType");

engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

return app.exec();
Expand Down
1 change: 1 addition & 0 deletions main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ApplicationWindow {
TreeView {
anchors.fill: parent
model: theModel
itemDelegate: TreeDelegate {}

TableViewColumn {
role: "title"
Expand Down
14 changes: 13 additions & 1 deletion treemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
#include "treeitem.h"
#include "treemodel.h"

#include "customtype.h"

#include <QStringList>

//! [0]
Expand Down Expand Up @@ -176,6 +178,16 @@ QHash<int, QByteArray> TreeModel::roleNames() const
return m_roleNameMapping;
}

QVariant TreeModel::newCustomType(const QString &text, int position)
{
CustomType *t = new CustomType(this);
t->setText(text);
t->setIndentation(position);
QVariant v;
v.setValue(t);
return v;
}

void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
{
QList<TreeItem*> parents;
Expand All @@ -200,7 +212,7 @@ void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
QList<QVariant> columnData;
for (int column = 0; column < columnStrings.count(); ++column)
columnData << columnStrings[column];
columnData << newCustomType(columnStrings[column], position);

if (position > indentations.last()) {
// The last child of the current parent is now the new parent
Expand Down
1 change: 1 addition & 0 deletions treemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class TreeModel : public QAbstractItemModel
QHash<int, QByteArray> roleNames() const override;

private:
QVariant newCustomType(const QString &text, int position);
void setupModelData(const QStringList &lines, TreeItem *parent);

TreeItem *rootItem;
Expand Down
6 changes: 4 additions & 2 deletions treeview.pro
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ QT += qml quick
CONFIG += c++11

HEADERS = treeitem.h \
treemodel.h
treemodel.h \
customtype.h
RESOURCES = treeview.qrc
SOURCES = treeitem.cpp \
treemodel.cpp \
main.cpp
main.cpp \
customtype.cpp

# install
#target.path = $$[QT_INSTALL_EXAMPLES]/widgets/itemviews/simpletreemodel
Expand Down
1 change: 1 addition & 0 deletions treeview.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
<qresource prefix="/">
<file>default.txt</file>
<file>main.qml</file>
<file>TreeDelegate.qml</file>
</qresource>
</RCC>

0 comments on commit b0b8b9d

Please sign in to comment.