Hi everybody.
I'm trying to load a QML component depending on the value of a certain variable declared as a property in a C++ class but when I start debugging the application QML does not recognize the C++ object containing that property.
Does anyone can help? I'll put the code down here.
This is the main.cpp file:
#include <QtGui/QGuiApplication>
#include <QtQml>
#include "app.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
cApp viewer;
viewer.setMainQmlFile(QStringLiteral("qml/ClemDemo/main.qml"));
viewer.showExpanded();
return app.exec();
}
#include <QtGui/QGuiApplication>
#include <QtQml>
#include "app.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
cApp viewer;
viewer.setMainQmlFile(QStringLiteral("qml/ClemDemo/main.qml"));
viewer.showExpanded();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
This is the cApp class derived from the class generated by the QtQuick wizard in order to use a particular nativeEvent function:
#include <QtQml>
#include "qtquick2applicationviewer.h"
#include "myclass.h"
class cApp : public QtQuick2ApplicationViewer
{
Q_OBJECT
bool nativeEvent
(const QByteArray &eventType,
void *message,
long *result
);
public:
cMyClass* myClass ;
explicit cApp(QWindow *parent = 0);
virtual ~cApp();
public slots:
void device_in();
void device_out();
};
cApp::cApp(QWindow *parent) : QtQuick2ApplicationViewer(parent)
{
myClass = new cMyClass;
rootContext()->setContextProperty("myqmlclass", myClass );
connect(myClass , SIGNAL(device_connected()), this, SLOT(device_in()));
connect(myClass , SIGNAL(device_removed()), this, SLOT(device_out()));
}
bool cApp
::nativeEvent(const QByteArray &eventType,
void *message,
long *result
) {
return myClass ->nativeEvent(eventType, message, result);
}
void cApp::device_in()
{
myClass ->setDeviceConnected(true);
}
void cApp::device_out()
{
myClass ->setDeviceConnected(false);
}
#include <QtQml>
#include "qtquick2applicationviewer.h"
#include "myclass.h"
class cApp : public QtQuick2ApplicationViewer
{
Q_OBJECT
bool nativeEvent(const QByteArray &eventType, void *message, long *result);
public:
cMyClass* myClass ;
explicit cApp(QWindow *parent = 0);
virtual ~cApp();
public slots:
void device_in();
void device_out();
};
cApp::cApp(QWindow *parent) : QtQuick2ApplicationViewer(parent)
{
myClass = new cMyClass;
rootContext()->setContextProperty("myqmlclass", myClass );
connect(myClass , SIGNAL(device_connected()), this, SLOT(device_in()));
connect(myClass , SIGNAL(device_removed()), this, SLOT(device_out()));
}
bool cApp::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
return myClass ->nativeEvent(eventType, message, result);
}
void cApp::device_in()
{
myClass ->setDeviceConnected(true);
}
void cApp::device_out()
{
myClass ->setDeviceConnected(false);
}
To copy to clipboard, switch view to plain text mode
This is the class i need to use into QML:
{
Q_OBJECT
friend class cApp;
protected:
bool nativeEvent
(const QByteArray &eventType,
void *message,
long *result
);
public:
cMyClass();
virtual ~cMyClass(){}
public:
bool DeviceConnected;
Q_PROPERTY(bool connected READ isConnected WRITE setDeviceConnected NOTIFY somethingHappened)
void setDeviceConnected(bool status){ DeviceConnected = status;}
bool isConnected(){ return DeviceConnected; }
signals:
void somethingHappened();
void device_connected();
void device_removed();
};
cMyClass::cMyClass()
{
//Connections for attach/detach notification of a particular device
// .............
}
bool cMyClass
::nativeEvent(const QByteArray &eventType,
void *message,
long *result
) {
//Handle messages of device attached/detached
}
class cMyClass: public QObject
{
Q_OBJECT
friend class cApp;
protected:
bool nativeEvent(const QByteArray &eventType, void *message, long *result);
public:
cMyClass();
virtual ~cMyClass(){}
public:
bool DeviceConnected;
Q_PROPERTY(bool connected READ isConnected WRITE setDeviceConnected NOTIFY somethingHappened)
void setDeviceConnected(bool status){ DeviceConnected = status;}
bool isConnected(){ return DeviceConnected; }
signals:
void somethingHappened();
void device_connected();
void device_removed();
};
cMyClass::cMyClass()
{
//Connections for attach/detach notification of a particular device
// .............
}
bool cMyClass::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
//Handle messages of device attached/detached
}
To copy to clipboard, switch view to plain text mode
And here is the QML code:
import QtQuick 2.0
BorderImage {
id: main_window
source: "qrc:/main/sfondo.jpg"
width: 1680; height: 932
horizontalTileMode: BorderImage.Stretch
verticalTileMode: BorderImage.Stretch
Component
{
id: devices
Rectangle {
id: device_side
width: 400; height: 500
color: "#C0A3A3A3" // #AARRGGBB where AA is Alpha channel (for transparency)
radius: 8
}
}
Loader { id: device_loader }
myqmlclass.onSomethingHappened: { device_loader.sourceComponent = devices }
}
import QtQuick 2.0
BorderImage {
id: main_window
source: "qrc:/main/sfondo.jpg"
width: 1680; height: 932
horizontalTileMode: BorderImage.Stretch
verticalTileMode: BorderImage.Stretch
Component
{
id: devices
Rectangle {
id: device_side
width: 400; height: 500
color: "#C0A3A3A3" // #AARRGGBB where AA is Alpha channel (for transparency)
radius: 8
}
}
Loader { id: device_loader }
myqmlclass.onSomethingHappened: { device_loader.sourceComponent = devices }
}
To copy to clipboard, switch view to plain text mode
The error is that myqmlclass is not recognized as a QML property. This is what I can see on the application output:
"Cannot assign to non-existent property "myqmlclass"
myqmlclass.onSomethingHappened: { device_loader.sourceComponent = devices }"
Bookmarks