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. Why manually map C++ object properties and QML elements?

Why manually map C++ object properties and QML elements?

Scheduled Pinned Locked Moved QML and Qt Quick
1 Posts 1 Posters 1.7k 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.
  • B Offline
    B Offline
    bkamps
    wrote on last edited by
    #1

    Hi,

    I was wondering, let's say you define your own QML "element" :

    Dialog.qml:

    @
    Rectangle {
    property string text: ""
    color: "black"

    Text {
        id: dialogText
        anchors.centerIn: parent
        text: parent.text
        color: parent.color
    }
    

    }
    @

    Now I want to map my C++ Object (text and color properties) to this own defined Dialog qml element. How to do this?

    C++ Object:

    @
    class Dialog : public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QString text READ Text NOTIFY TextChanged)
    Q_PROPERTY(QString color READ Color NOTIFY ColorChanged)

    public:
    Dialog(QObject *parent = 0);
    ~Dialog();

    const QString & Text();
    const QString & Color();   
    

    signals:
    void TextChanged();
    void ColorChanged();

    private:
    QString mText;
    QString mColor;
    };
    @

    If I do a setContextProperty() I still have to manually set all the properties of the Dialog.qml to the C++ object:

    C++
    @
    view.rootContext()->setContextProperty("myDialog", myDialogObject);
    @

    And Qml:

    @
    Dialog { text: myDialog.text; color: myDialog.color }
    @

    Is there a way that QML automatically maps the properties of the C++ object to the QML element?

    I have seen that if I make C++ Dialog object a plugin (QDeclarativeItem) I still need to manually map the properties!

    Enlighten me please :)

    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