Pass parameters from QML to C++
-
Hello, I currently have a tumbler built in qml which I can extract the current user set time. I have these 3 parameters (hour, minute, am/pm) printing to the console using this code.
Button {text: "Set" //onClicked: model.submit() onClicked: print(hoursTumbler.hourcode, minutesTumbler.mincode, amPmTumbler.ampmcode) }
My question is how can I pass these 3 parameters back to my C++ code which houses my main application.
The C++ code currently calls the QML file like this.QQuickView* quickView = new QQuickView(QUrl::fromLocalFile(QDir::currentPath() + "\\Resources\\videos\\aos.qml")); // Make the QML view resize when the parent is resized quickView->setResizeMode(QQuickView::SizeRootObjectToView); quickWidgettum = new QWidget; // Add the QML view to my widget layout quickWidgettum = QWidget::createWindowContainer(quickView); quickWidgettum->setWindowFlags(Qt::FramelessWindowHint | Qt::Window); laos->addWidget(quickWidgettum); quickWidgettum->hide();
-
Hello, I currently have a tumbler built in qml which I can extract the current user set time. I have these 3 parameters (hour, minute, am/pm) printing to the console using this code.
Button {text: "Set" //onClicked: model.submit() onClicked: print(hoursTumbler.hourcode, minutesTumbler.mincode, amPmTumbler.ampmcode) }
My question is how can I pass these 3 parameters back to my C++ code which houses my main application.
The C++ code currently calls the QML file like this.QQuickView* quickView = new QQuickView(QUrl::fromLocalFile(QDir::currentPath() + "\\Resources\\videos\\aos.qml")); // Make the QML view resize when the parent is resized quickView->setResizeMode(QQuickView::SizeRootObjectToView); quickWidgettum = new QWidget; // Add the QML view to my widget layout quickWidgettum = QWidget::createWindowContainer(quickView); quickWidgettum->setWindowFlags(Qt::FramelessWindowHint | Qt::Window); laos->addWidget(quickWidgettum); quickWidgettum->hide();
@celica I haven't used
QQuickView
but from the documentation I would imagine that you do something like this:MyApi* api = new MyApi(...); quickView->engine()->setContextProperty("myApi", api);
where
#include <QObject> class MyApi : public QObject { Q_OBJECT public: ... Q_INVOKABLE virtual void setValue(int i); ... };
Then in your QML, the identifier
myApi
is available to you and you should be able to call e.g.myApi.setValue(10)
from javascript code. -
Thanks for the input.
I do not have an engine in my code. So I simply use
quickViewdate->rootContext()->setContextProperty("myApi", api);I get the error ReferenceError: myApi is not defined when I try and call the function to connect.
@celica I think you need to do this is several steps:
QQuickView* quickView = new QQuickView() quickView->rootContext()->setContextProperty("myApi", api); quickView->setSource(QUrl::fromLocalFile(QDir::currentPath() + "\\Resources\\videos\\aos.qml"))); // More initialization, if required .. quickView->show();
-
@celica I think you need to do this is several steps:
QQuickView* quickView = new QQuickView() quickView->rootContext()->setContextProperty("myApi", api); quickView->setSource(QUrl::fromLocalFile(QDir::currentPath() + "\\Resources\\videos\\aos.qml"))); // More initialization, if required .. quickView->show();
@KroMignon Thanks all for your input
I managed to get it working. This may help someone:
What I did was within my qml file created these objectname tags for each tumbler:Tumbler { id: minutesTumbler objectName: "min" model: 60 delegate: delegateComponent readonly property int mincode: minutesTumbler.currentIndex }
Then from within c++ ->
QQuickItem* object = quickView->rootObject(); QObject *v_hour = object->findChild<QObject*>("hour"); double myhour = v_hour->property("hourcode").toDouble(); qDebug() << "Tumbler Data: " << myhour;
-
@KroMignon Thanks all for your input
I managed to get it working. This may help someone:
What I did was within my qml file created these objectname tags for each tumbler:Tumbler { id: minutesTumbler objectName: "min" model: 60 delegate: delegateComponent readonly property int mincode: minutesTumbler.currentIndex }
Then from within c++ ->
QQuickItem* object = quickView->rootObject(); QObject *v_hour = object->findChild<QObject*>("hour"); double myhour = v_hour->property("hourcode").toDouble(); qDebug() << "Tumbler Data: " << myhour;
@celica said in Pass parameters from QML to C++:
I managed to get it working
Great. So please don't forget to mark your post as solved!
-
@KroMignon Thanks all for your input
I managed to get it working. This may help someone:
What I did was within my qml file created these objectname tags for each tumbler:Tumbler { id: minutesTumbler objectName: "min" model: 60 delegate: delegateComponent readonly property int mincode: minutesTumbler.currentIndex }
Then from within c++ ->
QQuickItem* object = quickView->rootObject(); QObject *v_hour = object->findChild<QObject*>("hour"); double myhour = v_hour->property("hourcode").toDouble(); qDebug() << "Tumbler Data: " << myhour;
@celica said in Pass parameters from QML to C++:
double myhour = v_hour->property("hourcode").toDouble();
you can ensure conversion happened correctly if you pass a bool to toDouble
https://doc.qt.io/qt-5/qstring.html#toDouble