Skip to content

Commit 161f36e

Browse files
committed
commandlineparser: Make it possible to load dummydata like qmlscene
The code is copied over from qmlscene.
1 parent 868da21 commit 161f36e

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/build/
2+
/compile_commands.json

src/commandline/commandlineparser.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <QtCore/QJsonDocument>
66
#include <QtCore/QJsonArray>
77
#include <QtCore/QJsonObject>
8+
#include <QtQml/QQmlContext>
89

910
#include <utility>
1011

@@ -72,6 +73,33 @@ void CommandLineParser::setEngine(QQmlEngine* engine)
7273
}
7374
}
7475

76+
void CommandLineParser::loadDummyData(QQmlEngine *engine)
77+
{
78+
const QFileInfo file = ProvidesSomething::self()->filePath().toLocalFile();
79+
const QString pathToQml = file.canonicalPath();
80+
81+
const QDir dir(pathToQml + "/dummydata/", "*.qml");
82+
QStringList list = dir.entryList();
83+
for (int i = 0; i < list.size(); ++i) {
84+
QString qml = list.at(i);
85+
QQmlComponent comp(engine, dir.filePath(qml));
86+
QObject *dummyData = comp.create();
87+
88+
if(comp.isError()) {
89+
const QList<QQmlError> errors = comp.errors();
90+
for (const QQmlError &error : errors)
91+
fprintf(stderr, "%s\n", qPrintable(error.toString()));
92+
}
93+
94+
if (dummyData) {
95+
fprintf(stderr, "Loaded dummy data: %s\n", qPrintable(dir.filePath(qml)));
96+
qml.truncate(qml.length()-4);
97+
engine->rootContext()->setContextProperty(qml, dummyData);
98+
dummyData->setParent(engine);
99+
}
100+
}
101+
}
102+
75103
void CommandLineParser::printHelp()
76104
{
77105
for (auto& optionStruct : std::as_const(_optionsStruct)) {

src/commandline/commandlineparser.h

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class CommandLineParser : public QCommandLineParser {
4646
*/
4747
void setApplication(QCoreApplication* application);
4848

49+
void loadDummyData(QQmlEngine *engine);
50+
4951
private:
5052
struct OptionStruct {
5153
QCommandLineOption option;

src/main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ int main(int argc, char *argv[])
3333
ProvidesSomething::self()->setFilePath(QUrl::fromUserInput(dir.absolutePath()));
3434
}
3535
}
36+
commandLineParser.loadDummyData(&appEngine);
3637

3738
return app.exec();
3839
}

0 commit comments

Comments
 (0)