Answer the question
In order to leave comments, you need to log in
How to make a bidirectional binding (communication) between Qt (C ++) and QML?
Greetings!
There is a class with a property, for example
class AppCore : public QObject
{
Q_OBJECT
Q_PROPERTY(int val1 READ val1 WRITE setVal1 NOTIFY val1Changed)
....
Window {
visible: true
property var appCore: null
....
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
AppCore * appCore = new AppCore(0);
engine.rootObjects().at(0)->setProperty("appCore", qVariantFromValue(appCore));
Item {
id: wSlider
property double value: 100
....
WSlider {
anchors.centerIn: parent
value: appCore.val1
}
Answer the question
In order to leave comments, you need to log in
You need to use not
engine.rootObjects().at(0)->setProperty("appCore", qVariantFromValue(appCore));
and forward this AppCore as a new component:
//это должно быть до строки
//QQmlApplicationEngine engine;
qmlRegisterType<AppCore>("com.myown.project", 1, 0, "AppCore");
AppCore
{
id: appCoreId
val1: 100500;
}
//создавать придётся именно так:
QQmlEngine engine;
QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/main.qml")));
QObject *object = component.create();
//получаем созданный на стороне QML истанс класса AppCore
AppCore* appCore = object->findChild<AppCore*>("appCoreId");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question