[SOLVED] QString and .arg()



  • 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


  • Moderators

    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);
    @



  • 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.


  • Moderators

    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));
    @



  • 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.



  • Tried it out.

    It works.

    Thank you for explaining it to me.


  • Moderators

    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.


Log in to reply
 

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