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. Using variables ending with numbers
Forum Updated to NodeBB v4.3 + New Features

Using variables ending with numbers

Scheduled Pinned Locked Moved General and Desktop
11 Posts 6 Posters 2.2k 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.
  • K Offline
    K Offline
    koahnig
    wrote on last edited by
    #2

    According to the docs of Qt5 it would more like:
    @
    QString str;
    str = "nom%1";

    str.arg("", "X");
    @

    [edit, running into an issue with the editor here.
    the first argument of str.arg shall be "% 1 f" without spaces. ]

    However, I am not sure it looks stupid and might be wrong in the docs. I do not use QString very often, more the std::string variant.

    But this should work:
    @
    QString str;
    str = "nom";
    QString str2 = str + QString ("X");
    @
    or
    @
    QString str;
    str = "nom";
    QString str2 = str + "X";
    @

    Vote the answer(s) that helped you to solve your issue(s)

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

      Hi,
      Thank you for your reply.
      My request was not clear. Let me put it that way:

      @
      in header
      QString nom1;
      QString nom2;
      @
      @
      in .cpp
      nom1="Sam"
      nom2"Pierre";

      QString st="nom";
      QString st2=st+"1";
      st2="Paul";
      // nom1 should now be : Paul - but it isn't , only st2="Paul"
      @

      Christian EhrlicherC 1 Reply Last reply
      0
      • D Offline
        D Offline
        DerManu
        wrote on last edited by
        #4

        @QStringList nom;
        nom.append("Sam");
        nom.append("Pierre");

        QString someOtherString = nom.at(0); // will be "Sam"@

        This isn't a scripting language, so building variable identifiers from strings is rightly discouraged, for performance, security and anti-spaghetti-code reasons. Through hackery (a.k.a. meta object system), it is possible though. But don't do it. Leads to bad programs, and we've got plenty of those.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #5

          As already explained by DerManu.

          Or you may use "QMap":http://qt-project.org/doc/qt-5.0/qtcore/qmap.html .
          @
          QMap<QString, QString> myMap;
          myMap["nom1"]="Sam";
          myMap["nom2"]="Pierre";
          myMap["nom3"]="Paul";

          QString st = "nom";
          QString st2 = st + "1";

          qDebug() << myMap[st2];
          @
          should work.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DerManu
            wrote on last edited by
            #6

            @koahnig, I slightly disagree with the QMap usage here. Because that number in the end of "nom" just screams for an integer index based container like QVector or QList (@phil: QStringList is just QList<QString>), and so one should use them, and not the more key-generic QMap<QString, QString>. If non-continuous index ranges are needed, QMap<int, QString> would be okay.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #7

              [quote author="DerManu" date="1371374572"]@koahnig, I slightly disagree with the QMap usage here. Because that number in the end of "nom" just screams for an integer index based container like QVector or QList (@phil: QStringList is just QList<QString>), and so one should use them, and not the more key-generic QMap<QString, QString>. If non-continuous index ranges are needed, QMap<int, QString> would be okay.[/quote]
              Yes, you are right, but the question is what is the real case at the end. If it is a simplified example and the names will be a bit more complex and not just a number appended, it might make sense.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • P Offline
                P Offline
                phil63
                wrote on last edited by
                #8

                Thank you to both of you

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  AdamHynes
                  wrote on last edited by
                  #9
                  This post is deleted!
                  JonBJ 1 Reply Last reply
                  0
                  • A AdamHynes

                    This post is deleted!

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #10

                    @AdamHynes ...Might interfere with people's ability to write good Qt code... ;-)

                    1 Reply Last reply
                    0
                    • P phil63

                      Hi,
                      Thank you for your reply.
                      My request was not clear. Let me put it that way:

                      @
                      in header
                      QString nom1;
                      QString nom2;
                      @
                      @
                      in .cpp
                      nom1="Sam"
                      nom2"Pierre";

                      QString st="nom";
                      QString st2=st+"1";
                      st2="Paul";
                      // nom1 should now be : Paul - but it isn't , only st2="Paul"
                      @

                      Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11
                      This post is deleted!
                      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