Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QString and .arg()
QtWS25 Last Chance

[SOLVED] QString and .arg()

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 2.2k Views
  • 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.
  • P Offline
    P Offline
    Paawan
    wrote on last edited by
    #1

    Hi Guys,

    Need some help. As usual.

    Is it possible to split the QString and .arg() parts.

    For example:

    @

    QString phrase = "bingo";

    QString test1 = ("%1");

    test1.arg(phrase);

    qDebug() << test1;

    @

    I am trying to make it work but when I run the program it is displaying "%1" not "bingo" as I had expected.

    Thanks

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      "QString::arg":http://qt-project.org/doc/qt-5/qstring.html#arg returns a copy so you should assign it to other QString and the print.

      @
      QString tmp = test1.arg(phrase);
      qDebug() << tmp;
      @

      or

      @
      qDebug() << test1.arg(phrase);
      @

      157

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Paawan
        wrote on last edited by
        #3

        Thanks Gene. But the problem is something is as given below:

        @
        QString Entertainment::toString(bool labeled, QString sepchar) const{
        QString result("%1/ilm Type: %3Oilm Rating: %5");
        if(!labeled){
        result = QString("%1%2%3%4%5");
        }
        return result.arg(Film::toString(labeled, sepchar)).arg(sepchar).arg(FilmTypesStrings.at(m_Type)).arg(sepchar)
        .arg(MPAARatingsStrings.at(m_Rating));
        }
        @

        What I cannot understand in the above is:

        In line 2 how is the result getting its values?

        In line 4 how is result getting its values?

        Thanks again.

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          It is getting from here
          @
          return result.arg(Film::toString(labeled, sepchar)).arg(sepchar).arg(FilmTypesStrings.at(m_Type)).arg(sepchar)
          .arg(MPAARatingsStrings.at(m_Rating));
          @

          157

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Paawan
            wrote on last edited by
            #5

            Ok.

            Am I correct in saying that in line 2 and line 4 in my code it is just QString "result" is just creating a place holder.

            and the actual arguments are given when it is "result" being returned?

            Thanks again.

            1 Reply Last reply
            0
            • P Offline
              P Offline
              Paawan
              wrote on last edited by
              #6

              Tried it out.

              It works.

              Thank you for explaining it to me.

              1 Reply Last reply
              0
              • p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #7

                You're Welcome :)

                bq. Am I correct in saying that in line 2 and line 4 in my code it is just QString “result” is just creating a place holder.

                Yes.

                bq. and the actual arguments are given when it is “result” being returned?

                Yes, the function first gets executed and then the result is returned.

                157

                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