Silly? problem with QString



  • Hi!

    I'm just started work with Qt maybe week ago. I'm learning cpp for four years so I hope the adventure with Qt will be quite exciting ;)

    I tried to search in many places, but still I have silly problem with QString in console application.

    I'm trying to write QString on standard output with using cout

    @
    QString txt = "blabla";
    cout << txt;
    @

    but it will not compile:

    @
    no match for 'operator <<' in 'std::cout << txt'
    @

    But for me it's weird, because I found (in documentation), that << operator is overloaded in QString. And, as far as I know, when we overload operator in class we can use is "normally". As if it was in built-in type.

    I found solution:

    @
    QString txt = "blabla";

    string txt2 = qstrdup(txt.toLatin1());
    
    cout << txt;
    

    @

    But it's not convincing me. Is there quicker way to write QString in console? For example some write function (I mean: txt2.write(), or write(txt))



  • Many Qt developers using qDebug(), but you can use this if you need std::cout:
    @
    cout << qPrintable(txt);
    @

    [quote author="luka.v.pikor" date="1278592314"]But it&#39;s not convincing me. Is there quicker way to write QString in console? For example some write function (I mean: txt2.write(), or write(txt))
    [/quote]

    @
    QTextStream(stdout) << txt;
    @



  • P.S.: need feature for remove or/and join posts
    P.S.: questions like that need a posting to "Desktop":http://developer.qt.nokia.com/forums/viewforum/10/ section



  • @
    QTextStream(stdout) << txt;
    @

    works perfectly! Thanks!

    But what is qPrintable? I can't find it(http://doc.qt.nokia.com/4.6/classes.html) in opposite to qDebug().

    Also, I have the same problem with QDate.

    @
    QDate d1(2010,1,1);
    @

    The TextStream didn't work. I assume that I have to use similar function. Or maybe convert Date type to string format?



  • [quote author="luka.v.pikor" date="1278593533"]@
    Also, I have the same problem with QDate.
    @
    QDate d1(2010,1,1);
    @
    The TextStream didn&#39;t work. I assume that I have to use similar function. Or maybe convert Date type to string format?
    [/quote]

    Use QDate::toString() methods firstly.



  • [quote author="luka.v.pikor" date="1278593533"]
    But what is qPrintable? I can&#39;t find it(http://doc.qt.nokia.com/4.6/classes.html) in opposite to qDebug().
    [/quote]
    You can found this function in QtGlobal module, it's equivalent

    @str.toLocal8Bit().constData()@

    [quote author="luka.v.pikor" date="1278593533"]
    Also, I have the same problem with QDate.
    The TextStream didn&#39;t work.
    [/quote]
    For debugging purpose you can use qDebug(), this function have overloaded operator "<<" for many Qt classes.
    @qDebug() << QDate(2010,1,1);@

    [quote author="luka.v.pikor" date="1278593533"]
    I assume that I have to use similar function. Or maybe convert Date type to string format?
    [/quote]

    Yes, you must use method toString() for this:

    @QTextStream(stdout) << QDate(2010,1,1).toString();@



  • Thanks Denis and Sabrog for helping me. Now it works!



  • [quote author="SABROG" date="1278592777"]P.S.: need feature for remove or/and join posts
    P.S.: questions like that need a posting to "Desktop":http://developer.qt.nokia.com/forums/viewforum/10/ section[/quote]

    Thanks, I've moved it to Desktop now. A moderator can move, merge, split, and delete - but we haven't really begun finding moderators.



  • [quote author="MariusG" date="1278595484"]A moderator can move, merge, split, and delete
    [/quote]

    I think users must have feature for delete own post. About merging, if user post message, forum engine must check, if previous post author in current thread a same, then just auto merge posts if elapsed less than 5-10 minutes. If future merged post have more than character forum limit size, then not merge.



  • [quote author="luka.v.pikor" date="1278592314"]Hi!
    And, as far as I know, when we overload operator in class we can use is "normally". As if it was in built-in type.
    [/quote]
    It's not actually right. You have to have global <<(>>) operator when one of the argument is istream\ostream and second is your type. QString doesn't have this operator but feel free to create your own for the convenient using. @std::ostream& operator<<(std::ostream& xOut, const QString& xStr)
    {
    xOut << xStr.toStdString();
    return xOut;
    }@
    Also, in addition to answers above I see another solution: QString::toStdString would be appropriate function for your purpose


Log in to reply
 

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