PDA

View Full Version : Minimal required files for creating a simple Qt Quick project



Binary91
20th December 2014, 12:57
Hi,

I’m on the way to port my applications (desktop) to mobile phones (android).
I read a lot of times, that it’s strongly recommended to use QML instead of desktop Qt for mobile devices, because it would avoid lots of errors, is that right?

Well, I tried starting a new Qt Quick project by using the Qt Quick tutorial. The example code in the tutorial looks like that:

import QtQuick 2.3

Rectangle {
id: simpleButton
color: "grey"
width: 150; height: 75

Text {
id: buttonLabel
anchors.centerIn: parent
text: "button label"
}
}

The problem is, they “compile” or “run” the code above using qmlscene without using any other stuff (c++ source files, headers, resource files, pri files and so on…).
In my case, I’d really like to use QtCreator and so, I have to create a project.
When I do that, it creates a lot of files:
1. the .pro file
2. deployment.pri
3. main.cpp
4. main.qml
5. MainForm.ui.qml

When I run that project, everything works fine, but I don’t think I need all those files when Qt Quick tutorial only uses one qml file.

When I remove all those files except the main.cpp (where the main.qml is loaded) and the main.qml and try to run the project, no errors occure, the executable is created and it runs, but nothing happens. There is no UI created…

Does anyone know why? What am I doing wrong?

That is my project:
.pro file:

TEMPLATE = app

QT += qml quick widgets

SOURCES += main.cpp

DISTFILES += main.qml

main.cpp

#include <QApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QQmlApplicationEngine engine;
engine.load("main.qml");

return app.exec();
}

main.qml:

import QtQuick 2.3

Rectangle {
id: simpleButton
color: "grey"
width: 150; height: 75

Text {
id: buttonLabel
anchors.centerIn: parent
text: "button label"
}
}


Can anyone help me with that? :confused:
Thank you in anticipation!

anda_skoa
20th December 2014, 15:16
Maybe you are running the program from a directory that is not the source directory, e.g. from a shadow build directory.

One way to solve that is to put the QML file into a resource and build it into the app.

Cheers,
_

Binary91
21st December 2014, 11:01
Hi and thank you for your reply!
Well, I just changed the relative paths using absolute ones, but there is still nothing happening. I never had this problems you describe when building projects, so the error is maybe somewhere else...

Did you test the code on your system and it works? That would help me much to know that, because then I really have to check my QtCreator settings or create a resource file...


[EDIT]
I found my problem! When using a rectangle as basic element, I have to use QQuickView to load the QML file and show it manually. Otherwise I have to create a basic Window/ApplicationWindow in the QML file and load it like I did above using QQmlApplicationEngine...

I hope this helps other desperate newbies with the same problem :-P

Greetings