render 3d model with qml in qquickwidget











up vote
0
down vote

favorite












I use Qt 5.5.1 in ubuntu 18.04, I want to render a 3d model(.obj format) with qml in a qquickwidget), i use setSource founction to bind qml file to the qquickwidget, my qml file coded as bollow:



import Qt3D 2.0
import Qt3D.Renderer 2.0
import QtQuick 2.0
Entity {
id: root
Camera {
id: camera
projectionType: CameraLens.PerspectiveProjection
fieldOfView: 45
aspectRatio: _window.width / _window.height
nearPlane: 0.1
farPlane: 1000.0
position: Qt.vector3d(0.0, 10.0, 20.0)
viewCenter: Qt.vector3d(0.0, 0.0, 0.0)
upVector: Qt.vector3d(0.0, 1.0, 0.0)
}

property Material material

Mesh {
id: trefoilMesh
source: "../res/obj/model.obj"
}

Transform {
id: j2Transform
...
}
}


but when i run, i got some error:
QQuickWidget only supports loading of root objects that derive from QQuickItem.
If your example is using QML 2, (such as qmlscene) and the .qml file you
loaded has 'import QtQuick 1.0' or 'import Qt 4.7', this error will occur.
To load files with 'import QtQuick 1.0' or 'import Qt 4.7', use the
QDeclarativeView class in the Qt Quick 1 module.



then I wraped my qml body whit Item property, some thing like that:



Item {
id: root
Entity {
id: sceneRoot

Camera {
id: camera
projectionType: CameraLens.PerspectiveProjection
fieldOfView: 45
aspectRatio: _window.width / _window.height
nearPlane: 0.1
farPlane: 1000.0
position: Qt.vector3d(0.0, 10.0, 20.0)
viewCenter: Qt.vector3d(0.0, 0.0, 0.0)
upVector: Qt.vector3d(0.0, 1.0, 0.0)
...
}


Then i get another warnning: ReferenceError: _window is not defined, and I got an empty output. And then I use the Qt3D::Quick::QQmlAspectEngine and QWindow to render the qml(without Item property wraped) as the qt example, i cant got the correct model on the window, so i think my qml file is ok. But the problem is I can't use the QWindow, i need to embed my 3d model render as a part of my program(already have a mainwindow), can anyone help me how to do this? Thank you very much!










share|improve this question




























    up vote
    0
    down vote

    favorite












    I use Qt 5.5.1 in ubuntu 18.04, I want to render a 3d model(.obj format) with qml in a qquickwidget), i use setSource founction to bind qml file to the qquickwidget, my qml file coded as bollow:



    import Qt3D 2.0
    import Qt3D.Renderer 2.0
    import QtQuick 2.0
    Entity {
    id: root
    Camera {
    id: camera
    projectionType: CameraLens.PerspectiveProjection
    fieldOfView: 45
    aspectRatio: _window.width / _window.height
    nearPlane: 0.1
    farPlane: 1000.0
    position: Qt.vector3d(0.0, 10.0, 20.0)
    viewCenter: Qt.vector3d(0.0, 0.0, 0.0)
    upVector: Qt.vector3d(0.0, 1.0, 0.0)
    }

    property Material material

    Mesh {
    id: trefoilMesh
    source: "../res/obj/model.obj"
    }

    Transform {
    id: j2Transform
    ...
    }
    }


    but when i run, i got some error:
    QQuickWidget only supports loading of root objects that derive from QQuickItem.
    If your example is using QML 2, (such as qmlscene) and the .qml file you
    loaded has 'import QtQuick 1.0' or 'import Qt 4.7', this error will occur.
    To load files with 'import QtQuick 1.0' or 'import Qt 4.7', use the
    QDeclarativeView class in the Qt Quick 1 module.



    then I wraped my qml body whit Item property, some thing like that:



    Item {
    id: root
    Entity {
    id: sceneRoot

    Camera {
    id: camera
    projectionType: CameraLens.PerspectiveProjection
    fieldOfView: 45
    aspectRatio: _window.width / _window.height
    nearPlane: 0.1
    farPlane: 1000.0
    position: Qt.vector3d(0.0, 10.0, 20.0)
    viewCenter: Qt.vector3d(0.0, 0.0, 0.0)
    upVector: Qt.vector3d(0.0, 1.0, 0.0)
    ...
    }


    Then i get another warnning: ReferenceError: _window is not defined, and I got an empty output. And then I use the Qt3D::Quick::QQmlAspectEngine and QWindow to render the qml(without Item property wraped) as the qt example, i cant got the correct model on the window, so i think my qml file is ok. But the problem is I can't use the QWindow, i need to embed my 3d model render as a part of my program(already have a mainwindow), can anyone help me how to do this? Thank you very much!










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I use Qt 5.5.1 in ubuntu 18.04, I want to render a 3d model(.obj format) with qml in a qquickwidget), i use setSource founction to bind qml file to the qquickwidget, my qml file coded as bollow:



      import Qt3D 2.0
      import Qt3D.Renderer 2.0
      import QtQuick 2.0
      Entity {
      id: root
      Camera {
      id: camera
      projectionType: CameraLens.PerspectiveProjection
      fieldOfView: 45
      aspectRatio: _window.width / _window.height
      nearPlane: 0.1
      farPlane: 1000.0
      position: Qt.vector3d(0.0, 10.0, 20.0)
      viewCenter: Qt.vector3d(0.0, 0.0, 0.0)
      upVector: Qt.vector3d(0.0, 1.0, 0.0)
      }

      property Material material

      Mesh {
      id: trefoilMesh
      source: "../res/obj/model.obj"
      }

      Transform {
      id: j2Transform
      ...
      }
      }


      but when i run, i got some error:
      QQuickWidget only supports loading of root objects that derive from QQuickItem.
      If your example is using QML 2, (such as qmlscene) and the .qml file you
      loaded has 'import QtQuick 1.0' or 'import Qt 4.7', this error will occur.
      To load files with 'import QtQuick 1.0' or 'import Qt 4.7', use the
      QDeclarativeView class in the Qt Quick 1 module.



      then I wraped my qml body whit Item property, some thing like that:



      Item {
      id: root
      Entity {
      id: sceneRoot

      Camera {
      id: camera
      projectionType: CameraLens.PerspectiveProjection
      fieldOfView: 45
      aspectRatio: _window.width / _window.height
      nearPlane: 0.1
      farPlane: 1000.0
      position: Qt.vector3d(0.0, 10.0, 20.0)
      viewCenter: Qt.vector3d(0.0, 0.0, 0.0)
      upVector: Qt.vector3d(0.0, 1.0, 0.0)
      ...
      }


      Then i get another warnning: ReferenceError: _window is not defined, and I got an empty output. And then I use the Qt3D::Quick::QQmlAspectEngine and QWindow to render the qml(without Item property wraped) as the qt example, i cant got the correct model on the window, so i think my qml file is ok. But the problem is I can't use the QWindow, i need to embed my 3d model render as a part of my program(already have a mainwindow), can anyone help me how to do this? Thank you very much!










      share|improve this question















      I use Qt 5.5.1 in ubuntu 18.04, I want to render a 3d model(.obj format) with qml in a qquickwidget), i use setSource founction to bind qml file to the qquickwidget, my qml file coded as bollow:



      import Qt3D 2.0
      import Qt3D.Renderer 2.0
      import QtQuick 2.0
      Entity {
      id: root
      Camera {
      id: camera
      projectionType: CameraLens.PerspectiveProjection
      fieldOfView: 45
      aspectRatio: _window.width / _window.height
      nearPlane: 0.1
      farPlane: 1000.0
      position: Qt.vector3d(0.0, 10.0, 20.0)
      viewCenter: Qt.vector3d(0.0, 0.0, 0.0)
      upVector: Qt.vector3d(0.0, 1.0, 0.0)
      }

      property Material material

      Mesh {
      id: trefoilMesh
      source: "../res/obj/model.obj"
      }

      Transform {
      id: j2Transform
      ...
      }
      }


      but when i run, i got some error:
      QQuickWidget only supports loading of root objects that derive from QQuickItem.
      If your example is using QML 2, (such as qmlscene) and the .qml file you
      loaded has 'import QtQuick 1.0' or 'import Qt 4.7', this error will occur.
      To load files with 'import QtQuick 1.0' or 'import Qt 4.7', use the
      QDeclarativeView class in the Qt Quick 1 module.



      then I wraped my qml body whit Item property, some thing like that:



      Item {
      id: root
      Entity {
      id: sceneRoot

      Camera {
      id: camera
      projectionType: CameraLens.PerspectiveProjection
      fieldOfView: 45
      aspectRatio: _window.width / _window.height
      nearPlane: 0.1
      farPlane: 1000.0
      position: Qt.vector3d(0.0, 10.0, 20.0)
      viewCenter: Qt.vector3d(0.0, 0.0, 0.0)
      upVector: Qt.vector3d(0.0, 1.0, 0.0)
      ...
      }


      Then i get another warnning: ReferenceError: _window is not defined, and I got an empty output. And then I use the Qt3D::Quick::QQmlAspectEngine and QWindow to render the qml(without Item property wraped) as the qt example, i cant got the correct model on the window, so i think my qml file is ok. But the problem is I can't use the QWindow, i need to embed my 3d model render as a part of my program(already have a mainwindow), can anyone help me how to do this? Thank you very much!







      c++ 3d qquickwidget






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 5 at 3:43

























      asked Nov 3 at 14:35









      Bruce

      44




      44





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53132333%2frender-3d-model-with-qml-in-qquickwidget%23new-answer', 'question_page');
          }
          );

          Post as a guest





































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53132333%2frender-3d-model-with-qml-in-qquickwidget%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()