Qml and qlist c++
-
-
Ok so this is what I've come up with. I'm sure there is a much better way of doing this without the overhead.
StateDeviceListProvider.h
@
#ifndef STATEDEVICELISTPROVIDER_H
#define STATEDEVICELISTPROVIDER_H#include <QObject>
#include <QQmlListProperty>// Forward declarations.
class StateDevice;class StateDeviceListProvider : public QObject
{
Q_OBJECT
Q_PROPERTY(QQmlListProperty<StateDevice> stateDeviceList READ stateDeviceList)public:
static StateDeviceListProvider* instance();
QQmlListProperty<StateDevice> stateDeviceList();private:
StateDeviceListProvider(QObject *parent = NULL);private:
static StateDeviceListProvider mInstance;
QList<StateDevice> mStateDeviceList;
};#endif // STATEDEVICELISTPROVIDER_H
@StateDeviceListProvider.cpp
@
#include "statedevicelistprovider.h"
#include "statedevice.h"StateDeviceListProvider* StateDeviceListProvider::mInstance = NULL;
StateDeviceListProvider::StateDeviceListProvider(QObject *parent) :
QObject(parent)
{// This is just to test this code. You will use your real list. mStateDeviceList << new StateDevice; mStateDeviceList << new StateDevice; mStateDeviceList << new StateDevice;
}
StateDeviceListProvider* StateDeviceListProvider::instance()
{
if(mInstance == NULL)
{
mInstance = new StateDeviceListProvider;
}
return mInstance;
}QQmlListProperty<StateDevice> StateDeviceListProvider::stateDeviceList()
{// Here you would retrieve your real list and return it. return QQmlListProperty<StateDevice>(this, mStateDeviceList);
}
@main.cpp
@
#include <qqml.h>
#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include "statedevicelistprovider.h"
#include "statedevice.h"static QObject* StaticDeviceListProviderCallback(QQmlEngine *engine, QJSEngine *scriptEngine)
{
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)return StateDeviceListProvider::instance();
}
int main(int argc, char *argv[])
{
qmlRegisterType<StateDevice>();
qmlRegisterSingletonType<StateDeviceListProvider>(
"MyModule", 1, 0, "StateDeviceListProvider", StaticDeviceListProviderCallback);QGuiApplication app(argc, argv); QtQuick2ApplicationViewer viewer; viewer.setMainQmlFile(QStringLiteral("qml/untitled/main.qml")); viewer.showExpanded(); return app.exec();
}
@main.qml
@
import QtQuick 2.0
import MyModule 1.0Rectangle {
width: 360
height: 360// Now you have your list exposed to qml. property variant list: StateDeviceListProvider.stateDeviceList Component.onCompleted: { // Just testing here. var el0 = list[0]; console.log(el0.number()) }
}
@ -
It should be near enough to the same thing with some minor modifications (class name changes). Pre Qt5 does not support qmlRegisterSingletonType so there needs to be some modification there as well. I will give it a go and post my results when I have more time. Did my first example work for you then?
-
Ok here's an example that works with QQuick 1.0. Again, If anyone has a better way of doing this then please share.
StateDeviceListProvider.h
@
#ifndef STATEDEVICELISTPROVIDER_H
#define STATEDEVICELISTPROVIDER_H#include <QObject>
#include <QDeclarativeListProperty>class StateDevice;
class StateDeviceListProvider : public QObject
{
Q_OBJECT
Q_PROPERTY(QDeclarativeListProperty<StateDevice> stateDeviceList READ stateDeviceList CONSTANT)public:
static StateDeviceListProvider* instance();
QDeclarativeListProperty<StateDevice> stateDeviceList();private:
StateDeviceListProvider(QObject *parent = NULL);private:
static StateDeviceListProvider mInstance;
QList<StateDevice> mStateDeviceList;
};#endif // STATEDEVICELISTPROVIDER_H
@StateDeviceListProvider.cpp
@
#include "statedevicelistprovider.h"
#include "statedevice.h"StateDeviceListProvider* StateDeviceListProvider::mInstance = NULL;
StateDeviceListProvider::StateDeviceListProvider(QObject *parent) :
QObject(parent)
{// This is just to test this code. You will use your real list. mStateDeviceList << new StateDevice; mStateDeviceList << new StateDevice; mStateDeviceList << new StateDevice;
}
StateDeviceListProvider* StateDeviceListProvider::instance()
{
if(mInstance == NULL)
{
mInstance = new StateDeviceListProvider;
}
return mInstance;
}QDeclarativeListProperty<StateDevice> StateDeviceListProvider::stateDeviceList()
{// Here you would retrieve your real list and return it. return QDeclarativeListProperty<StateDevice>(this, mStateDeviceList);
}
@main.cpp
@
#include <QApplication>
#include <qdeclarative.h>
#include <QDeclarativeContext>
#include "qmlapplicationviewer.h"
#include "statedevicelistprovider.h"
#include "statedevice.h"Q_DECL_EXPORT int main(int argc, char *argv[])
{
qmlRegisterType<StateDevice>();QScopedPointer<QApplication> app(createApplication(argc, argv)); QmlApplicationViewer viewer; // Pre Qt5 does not support qmlRegisterSingletonType. So this is a work-around. viewer.rootContext()->setContextProperty("StateDeviceListProvider", StateDeviceListProvider::instance()); viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto); viewer.setMainQmlFile(QLatin1String("qml/untitledQQuick1/main.qml")); viewer.showExpanded(); return app->exec();
}
@main.qml
@
import QtQuick 1.1Rectangle {
width: 360
height: 360// Now you have your list exposed to qml. property variant list: StateDeviceListProvider.stateDeviceList Component.onCompleted: { // Just testing here. var el0 = list[0]; console.log(el0.number) }
}
@ -
visual basic and c++ are the best pc programs that ..........