aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quick/tableview/gameoflife
diff options
context:
space:
mode:
Diffstat (limited to 'examples/quick/tableview/gameoflife')
-rw-r--r--examples/quick/tableview/gameoflife/doc/src/gameoflife.qdoc6
-rw-r--r--examples/quick/tableview/gameoflife/gameoflife.pro5
-rw-r--r--examples/quick/tableview/gameoflife/gameoflifemodel.h2
-rw-r--r--examples/quick/tableview/gameoflife/main.cpp4
-rw-r--r--examples/quick/tableview/gameoflife/main.qml8
5 files changed, 15 insertions, 10 deletions
diff --git a/examples/quick/tableview/gameoflife/doc/src/gameoflife.qdoc b/examples/quick/tableview/gameoflife/doc/src/gameoflife.qdoc
index 069513636a..b61ba90a58 100644
--- a/examples/quick/tableview/gameoflife/doc/src/gameoflife.qdoc
+++ b/examples/quick/tableview/gameoflife/doc/src/gameoflife.qdoc
@@ -50,9 +50,6 @@
the view should be scrolled to.
\snippet tableview/gameoflife/main.qml model
- The model that we use is a custom C++ class that we register
- in the QML system:
- \snippet tableview/gameoflife/main.cpp registertype
\section1 The C++ Model
@@ -61,7 +58,8 @@
used as the model of our TableView component. Therefore, it needs to
implement some functions so the TableView component can interact with
the model. As you can see in the \c private part of the class, the model
- uses a fixed-size array to store the current state of all the cells.
+ uses a fixed-size array to store the current state of all the cells. We
+ also use the QML_ELEMENT macro in order to expose the class to QML.
\snippet tableview/gameoflife/gameoflifemodel.cpp modelsize
Here, the \c rowCount and \c columnCount methods are implemented so
diff --git a/examples/quick/tableview/gameoflife/gameoflife.pro b/examples/quick/tableview/gameoflife/gameoflife.pro
index 98050b0d79..9c47a9b530 100644
--- a/examples/quick/tableview/gameoflife/gameoflife.pro
+++ b/examples/quick/tableview/gameoflife/gameoflife.pro
@@ -1,6 +1,11 @@
TEMPLATE = app
QT += quick qml
+
+CONFIG += qmltypes
+QML_IMPORT_NAME = GameOfLifeModel
+QML_IMPORT_MAJOR_VERSION = 1
+
SOURCES += \
main.cpp \
gameoflifemodel.cpp
diff --git a/examples/quick/tableview/gameoflife/gameoflifemodel.h b/examples/quick/tableview/gameoflife/gameoflifemodel.h
index 3ea1469861..161e0b2cfc 100644
--- a/examples/quick/tableview/gameoflife/gameoflifemodel.h
+++ b/examples/quick/tableview/gameoflife/gameoflifemodel.h
@@ -54,11 +54,13 @@
#include <array>
#include <QAbstractTableModel>
#include <QPoint>
+#include <QtQml/qqml.h>
//! [modelclass]
class GameOfLifeModel : public QAbstractTableModel
{
Q_OBJECT
+ QML_ELEMENT
Q_ENUMS(Roles)
public:
diff --git a/examples/quick/tableview/gameoflife/main.cpp b/examples/quick/tableview/gameoflife/main.cpp
index 5101880b06..1500efdcca 100644
--- a/examples/quick/tableview/gameoflife/main.cpp
+++ b/examples/quick/tableview/gameoflife/main.cpp
@@ -58,10 +58,6 @@ int main(int argc, char *argv[])
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
- //! [registertype]
- qmlRegisterType<GameOfLifeModel>("GameOfLifeModel", 1, 0, "GameOfLifeModel");
- //! [registertype]
-
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
diff --git a/examples/quick/tableview/gameoflife/main.qml b/examples/quick/tableview/gameoflife/main.qml
index 90be69b9c0..fb3195892f 100644
--- a/examples/quick/tableview/gameoflife/main.qml
+++ b/examples/quick/tableview/gameoflife/main.qml
@@ -48,6 +48,7 @@
**
****************************************************************************/
+import QtQml 2.12
import QtQuick 2.12
import QtQuick.Window 2.3
import QtQuick.Controls 2.2
@@ -81,11 +82,14 @@ ApplicationWindow {
implicitWidth: 15
implicitHeight: 15
- color: model.value ? "#f3f3f4" : "#b5b7bf"
+ required property var model
+ required property bool value
+
+ color: value ? "#f3f3f4" : "#b5b7bf"
MouseArea {
anchors.fill: parent
- onClicked: model.value = !model.value
+ onClicked: parent.model.value = !parent.value
}
}
//! [tableview]