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. Dynamic text using QString::arg() in QML
Forum Updated to NodeBB v4.3 + New Features

Dynamic text using QString::arg() in QML

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

    The following web page mentions how to achieve dynamic text using QString::arg() :
    http://doc.qt.nokia.com/4.7/internationalization.html#use-qstring-arg-for-dynamic-text

    This looks wonderful for localized text where the arguments are not always at the same position:
    @
    label.setText(tr("%1 of %2 files copied.\nCopying: %3")
    .arg(done)
    .arg(total)
    .arg(currentFile));
    @
    How to achieve the same thing in QML ?
    Is there a QML interface for QString::arg ?
    Do I need to create one ?
    I wrote this little qml file that does the job in ECMAScript but I am not really happy with it:
    @
    import Qt 4.7

    Rectangle {
    width: 100
    height: 62

    function stringFormat(str, args) {
        for(var i=0; i<args.length; i++)
            str = str.replace(eval&#40;"/%"+(i+1&#41;+"/"), args[i]);
    
        return str;
    }
    
    Component.onCompleted: {
        var str = stringFormat(qsTr("%1 of %2 files copied.-Copying: %3"), new Array("5", "10", "file.txt"));
    
        console.log("str="+str);
    }
    

    }
    @

    1 Reply Last reply
    0
    • D Offline
      D Offline
      digitalsurgeon
      wrote on last edited by
      #2

      i think javascript does not support those argument like functionality for strings

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

        [quote author="agroyer" date="1291363661"]
        How to achieve the same thing in QML ?
        [/quote]

        @
        Text {
        text:done+" of "+total+" files copied.-Copying: "+currentFile
        }
        @

        1 Reply Last reply
        0
        • I Offline
          I Offline
          infidel
          wrote on last edited by
          #4

          The question was how to avoid fixing the string structure as agroyer stated in the first place.

          [quote author="Kxyu" date="1291368244"][quote author="agroyer" date="1291363661"]
          How to achieve the same thing in QML ?
          [/quote]

          @
          Text {
          text:done+" of "+total+" files copied.-Copying: "+currentFile
          }
          @

          [/quote]

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            Indeed, we need to move further away from fixing strings in this way, not moving back towards it. There are many problems with fixing strings, that multiply with localization. One issue is that not all languages would have all arguments in the same place, and another one is that plural froms may work very differently across languages. There may even be more than one plural form, such as in some Eastern European languages.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              thp4
              wrote on last edited by
              #6

              As for a temporary workaround, you could subclass QObject and provide some invokable helper functions that take care of formatting the strings, and use setContextProperty on the QML view to "inject" your string formatting helper. This would also make your QML code easier to read, as you don't do the formatting in there, and the details of how the string is formatted are hidden.

              Imaginary example call:

              @text: formatter.copyProgress(done, total, currentFile)@

              1 Reply Last reply
              0
              • E Offline
                E Offline
                ErikH
                wrote on last edited by
                #7

                I know it's an old thread, but if someone is still interested...

                In qml the qsTr() does take arguments in the same way as QString does. You can write qsTr("%1 copied").arg(filename).

                This will translate the string and then format it using the argument. At least it works fine with string arguments.

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  chrisadams
                  wrote on last edited by
                  #8

                  Yes.

                  The JavaScript environment in QML is slightly modified from the ECMAScript standard. If you take a look at the implementation of the QScriptEngine::installTranslatorFunctions(const QScriptValue &object) function in Qt4, you'll note that we actually modify the String prototype to include the .arg() function. In Qt5 we do a similar modification, in QV8Engine.cpp, where we add the arg function to the String prototype (which calls through to a qml builtin function called stringArg).

                  That is, all JavaScript String objects in QML have a non-standard arg() function, which behaves similarly to the QString::arg() function we're all familiar with from C++.

                  The JavaScript environment also has several other modifications, including adding the qsTr and similar functions to the global object.

                  Cheers,
                  Chris.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    sohail
                    wrote on last edited by
                    #9

                    Appears that the .arg function does not allow you to set a fieldWidth from 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