Skip to content

Commit a9c7218

Browse files
committed
Add sample project
1 parent a2efb3a commit a9c7218

File tree

8 files changed

+411
-0
lines changed

8 files changed

+411
-0
lines changed

MyDemoProject/MyDemoProject.pro

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Add more folders to ship with the application, here
2+
folder_01.source = qml/MyDemoProject
3+
folder_01.target = qml
4+
DEPLOYMENTFOLDERS = folder_01
5+
6+
# The .cpp file which was generated for your project. Feel free to hack it.
7+
SOURCES += main.cpp \
8+
qmldebugger.cpp
9+
10+
HEADERS += \
11+
qmldebugger.h
12+
13+
# Please do not modify the following two lines. Required for deployment.
14+
include(qtquick2applicationviewer/qtquick2applicationviewer.pri)
15+
qtcAddDeployment()

MyDemoProject/main.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <QtGui/QGuiApplication>
2+
#include <QtQml>
3+
#include "qtquick2applicationviewer.h"
4+
5+
#include "qmldebugger.h"
6+
7+
int main(int argc, char *argv[])
8+
{
9+
QGuiApplication app(argc, argv);
10+
11+
qmlRegisterType<QMLDebugger>("MyDemoLibrary", 1, 0, "QMLDebugger");
12+
13+
QtQuick2ApplicationViewer viewer;
14+
viewer.setMainQmlFile(QStringLiteral("qml/MyDemoProject/main.qml"));
15+
viewer.showExpanded();
16+
17+
return app.exec();
18+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import QtQuick 2.0
2+
import MyDemoLibrary 1.0
3+
4+
Rectangle {
5+
id: mainRectangle
6+
7+
width: 360
8+
height: 360
9+
color: "silver"
10+
11+
Text {
12+
id: textElement
13+
color: "#d71f1f"
14+
text: qsTr("Hello World")
15+
font.bold: true
16+
font.italic: true
17+
font.underline: true
18+
style: Text.Raised
19+
horizontalAlignment: Text.AlignHCenter
20+
font.pointSize: 16
21+
anchors.top: parent.top
22+
anchors.left: parent.left
23+
anchors.right: parent.right
24+
}
25+
26+
QMLDebugger {
27+
id: qmlDebugger
28+
}
29+
30+
Component.onCompleted: {
31+
console.log("Debug mainRectangle:");
32+
console.log(qmlDebugger.properties(mainRectangle));
33+
console.log("Debug textElement:");
34+
console.log(qmlDebugger.properties(textElement, false));
35+
}
36+
}

MyDemoProject/qmldebugger.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "qmldebugger.h"
2+
3+
#include <QMetaObject>
4+
#include <QMetaProperty>
5+
6+
QString QMLDebugger::properties(QQuickItem *item, bool linebreak)
7+
{
8+
const QMetaObject *meta = item->metaObject();
9+
10+
QHash<QString, QVariant> list;
11+
for (int i = 0; i < meta->propertyCount(); i++)
12+
{
13+
QMetaProperty property = meta->property(i);
14+
const char* name = property.name();
15+
QVariant value = item->property(name);
16+
list[name] = value;
17+
}
18+
19+
QString out;
20+
QHashIterator<QString, QVariant> i(list);
21+
while (i.hasNext()) {
22+
i.next();
23+
if (!out.isEmpty())
24+
{
25+
out += ", ";
26+
if (linebreak) out += "\n";
27+
}
28+
out.append(i.key());
29+
out.append(": ");
30+
out.append(i.value().toString());
31+
}
32+
return out;
33+
}

MyDemoProject/qmldebugger.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef QMLDEBUGGER_H
2+
#define QMLDEBUGGER_H
3+
4+
#include <QObject>
5+
#include <QQuickItem>
6+
7+
class QMLDebugger : public QObject
8+
{
9+
Q_OBJECT
10+
public:
11+
Q_INVOKABLE static QString properties(QQuickItem *item, bool linebreak = true);
12+
13+
};
14+
15+
#endif // QMLDEBUGGER_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// checksum 0x4f6f version 0x90005
2+
/*
3+
This file was generated by the Qt Quick 2 Application wizard of Qt Creator.
4+
QtQuick2ApplicationViewer is a convenience class containing mobile device specific
5+
code such as screen orientation handling. Also QML paths and debugging are
6+
handled here.
7+
It is recommended not to modify this file, since newer versions of Qt Creator
8+
may offer an updated version of it.
9+
*/
10+
11+
#include "qtquick2applicationviewer.h"
12+
13+
#include <QtCore/QCoreApplication>
14+
#include <QtCore/QDir>
15+
#include <QtQml/QQmlEngine>
16+
17+
class QtQuick2ApplicationViewerPrivate
18+
{
19+
QString mainQmlFile;
20+
friend class QtQuick2ApplicationViewer;
21+
static QString adjustPath(const QString &path);
22+
};
23+
24+
QString QtQuick2ApplicationViewerPrivate::adjustPath(const QString &path)
25+
{
26+
#if defined(Q_OS_MAC)
27+
if (!QDir::isAbsolutePath(path))
28+
return QString::fromLatin1("%1/../Resources/%2")
29+
.arg(QCoreApplication::applicationDirPath(), path);
30+
#elif defined(Q_OS_BLACKBERRY)
31+
if (!QDir::isAbsolutePath(path))
32+
return QString::fromLatin1("app/native/%1").arg(path);
33+
#elif !defined(Q_OS_ANDROID)
34+
QString pathInInstallDir =
35+
QString::fromLatin1("%1/../%2").arg(QCoreApplication::applicationDirPath(), path);
36+
if (QFileInfo(pathInInstallDir).exists())
37+
return pathInInstallDir;
38+
pathInInstallDir =
39+
QString::fromLatin1("%1/%2").arg(QCoreApplication::applicationDirPath(), path);
40+
if (QFileInfo(pathInInstallDir).exists())
41+
return pathInInstallDir;
42+
#endif
43+
return path;
44+
}
45+
46+
QtQuick2ApplicationViewer::QtQuick2ApplicationViewer(QWindow *parent)
47+
: QQuickView(parent)
48+
, d(new QtQuick2ApplicationViewerPrivate())
49+
{
50+
connect(engine(), SIGNAL(quit()), SLOT(close()));
51+
setResizeMode(QQuickView::SizeRootObjectToView);
52+
}
53+
54+
QtQuick2ApplicationViewer::~QtQuick2ApplicationViewer()
55+
{
56+
delete d;
57+
}
58+
59+
void QtQuick2ApplicationViewer::setMainQmlFile(const QString &file)
60+
{
61+
d->mainQmlFile = QtQuick2ApplicationViewerPrivate::adjustPath(file);
62+
#ifdef Q_OS_ANDROID
63+
setSource(QUrl(QLatin1String("assets:/")+d->mainQmlFile));
64+
#else
65+
setSource(QUrl::fromLocalFile(d->mainQmlFile));
66+
#endif
67+
}
68+
69+
void QtQuick2ApplicationViewer::addImportPath(const QString &path)
70+
{
71+
engine()->addImportPath(QtQuick2ApplicationViewerPrivate::adjustPath(path));
72+
}
73+
74+
void QtQuick2ApplicationViewer::showExpanded()
75+
{
76+
#if defined(Q_WS_SIMULATOR) || defined(Q_OS_QNX)
77+
showFullScreen();
78+
#else
79+
show();
80+
#endif
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// checksum 0xfde6 version 0x90005
2+
/*
3+
This file was generated by the Qt Quick 2 Application wizard of Qt Creator.
4+
QtQuick2ApplicationViewer is a convenience class containing mobile device specific
5+
code such as screen orientation handling. Also QML paths and debugging are
6+
handled here.
7+
It is recommended not to modify this file, since newer versions of Qt Creator
8+
may offer an updated version of it.
9+
*/
10+
11+
#ifndef QTQUICK2APPLICATIONVIEWER_H
12+
#define QTQUICK2APPLICATIONVIEWER_H
13+
14+
#include <QtQuick/QQuickView>
15+
16+
class QtQuick2ApplicationViewer : public QQuickView
17+
{
18+
Q_OBJECT
19+
20+
public:
21+
explicit QtQuick2ApplicationViewer(QWindow *parent = 0);
22+
virtual ~QtQuick2ApplicationViewer();
23+
24+
void setMainQmlFile(const QString &file);
25+
void addImportPath(const QString &path);
26+
27+
void showExpanded();
28+
29+
private:
30+
class QtQuick2ApplicationViewerPrivate *d;
31+
};
32+
33+
#endif // QTQUICK2APPLICATIONVIEWER_H

0 commit comments

Comments
 (0)