How to bind a property to a singleton object property from QML

cavitsinadogru

There is a question about how to bind from a singleton object property to a QML property. But what about if we like to bind a QML property to a singleton object.

Here is the singleton class definition,

class Singleton : public QObject {
    Q_OBJECT
    Q_PROPERTY(QString name READ name WRITE setName)
public:
    explicit Singleton(QObject *parent = nullptr);
    QString name() const;
    void setName(const QString &name);
private:
    QString m_name;
};

And on QML

property string qmlName: textField.text
TextField {
    id: textField
}

I would like to bind textField.text to Singleton object name property. It is possible to bind it with a workaround like,

onQmlNameChanged: {
    Singleton.name = qmlName;
}

But that won't be an Property Binding actually, because it is an assignment.

So is there a more nature binding way to a singleton object property?

Felix

You could try to assign the binding like this:

Component.onCompleted: Singleton.name = Qt.binding(function() { return qmlName })

It works for normal QML-Objects, not sure it works with a singleton class, though. Anyhow, you can read more about this approach in the section "Creating Property Bindings from JavaScript".

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Read property from QML singleton with C++

From Dev

How to bind control to object property?

From Dev

How to bind control to object property?

From Dev

Bind a QAbstractListModel derived listmodel member of an object in QML as Q_PROPERTY

From Dev

How to change a property of a object in QML upon a hover?

From Dev

Bind ImageAwesome Object in WPF from a ViewModel Property

From Dev

How to bind property on parent object with knockoutjs?

From Dev

How to bind a list which is a property of an object to DataGridView?

From Dev

How to bind template to object in array by property value?

From Dev

How to parse a json object property to bind it to a control

From Dev

How to bind List object with no duplicate property

From Dev

Bind to property of object

From Dev

Change property of the QML singleton with C++

From Dev

Change property of the QML singleton with C++

From Dev

QML Access object property by property name string

From Dev

QML object property memory management

From Dev

QML object property memory management

From Dev

Setting object type property in QML from C++

From Dev

QML object var property is null QVariant(QJSValue, ) from cpp

From Dev

Unable to bind ComboBox SelectedValue property with object property

From Dev

How to bind textblock with property

From Dev

How to Bind Attached Property

From Dev

How to bind dynamic property?

From Dev

How to Bind the Property of an Instance?

From Dev

How to bind a ContentView to a property?

From Dev

Bind custom object property to BooleanBinding

From Dev

Bind knockoutjs to javascript object property

From Dev

How to get updated property in singleton from a component in Blazor?

From Dev

How to bind to root context object signal from QML

Related Related

  1. 1

    Read property from QML singleton with C++

  2. 2

    How to bind control to object property?

  3. 3

    How to bind control to object property?

  4. 4

    Bind a QAbstractListModel derived listmodel member of an object in QML as Q_PROPERTY

  5. 5

    How to change a property of a object in QML upon a hover?

  6. 6

    Bind ImageAwesome Object in WPF from a ViewModel Property

  7. 7

    How to bind property on parent object with knockoutjs?

  8. 8

    How to bind a list which is a property of an object to DataGridView?

  9. 9

    How to bind template to object in array by property value?

  10. 10

    How to parse a json object property to bind it to a control

  11. 11

    How to bind List object with no duplicate property

  12. 12

    Bind to property of object

  13. 13

    Change property of the QML singleton with C++

  14. 14

    Change property of the QML singleton with C++

  15. 15

    QML Access object property by property name string

  16. 16

    QML object property memory management

  17. 17

    QML object property memory management

  18. 18

    Setting object type property in QML from C++

  19. 19

    QML object var property is null QVariant(QJSValue, ) from cpp

  20. 20

    Unable to bind ComboBox SelectedValue property with object property

  21. 21

    How to bind textblock with property

  22. 22

    How to Bind Attached Property

  23. 23

    How to bind dynamic property?

  24. 24

    How to Bind the Property of an Instance?

  25. 25

    How to bind a ContentView to a property?

  26. 26

    Bind custom object property to BooleanBinding

  27. 27

    Bind knockoutjs to javascript object property

  28. 28

    How to get updated property in singleton from a component in Blazor?

  29. 29

    How to bind to root context object signal from QML

HotTag

Archive