Strings in QML/QtQuick/Qt



  • Hi,

    This questions is partly about data types, partly about QML & design.

    So, Firstly:

    What data type is the QML 'string' type?
    If I wanted this property to be bound to my c++ app, what type should it be? QString?

    Secondly:
    If I wanted a QML Text element to conditionally change I know I can do something like this:

    @Text {
    text: {
    if(value1) { "value1" }
    else { "value2" }
    }
    }@

    However this doesn't seem very... clean.
    Is there a better way to achieve such a thing (such as using a property binded to c++ variable)?
    Or some other method?





  • If so?

    @Text {
    text: {
    if(value1) { return "value1" }
    else { return "value2" }
    }
    }@



  • @
    Text {
    text: (value1)?"value1":"value2"
    }
    @
    is not ok for you?



  • That's the same as what I wrote, just different syntax.

    I'm not actually talking about syntax, more the fact that QML is supposed to describe the UI.

    For me it seems that I'm slightly abusing the fact that I can use conditional programming there.

    It seems to me that QML should be 'pure' if possible.

    I suppose using C++ (or whatever) to drive the content the QML describes is necessary.



  • States is a clean and common concept in QML, maybe you can map these better without feeling bad.

    But having something as
    @
    Text {
    text: (value1)?"value1":"value2"
    }
    @
    does not look bad to me, I have seen far more complicated things in templating engines or other "pure" UI description languages.
    In the end the style of your application is up to you, but I would start in the way of least resistance.



  • You could create a "model" class in c++ of your value that emits a signal when the value changes.

    Something like this for example:
    @
    class PersonName : public QObject
    {
    Q_OBJECT

    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)

    public:
    QString name() const { return m_name }

    public slots:
    void setName(const QString &name) {
    if (m_name != name) {
    m_name = name;
    emit nameChanged();
    }
    }

    signals:
    void nameChanged();

    private:
    QString m_name;
    }
    @

    Create an instance in c++ and add it to the decl. context. Now you can both set the value in qml and get notifications when the value changes.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.