Results 1 to 6 of 6

Thread: Qt 5.5: Rotate 3D Model

  1. #1
    Join Date
    Jul 2015
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Qt 5.5: Rotate 3D Model

    I had a custom QML object "Model.qml" to load and rotate 3D model.

    Model.qml
    Entity {
    id: root

    property Material material

    property alias myRoll : transform.rollAngle

    components: [ transform, mesh, root.material ]

    Transform {

    id: transform
    objectName: "MyModel"

    property real rollAngle : 0
    property real pitchAngle : 20

    Translate { id: translation }
    Scale { id: scaleTransform }

    Rotate {
    objectName: "rotateRoll"
    axis : Qt.vector3d(1, 0, 0)
    angle : transform.rollAngle
    }

    }


    Mesh {
    id: mesh
    source: "qrc:/3dmodel/Drone.obj"
    }
    }

    And in drone.cpp i update property "rollAngle" to rotate model whenever this property changed but it doesn't work anyway. Here is the code I use to update "rollAngle"

    drone.cpp
    QQmlEngine engine;
    QQmlComponent component(&engine, QUrl("qrc:/src/Model.qml"));
    QObject *object = component.create();
    QObject *rotateObject = object->findChild<QObject *>("rotateRoll");
    childObject->setProperty("rollAngle",this->roll);
    rotateObject->setProperty("angle", this->roll);
    qDebug() << "Property value:" << rotateObject->property("angle").toFloat();
    engine.destroyed();

    "rollAngle" changes but 3D model doesn't rotate. I use SequenceAnimation instead but it can't run too. Can anyone give me some advice?

    Thks.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt 5.5: Rotate 3D Model

    Better than calling into QML is to expose the values via properties and then bind them in QML.

    I.e. add whatever values you want to change from C++ side as Q_PROPERTY to the drone object, then set the object as a context property.
    The object becomes available in QML and its properties with it. The properties can then be used just like properties of other QML elements.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    notthing (29th July 2015)

  4. #3
    Join Date
    Jul 2015
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt 5.5: Rotate 3D Model

    First thks anda_skoa

    But I misunderstand "set the object as a context property" can you explain in more detail or can you give a simple example?

    Thks again.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt 5.5: Rotate 3D Model

    The QQmlEngine has a root context (of type QQmlContext) and it can be used to "export" values and objects from C++ to be used from QML.

    See QQmlContest::setContextProperty().

    When an object (an instance of a QObject derived class) is exported like this, its properties (defined via Q_PROPERTY) become as easily usable in QML as properties of QML elements.
    I.e. you can create bindings that include them and those bindings get reevaluated automatically if the property value changes.

    See http://doc.qt.io/qt-5/qtqml-cppinteg...ttributes.html for an example

    Cheers,
    _

  6. #5
    Join Date
    Jul 2015
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt 5.5: Rotate 3D Model

    Ok, I'll try this way. Thks.

  7. #6
    Join Date
    Jul 2015
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt 5.5: Rotate 3D Model

    Quote Originally Posted by anda_skoa View Post
    The QQmlEngine has a root context (of type QQmlContext) and it can be used to "export" values and objects from C++ to be used from QML.

    See QQmlContest::setContextProperty().

    When an object (an instance of a QObject derived class) is exported like this, its properties (defined via Q_PROPERTY) become as easily usable in QML as properties of QML elements.
    I.e. you can create bindings that include them and those bindings get reevaluated automatically if the property value changes.

    See http://doc.qt.io/qt-5/qtqml-cppinteg...ttributes.html for an example

    Cheers,
    _
    Thks a lot anda_skoa

    I solved this issue Just create Q_PROPERTY attribute roll in Drone.h that links to attribute roll of object Model.qml and set rootContext to Drone. Then Q_EMIT the function notify with roll (in Drone.h) and the Rotate object in Model.qml we change to drone.roll. And finally it works.

Similar Threads

  1. Rotate about point
    By Andree in forum Newbie
    Replies: 2
    Last Post: 18th May 2014, 12:44
  2. Replies: 1
    Last Post: 29th August 2013, 05:41
  3. Replies: 9
    Last Post: 14th February 2013, 19:39
  4. How do I rotate a QPolygon
    By rawfool in forum Qt Programming
    Replies: 5
    Last Post: 29th December 2011, 08:31
  5. Rotate QLabel
    By shader76 in forum Newbie
    Replies: 9
    Last Post: 24th December 2007, 12:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.