QQuickView to QQmlApplicationEngine
-
Hello everyone :)
i am making samples for getting used to change Class with the same function like "paraphrasing"
But there is a problem
QQmlApplicationEngine is bullying me.here is codes
Original
qmlRegisterType<QVTKFrameBufferObjectItem>("VtkQuick", 1, 0, "VtkRenderWindow");
QQuickView view;
view.setSource(QUrl::fromLocalFile(PROJECT_SOURCE_DIR "//main.qml"));
QList<QVTKFrameBufferObjectItem*> vtkItems = view.rootObject()->findChildren<QVTKFrameBufferObjectItem*>();i want to change QQuickView to QQmlApplicationEngine but i couldn't
Changed
qmlRegisterType<QVTKFrameBufferObjectItem>("VtkQuick", 1, 0, "VtkRenderWindow");
QQmlApplicationEnging engine;
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
QList<QVTKFrameBufferObjectItem*> vtkItems = view.rootObjects()->findChildren<QVTKFrameBufferObjectItem*>();i thought the function of rootObject() and rootObjects() is the same.
but it didn't work.Could you please tell me what should be different from QQuickView?
If you make me know differences of QQmlApplicationEngine compared with QQuickView, it's going to be perfect advice for me.
Thanks !
-
Unlike QQuickView, QQmlApplicationEngine does not automatically create a root window. If you are using visual items from Qt Quick, you will need to place them inside of a Window.
That is the biggest difference. QQuickView creates a window, so you can have any QML component as root item. For QQmlApplicationEngine, your root component has to be Window or ApplicationWindow. I recommend reading the whole description of QQmlApplicationEngine, it contains some other important aspects.
Regarding the pointer. Here I'm not sure (and can't check now), but it looks like your call findChildren() on a QList, which does not have such method. Try this instead:
for (QObject *obj : qAsConst(engine.rootObjects())) { vtkItems = obj->findChildren<QVTKFrameBufferObjectItem*>(); if (!vtkItems.isEmpty()) break; }
Or give your VTK component an objectName and then
-
@sierdzio Thanks for your replying.
i tried your code, and it seemed work. (i deleted qAsConst, so it worked)
What it means, there was no error.
but it didn't work like original code, so i need to find some other way.Anyway, your code was helpful.
it makes me think other ways.
Thanks a lot! XD