QuickView rootObjects()
-
I am trying to run a code like this but the object pointer is always NULL.
I have different qml files but the object pointers are always NULL.
Do you have any idea?
Thank you// Using QQuickView
QQuickView view;
view.setSource(QUrl::fromLocalFile("MyItem.qml"));
view.show();
QObject *object = view.rootObject(); -
I am trying to run a code like this but the object pointer is always NULL.
I have different qml files but the object pointers are always NULL.
Do you have any idea?
Thank you// Using QQuickView
QQuickView view;
view.setSource(QUrl::fromLocalFile("MyItem.qml"));
view.show();
QObject *object = view.rootObject();@Cyclotech said in QuickView rootObjects():
view.rootObject
You're calling it right after calling show() which is asynchronous. My guess is that at that time view is not yet ready. You should try to callrootObject later, for example if https://doc.qt.io/qt-6/qquickview.html#statusChanged changes to QQuickView::Ready.
-
I am trying to run a code like this but the object pointer is always NULL.
I have different qml files but the object pointers are always NULL.
Do you have any idea?
Thank you// Using QQuickView
QQuickView view;
view.setSource(QUrl::fromLocalFile("MyItem.qml"));
view.show();
QObject *object = view.rootObject();@Cyclotech said in QuickView rootObjects():
// Using QQuickView
QQuickView view;
view.setSource(QUrl::fromLocalFile("MyItem.qml"));
view.show();
QObject *object = view.rootObject();jsulm is right. But it is better that you code it using pointer.
// Using QQuickView auto view = new QQuickView; view->setSource(QUrl::fromLocalFile("MyItem.qml")); view->setResizeMode( QQuickWidget::SizeRootObjectToView ); QObject *object = view->rootObject(); view->setVisible( true ); -
@Cyclotech said in QuickView rootObjects():
// Using QQuickView
QQuickView view;
view.setSource(QUrl::fromLocalFile("MyItem.qml"));
view.show();
QObject *object = view.rootObject();jsulm is right. But it is better that you code it using pointer.
// Using QQuickView auto view = new QQuickView; view->setSource(QUrl::fromLocalFile("MyItem.qml")); view->setResizeMode( QQuickWidget::SizeRootObjectToView ); QObject *object = view->rootObject(); view->setVisible( true );@JoeCFD Not really. Why do you say it's better?
@Cyclotech Why do you want to access your rootObject, it is generally considered a bad practice and should be avoided.
Expose properties and slots in C++ instead of accessing your QML objects from C++. -
@JoeCFD Not really. Why do you say it's better?
@Cyclotech Why do you want to access your rootObject, it is generally considered a bad practice and should be avoided.
Expose properties and slots in C++ instead of accessing your QML objects from C++.@GrecKo said in QuickView rootObjects():
Not really. Why do you say it's better?
Because current code is using stack allocated object (view) which will be destroyed as soon as it gets out of scope. I did not comment on that to see whether OP will discover that by himself :-)
-
@GrecKo said in QuickView rootObjects():
Not really. Why do you say it's better?
Because current code is using stack allocated object (view) which will be destroyed as soon as it gets out of scope. I did not comment on that to see whether OP will discover that by himself :-)
If it's done in the main it's not a problem, I should have said so.
Your code is potentially leaking. -
If it's done in the main it's not a problem, I should have said so.
Your code is potentially leaking. -
@JoeCFD Not really. Why do you say it's better?
@Cyclotech Why do you want to access your rootObject, it is generally considered a bad practice and should be avoided.
Expose properties and slots in C++ instead of accessing your QML objects from C++.@GrecKo For example in my app, I need it to find a child item inside a qml and set it as sink in rtsp streaming. Is there any other way around?
Ok, I guess I can use qwidgets to wrap to this item. Good to know it, thanks. I realized QQuickView is very sensitive when it is put into a layout of qwidgets. The experience is that make qml as simple as possible for QQuickView or QQuickWidget.
-
@JoeCFD Not really. Why do you say it's better?
@Cyclotech Why do you want to access your rootObject, it is generally considered a bad practice and should be avoided.
Expose properties and slots in C++ instead of accessing your QML objects from C++.