Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] C++ object as property parameter
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] C++ object as property parameter

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 1.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kortus
    wrote on last edited by
    #1

    I come with a question to the integration between C++ and QML. I have read the good
    "cpp-integration":http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-definetypes.html
    documentation. But I see no solution for my use case.

    The target is to write a module that display properties (in real a lot of properties) of a C++-Object.
    At the moment I use the eval() function and it works:-), but I ask if there is a smarter solution. Here my code.

    @// MyPropVis.qml
    import QtQuick 2.1

    Column {
    id: propCol
    property string objName

    Text { text: eval(propCol.objName+".number") }
    Text { text: 2 * eval(propCol.objName+".number") }
    Text { text: eval(propCol.objName+".number") * eval(propCol.objName+".number") }
    }@

    @//PropMain.qml
    import QtQuick 2.1
    import QtQuick.Controls 1.1

    Rectangle {
    width: 320
    height: 200

    Row {
    spacing: 40

    MyPropVis { objName: "prop1" }
    MyPropVis { objName: "prop2" }

    Button {
    text: qsTr("Set 87")
    onClicked: { prop1.setnumber(87) }
    }
    }
    }@

    @//MyProps.h
    #ifndef MYPROPS_H
    #define MYPROPS_H

    #include <QObject>

    class MyProps : public QObject
    {
    Q_OBJECT

    Q_PROPERTY(quint32 number READ getnumber WRITE setnumber NOTIFY numberChanged)
    public:
    explicit MyProps(quint32 init, QObject *parent = 0);

    quint32 getnumber() const { return number; }

    signals:
    void numberChanged();

    public slots:
    void setnumber(quint32 localParameter);

    private:
    quint32 number;
    };

    #endif // MYPROPS_H@

    @//main.cpp
    #include "MyProps.h"

    #include <QGuiApplication>
    #include <QQmlContext>
    #include <QQmlEngine>
    #include <QQuickView>

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);
    QQuickView viewer;

    MyProps prop1(34);
    MyProps prop2(42);

    viewer.rootContext()->setContextProperty("prop1", &prop1);
    viewer.rootContext()->setContextProperty("prop2", &prop2);
    viewer.setSource(QUrl("qrc:/qml/helloQml/PropMain.qml"));
    viewer.show();
    return app.exec();
    }@

    Thanks for any hint.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      All properties of an object (that derives from QObject) can be read in QML using dot notation.

      You do not need to create your special "objName" property: all QML and QObjects have "objectName" property. By default it is empty, but you can choose to set it.

      You do not need to pass your objects by string name: QML understands QObject pointers. So, instead of using
      @
      property string objName
      @

      you can use:
      @
      property var objectPointer
      @

      I think this might help you a bit ;) I've kept the description broad and not detailed because I do not fully understand your use case. Feel free to ask for more info if anything is unclear.

      (Z(:^

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kortus
        wrote on last edited by
        #3

        Yeah, so simple, thanks.

        Sometimes it is hard to do the step from a strongly typed language to Javascript/QML:-)

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved