PDA

View Full Version : QML/C++ problem



alenn.masic
4th January 2013, 19:41
Hi all

I'm trying to learn binding QML and C++ but with no luck.

//main.qml


import QtQuick 1.1

Item{
width: 100;
height: 100;
Text{ text: "Bezveze";}
objectName: bb;
}

// main.cpp


#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QObject>

Q_DECL_EXPORT int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDeclarativeView* view = new QDeclarativeView;
view->setSource(QUrl::fromLocalFile("/home/alen/QtSDK/bezveze/qml/bezveze/main.qml"));
view->show();
QObject *object = view->findChild<QObject*>("bb");
object->setProperty("width", 500);
return app.exec();
}

I think this should work since I followed tutorial from Qt site. But I'm getting following error:


no matching function for call to 'QObject::setProperty(const char [6], int)'

How to solve this.

wysota
4th January 2013, 20:31
Does this work?


#include <QtGui/QApplication>
#include <QObject>

Q_DECL_EXPORT int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QObject *object = new QObject(&app);
object->setProperty("width", 500);
return 0;
}

alizadeh91
4th January 2013, 20:33
Look at this: http://doc.qt.digia.com/qt/qtbinding.html , thats so easy

alenn.masic
4th January 2013, 22:11
Does this work?


#include <QtGui/QApplication>
#include <QObject>

Q_DECL_EXPORT int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QObject *object = new QObject(&app);
object->setProperty("width", 500);
return 0;
}

no, I still have the same error


Look at this: http://doc.qt.digia.com/qt/qtbinding.html , thats so easy

I'm already following this tutorial. But I have that error which I posted

wysota
4th January 2013, 22:35
no, I still have the same error
Exactly. Your problem is unrelated to QML.

Hmm... Try adding this to your file:


#include <QVariant>

alenn.masic
4th January 2013, 23:26
Now it just says that program is unexpectedly finished. And btw I'm using Qt 4.8.0 (32 bit) maybe it's relevant.

wysota, can you zip your project and send to me. Maybe I made mistake somewhere.

Added after 15 minutes:

I found this solution and it's working for me: http://stackoverflow.com/questions/6033515/accessing-properties-of-qml-objects-from-c-withoun-q-property-definition

If you have something better I would like to know it :)

Added after 27 minutes:

Heh, with that example I can only change properity but I don't see the window I created in QML. Something is wrong

wysota
4th January 2013, 23:33
Now it just says that program is unexpectedly finished.
This is most probably because findChild() returns 0 for you and you're not checking the pointer before dereferencing it. Items in QtQuick are not children of the view, they are children of the root object of the root context of the declarative engine (phew...) used by the view.

alenn.masic
4th January 2013, 23:48
There, I fixed it:



#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QObject>
#include <QVariant>
#include <QDebug>
#include <QDeclarativeComponent>
#include <QDeclarativeEngine>
#include <QDeclarativeProperty>
#include <QUrl>
#include <QDeclarativeItem>

Q_DECL_EXPORT int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDeclarativeView* view = new QDeclarativeView;
view->setSource(QUrl::fromLocalFile("/home/alen/QtSDK/bezveze/qml/bezveze/main.qml"));
view->show();
QObject *object = view->rootObject();
QDeclarativeProperty(object, "width").write(500);
qDebug() << object;
return app.exec();
}

Thank's for all your help. Problem solved.

d_stranz
5th January 2013, 00:12
According to the QtBinding link posted earlier, these two should behave in exactly the same way:



object->setProperty("width", 500);

// vs.

QDeclarativeProperty(object, "width").write(500);


Are you saying that the only version that works for you is the second one?

wysota
5th January 2013, 00:16
Both work. The essential part is that he corrected the way he was extracting the object from QtQuick runtime.

anda_skoa
7th January 2013, 14:25
import QtQuick 1.1

Item{
width: 100;
height: 100;
Text{ text: "Bezveze";}
objectName: bb;
}

That looks wrong, objectName is a string property (unless bb is a string property defined elsewhere)





QObject *object = view->findChild<QObject*>("bb");
object->setProperty("width", 500);
}


I would recommend not going down that route at all.
Accessing objects created by the QML engine from C++ is bad design, the better way is the other direction, i.e. exporting objects from C++ to the QML scene.

In other words make the UI depend on the application core and not having the application core depend on the UI.

Cheers,
_

alenn.masic
7th January 2013, 15:37
That looks wrong, objectName is a string property (unless bb is a string property defined elsewhere)

Yes, but I fixed it later when I realised when my error was.





I would recommend not going down that route at all.
Accessing objects created by the QML engine from C++ is bad design, the better way is the other direction, i.e. exporting objects from C++ to the QML scene.

In other words make the UI depend on the application core and not having the application core depend on the UI.

Cheers,
_

Can you provide me with some link where I can find more information about it.

anda_skoa
8th January 2013, 12:23
See QDeclarativeContext: http://qt-project.org/doc/qt-4.8/qdeclarativecontext.html

You can retrieve your root context directly from the QDeclarativeView object.

Basically you can make any QObject that you've created on C++ visible to the QML scene by any name you see fit and then use its properies like those of any QML element and even call slots and Qt invokable mehods.

Cheers,
_