Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt3D - Retrieving Qml Viewport Element as C++ Object



  • Hey guys,

    I've been messing around with Qt5 and Qt3D / Quick3D a bit but I getting stuck trying to retrieve my qml Viewport Element as a c++ Viewport object.
    I can't seem to find the Viewport class include which is not indicated in the documentation :
    http://qt-project.org/doc/qt-5.0/qml-viewport.html
    http://doc-snapshot.qt-project.org/5.0/viewport.html
    I don't know if it's because it's deprecated or on the contrary not finalized yet.

    Then I'd like to make sure I'm retrieving my object the right way so I've posted a sample of my code :

    @
    bool QmlViewManager::initView()
    {
    // SET STARTING QML FILE
    this->viewer->setSource(QUrl::fromLocalFile("qml/Tepee3DSampleApp/main.qml"));
    this->viewer->show();
    //CONTROLLER BETWEEN QML VIEW AND MODELS
    this->viewer->rootContext()->setContextProperty("controller", this);
    // GET ROOT QML OBJECT
    this->rootQmlObject = this->viewer->rootObject();
    // SET SCREESIZE IN ROOT OBJECT
    this->rootQmlObject->setProperty("width", this->desktopWidget->screenGeometry(-1).width());
    this->rootQmlObject->setProperty("height", this->desktopWidget->screenGeometry(-1).height());
    // SHOW QML FILE IN FULLSCREEN
    QObject *tmpObj;
    if ((tmpObj = this->rootQmlObject->findChild<QObject >("viewport")))
    {
    this->viewport = (Viewport
    )tmpObj;
    std::cout << "ViewPort found" << std::endl;
    }
    if ((tmpObj = this->rootQmlObject->findChild<QObject >("root3dObject")))
    {
    this->root3dObject = (QQuickItem
    )tmpObj;
    std::cout << "Root3dObject found" << std::endl;
    }
    }
    @

    Any help will be very appreciated.

    Thanks



  • For those interested, you can't really retrieve a Viewport directly from qml as it is a QQuickItem. So either you retrieve it as a QQuickItem* and then try to insert your 3D items in it (not tested but it should work) or you do this on the qml side using loaders.


Log in to reply