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. Strings in QML/QtQuick/Qt
Forum Updated to NodeBB v4.3 + New Features

Strings in QML/QtQuick/Qt

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 5 Posters 6.3k 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.
  • D Offline
    D Offline
    Dynite
    wrote on 25 Nov 2010, 10:48 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alexander
      wrote on 25 Nov 2010, 10:54 last edited by
      #2

      "http://doc.qt.nokia.com/4.7/qml-extending-types.html":http://doc.qt.nokia.com/4.7/qml-extending-types.html

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alexander
        wrote on 25 Nov 2010, 10:59 last edited by
        #3

        If so?

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

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DenisKormalev
          wrote on 25 Nov 2010, 12:35 last edited by
          #4

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

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Dynite
            wrote on 25 Nov 2010, 12:41 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mutaphysis
              wrote on 25 Nov 2010, 14:10 last edited by
              #6

              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.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mario
                wrote on 25 Nov 2010, 19:01 last edited by
                #7

                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.

                1 Reply Last reply
                0

                1/7

                25 Nov 2010, 10:48

                • Login

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