PDA

View Full Version : Problems with QtQuick wizard generated project



enrico5th
16th March 2014, 10:52
Hello,

I'm trying to add a C++ defined type to a simple QML file, but when I start debugging the application I receive the following error: "module "APP_CHILD" is not installed
import APP_CHILD 1.0".

The application I'm running is derived from a wizard generated QtQuick application which is meant to handle native system events some way.

The program is structured as follows:



qtquick2applicationviewer/qtquick2applicationviewer.cpp (Wizard generated)
qtquick2applicationviewer/qtquick2applicationviewer.h (Wizard generated)
qtquick2applicationviewer/qtquick2applicationviewer.pri (Wizard generated)
qml/provaintegrazione/main.qml
app.cpp
app.h
main.cpp
provaintegrazione.pro
provaintegrazione.pro.user


and this is the code of the files I modified by myself:

app.h



#ifndef APP_H
#define APP_H

#include <QTimer>
#include "qtquick2applicationviewer.h"

class cAppChild : public QObject
{
Q_OBJECT

QTimer timer;
bool check;

public:
cAppChild();
virtual ~cAppChild(){}

private slots:
void change(){ check = !check; timer.start(); }
};

class cApp : public QtQuick2ApplicationViewer
{
Q_OBJECT

bool nativeEvent(const QByteArray &eventType, void *message, long *result);

public:
explicit cApp(QWindow *parent = 0);
virtual ~cApp();
};

#endif // APP_H




app.cpp



#include <QtDeclarative>
#include "app.h"

cAppChild::cAppChild()
{
check = false;
timer.start();
//Connections for attach/detach notification
connect(&timer, SIGNAL(timeout()), this, SLOT(change()));
}



cApp::cApp(QWindow *parent) : QtQuick2ApplicationViewer(parent)
{
}

cApp::~cApp()
{
}

bool cApp::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
if( eventType == "windows_generic_MSG" )
{
// Some kind of event handling.
}
return false;
}




main.cpp



#include <QtGui/QGuiApplication>
#include <QtDeclarative>
#include "app.h"

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

qmlRegisterType<cAppChild>("APP_CHILD", 1, 0, "appchild");

cAppChild myApp;

cApp viewer;
viewer.setMainQmlFile(QStringLiteral("qml/provaintegrazione/main.qml"));
viewer.showExpanded();

return app.exec();
}


qml/provaintegrazione/main.qml



import QtQuick 2.0
import QtQuick.Controls 1.0
import APP_CHILD 1.0

ApplicationWindow
{
id: main_window
Rectangle
{
id: pippo
width: 360
height: 360
color: "red"
}
}



Can anybody try to use the code above on his QtCreator and explain me how to make the qml code recognize the APP_CHILD module?

anda_skoa
16th March 2014, 11:59
That looks OK. Are you sure you are running the actual application, not just "running" the QML file?

Cheers,
_

enrico5th
16th March 2014, 13:15
I didn't understand your question. The QML file should act as UI and use my C++ class registered as a new QML type. Now the main.cpp file was created by the QtCreator wizard, I just added the type registration for QML and used a class inherited by the class within qtquick2applicationviewer.h in order to use my own nativeEvent() function in the future. What I don't understand is why the import statement inside main.qml file is not recognized. Could you please try to create a QtQuick project like this, add my files and see if everything works on your side? Maybe an experienced Qt programmer like you could understand the problem better than I can do.

anda_skoa
16th March 2014, 15:11
I didn't understand your question.

you wrote "when I start debugging" and I was asking what exactly you were doing.
Did you run the application or just "run" the QML file.



What I don't understand is why the import statement inside main.qml file is not recognized.

It should, hence my question to narrow down possible errors.


Could you please try to create a QtQuick project like this, add my files and see if everything works on your side? Maybe an experienced Qt programmer like you could understand the problem better than I can do.

I can do that if you attach a buildable example.

Cheers,
_

enrico5th
16th March 2014, 16:04
I run the whole application.
You can find the whole project attached to this post.

anda_skoa
16th March 2014, 18:18
After I got it to build, it worked for me.

10131

Cheers,
_

enrico5th
17th March 2014, 08:27
Does it work without any modification? When I build and run it, a little white window appears on the screen (instead of a big red rectangle), then I look at the Application Output window and the message "module "APP_CHILD" is not installed import APP_CHILD 1.0" has been output there. I tried on two different platform and I got the same problem. I'm now asking to myself what's the difference between my platform and yours.

anda_skoa
17th March 2014, 11:03
Lets try with a very simple example

main.cpp


#include <QtQml>

int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);

qmlRegisterType<QTimer>("APP_CHILD", 1, 0, "AppChild");

QQmlEngine engine;

QQmlComponent component(&engine);
component.setData("import APP_CHILD 1.0\n\nAppChild { }", QUrl());
qDebug() << "created object=" << component.create();

return 0;
}


appchildtest.pro


TEMPLATE = app
TARGET = appchildtest
INCLUDEPATH += .

# Input
SOURCES += main.cpp

QT += qml


Executing that should give you an output like this:

created object= QTimer(0xc2b250)

Once you've veryfied that, add you class and register it instead of QTimer.

Cheers,
_

enrico5th
17th March 2014, 14:02
Your example works perfectly, this makes me think there's some problem in the way I've attached the main.qml file in my project. But that was done automatically by the Qt Quick wizard for projects creation. This thing is making me crazy!!!

anda_skoa
17th March 2014, 15:55
Have you tried running the program from a terminal/shell?

Just to make sure you are actually running the program, not just "running" the QML file.

Cheers,
_