Skip to content

Commit 94678ba

Browse files
author
Kimmo
committed
Add QtQuick example exercise
1 parent 7daeb2c commit 94678ba

File tree

12 files changed

+232
-0
lines changed

12 files changed

+232
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ qrc_*.cpp
1212
ui_*.h
1313
Makefile*
1414
*build-*
15+
*.qmlc
1516

1617
# QtCreator
1718

CookieClicker/CookieClicker.pro

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
TEMPLATE = subdirs
2+
SUBDIRS += \
3+
src \
4+
test_runner
5+
6+
test_runner.depends = src
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import QtQuick 2.9
2+
import QtQuick.Dialogs 1.1
3+
import QtQuick.Controls 1.0
4+
5+
Item {
6+
id: cookieClicker
7+
anchors.fill: parent
8+
9+
// Write correct properties and aliases here
10+
// BEGIN SOLUTION
11+
property int click: 0
12+
property string welcome : "Generic welcome!"
13+
property alias status: cookieText
14+
property string clickedType : "Cookie"
15+
// END SOLUTION
16+
17+
Image {
18+
id: image
19+
anchors.fill: parent
20+
fillMode: Image.PreserveAspectFit
21+
source: "qrc:/cookie.jpg"
22+
23+
// Write something clickable here!
24+
// BEGIN SOLUTION
25+
MouseArea {
26+
anchors.fill: parent
27+
onClicked: {
28+
click++;
29+
cookieText.text = clickedType + "s clicked: " + click;
30+
}
31+
}
32+
// END SOLUTION
33+
}
34+
35+
Text {
36+
anchors.horizontalCenter: parent.horizontalCenter
37+
id: cookieText
38+
x: 0
39+
y: 0
40+
text: welcome
41+
color: "brown"
42+
font.pixelSize: 30
43+
}
44+
}

CookieClicker/src/cookie.jpg

64.6 KB
Loading

CookieClicker/src/main.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <QGuiApplication>
2+
#include <QQmlApplicationEngine>
3+
4+
int main(int argc, char *argv[])
5+
{
6+
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
7+
8+
QGuiApplication app(argc, argv);
9+
10+
QQmlApplicationEngine engine;
11+
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
12+
if (engine.rootObjects().isEmpty())
13+
return -1;
14+
return app.exec();
15+
}

CookieClicker/src/main.qml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import QtQuick 2.0
2+
import QtQuick.Window 2.2
3+
4+
Window {
5+
id: window
6+
width: 620
7+
visible: true
8+
height: 420
9+
title: qsTr("Hello Cookie")
10+
11+
CookieClicker {
12+
welcome: "Cookie clicker!"
13+
clickedType: "Chocolate cookie"
14+
width: 350
15+
height: 200
16+
anchors.centerIn: parent
17+
}
18+
19+
}

CookieClicker/src/qml.qrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<RCC>
2+
<qresource prefix="/">
3+
<file>main.qml</file>
4+
<file>CookieClicker.qml</file>
5+
<file>cookie.jpg</file>
6+
</qresource>
7+
</RCC>

CookieClicker/src/src.pro

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
QT += quick
2+
TARGET = main
3+
CONFIG += c++11
4+
5+
# The following define makes your compiler emit warnings if you use
6+
# any feature of Qt which as been marked deprecated (the exact warnings
7+
# depend on your compiler). Please consult the documentation of the
8+
# deprecated API in order to know how to port your code away from it.
9+
DEFINES += QT_DEPRECATED_WARNINGS
10+
11+
# You can also make your code fail to compile if you use deprecated APIs.
12+
# In order to do so, uncomment the following line.
13+
# You can also select to disable deprecated APIs only up to a certain version of Qt.
14+
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
15+
16+
SOURCES += main.cpp
17+
18+
RESOURCES += qml.qrc
19+
20+
# Additional import path used to resolve QML modules in Qt Creator's code model
21+
QML_IMPORT_PATH =
22+
23+
# Additional import path used to resolve QML modules just for Qt Quick Designer
24+
QML_DESIGNER_IMPORT_PATH =
25+
26+
# Default rules for deployment.
27+
qnx: target.path = /tmp/$${TARGET}/bin
28+
else: unix:!android: target.path = /opt/$${TARGET}/bin
29+
!isEmpty(target.path): INSTALLS += target
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import QtQuick 2.0
2+
import "../src/" 1.00
3+
4+
CookieClicker {
5+
welcome: "Cookie clicker!"
6+
clickedType: "Chocolate cookie"
7+
width: 350
8+
height: 200
9+
anchors.centerIn: parent
10+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
TEMPLATE = app
2+
TARGET = tst_cookieclicker
3+
QT += quick core testlib
4+
CONFIG += c++11 warn_on qmltestcase
5+
CONFIG -= app_bundle
6+
7+
# The following define makes your compiler emit warnings if you use
8+
# any feature of Qt which as been marked deprecated (the exact warnings
9+
# depend on your compiler). Please consult the documentation of the
10+
# deprecated API in order to know how to port your code away from it.
11+
DEFINES += QT_DEPRECATED_WARNINGS
12+
13+
# You can also make your code fail to compile if you use deprecated APIs.
14+
# In order to do so, uncomment the following line.
15+
# You can also select to disable deprecated APIs only up to a certain version of Qt.
16+
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
17+
18+
SOURCES += tst_cookieclicker.cpp
19+
20+
RESOURCES += \
21+
$$PWD/../src/qml.qrc
22+
23+
INCLUDEPATH += $$PWD/../src
24+
25+
# Additional import path used to resolve QML modules in Qt Creator's code model
26+
QML2_IMPORT_PATH = $$PWD/../src/qml.qrc
27+
28+
# Additional import path used to resolve QML modules just for Qt Quick Designer
29+
QML_DESIGNER_IMPORT_PATH =
30+
31+
# Default rules for deployment.
32+
qnx: target.path = /tmp/$${TARGET}/bin
33+
else: unix:!android: target.path = /opt/$${TARGET}/bin
34+
!isEmpty(target.path): INSTALLS += target
35+
36+
OTHER_FILES += \
37+
tst_cookieclicker.qml \
38+
CookieClickerTest.qml

0 commit comments

Comments
 (0)