Results 1 to 14 of 14

Thread: QString operator+= strange behaviour

  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default QString operator+= strange behaviour

    Hi,
    The following code works but does not ouput the desired result.

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QDebug>
    3. #include <QString>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication a(argc, argv);
    8.  
    9. const int timeOut = 3000;
    10. QString string("Time out in wait for ReadyRead " + timeOut);//why no error? , Of course QString::number(timeOut) is right syntax.
    11. qDebug()<<string;//prints nothing.
    12.  
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    I have a long program which uses static QString in that program the qDebug()<<string prints those static string values, that too which are present in another thread(very strange).
    I am not able to reproduce the minimal code which will produce the same bug.

    ThankYou,

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString operator+= strange behaviour

    Why should there be an error here? 3000 is a perfectly good QChar value...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QString operator+= strange behaviour

    (char* + QChar ) is undefined?
    Because QString string("Time out in wait for ReadyRead " + timeOut)
    qDebug()<<string;
    prints nothing.

  4. #4
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString operator+= strange behaviour

    Quote Originally Posted by babu198649 View Post
    (char* + QChar ) is undefined?
    Because QString string("Time out in wait for ReadyRead " + timeOut)
    qDebug()<<string;
    prints nothing.
    I think it's rather char* + int so you're printing some garbage.
    Anyway my compiler gives warning: array subscript is above array bounds
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  5. #5
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QString operator+= strange behaviour

    thanks for reply.

    What compiler you are using? what tool should i use to run a level 3 diagnostic..

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QString operator+= strange behaviour

    QString uses Unicode internally, so QChar is a 16-bit value, but what is probably happening is that your string is being turned into a pointer (char *) and then 3000 is being added to the address of that pointer. Then the resulting string (which may be null) is being passed to qstring. End result is an empty string, thus qDebug shows nothing.

  7. The following user says thank you to squidge for this useful post:

    for_hope (17th January 2010)

  8. #7
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString operator+= strange behaviour

    What compiler you are using?
    gcc 4.4.1

    what tool should i use to run a level 3 diagnostic..
    It's just my signature (it's from Star Trek NG series) The line separating post from signature is not really visible in new QtCentre
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  9. #8
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString operator+= strange behaviour

    Quote Originally Posted by fatjuicymole View Post
    QString uses Unicode internally, so QChar is a 16-bit value, but what is probably happening is that your string is being turned into a pointer (char *) and then 3000 is being added to the address of that pointer. Then the resulting string (which may be null) is being passed to qstring. End result is an empty string, thus qDebug shows nothing.
    You really love answering already answered questions, don't you?

    Anyway the resulting string is not necessarily null, you can convert it to QByteArray and print as hex representation and you'll see the garbage I've mentioned before.
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString operator+= strange behaviour

    Quote Originally Posted by calhal View Post
    The line separating post from signature is not really visible in new QtCentre
    Maybe you would notice it if you ran a level 3 diagnostic?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #10
    Join Date
    Nov 2007
    Posts
    31
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString operator+= strange behaviour

    Quote Originally Posted by babu198649 View Post
    (char* + QChar ) is undefined?
    Because QString string("Time out in wait for ReadyRead " + timeOut)
    qDebug()<<string;
    prints nothing.
    You should add QChar('\0') at the end.
    but if you did not install font set which can display 3000 in unicode code point code, character is not shown.

    Try 0x41 instead of 3000.
    0x41 is 'A' in unicode code point code.

    QString string(QString("Time out in wait for ReadyRead ") + QChar(0x41) + QChar('\0'));
    kichi

  12. #11
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QString operator+= strange behaviour

    Quote Originally Posted by calhal View Post
    You really love answering already answered questions, don't you?
    I think we can both agree that my answer was superior, therefore worth the post

    Nah, just kidding. I understood your answer, but perhaps some people viewing this thread might not have, so I just explained a little more.

  13. #12
    Join Date
    Dec 2009
    Location
    New Zealand
    Posts
    54
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: QString operator+= strange behaviour

    You really love answering already answered questions, don't you?
    Everyone explains things differently and the more explanations available the more likely someone will be to grasp the concept

  14. #13
    Join Date
    Jan 2010
    Posts
    1
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QString operator+= strange behaviour

    Quote Originally Posted by fatjuicymole View Post
    QString uses Unicode internally, so QChar is a 16-bit value, but what is probably happening is that your string is being turned into a pointer (char *) and then 3000 is being added to the address of that pointer. Then the resulting string (which may be null) is being passed to qstring. End result is an empty string, thus qDebug shows nothing.
    I just have a test ,I think u are right

  15. #14
    Join Date
    Nov 2007
    Posts
    31
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString operator+= strange behaviour

    Quote Originally Posted by fatjuicymole View Post
    QString uses Unicode internally, so QChar is a 16-bit value, but what is probably happening is that your string is being turned into a pointer (char *) and then 3000 is being added to the address of that pointer. Then the resulting string (which may be null) is being passed to qstring. End result is an empty string, thus qDebug shows nothing.
    Quote Originally Posted by for_hope View Post
    I just have a test ,I think u are right
    You are right.

    Quote Originally Posted by kichi View Post
    You should add QChar('\0') at the end.
    but if you did not install font set which can display 3000 in unicode code point code, character is not shown.

    Try 0x41 instead of 3000.
    0x41 is 'A' in unicode code point code.

    QString string(QString("Time out in wait for ReadyRead ") + QChar(0x41) + QChar('\0'));
    I wrote wrong.
    I'm sorry.
    kichi

Similar Threads

  1. Need help: Strange behaviour
    By navi1084 in forum Qt Programming
    Replies: 3
    Last Post: 14th November 2008, 04:03
  2. Very strange (perhaps) QHttp behaviour.
    By Kumosan in forum Qt Programming
    Replies: 5
    Last Post: 11th June 2008, 07:03
  3. qinputdialog strange behaviour
    By dreamer in forum Qt Programming
    Replies: 1
    Last Post: 11th May 2008, 19:29
  4. Strange Painter behaviour
    By cwomeijer in forum Qt Programming
    Replies: 2
    Last Post: 4th September 2006, 20:52
  5. very strange behaviour
    By regix in forum Qt Programming
    Replies: 23
    Last Post: 20th July 2006, 17:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.