QString operator+= strange behaviour
Hi,
The following code works but does not ouput the desired result.
Code:
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QString>
int main(int argc, char *argv[])
{
const int timeOut = 3000;
QString string
("Time out in wait for ReadyRead " + timeOut
);
//why no error? , Of course QString::number(timeOut) is right syntax. qDebug()<<string;//prints nothing.
return a.exec();
}
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,
Re: QString operator+= strange behaviour
Why should there be an error here? 3000 is a perfectly good QChar value...
Re: QString operator+= strange behaviour
(char* + QChar ) is undefined?
Because QString string("Time out in wait for ReadyRead " + timeOut)
qDebug()<<string; prints nothing.
Re: QString operator+= strange behaviour
Quote:
Originally Posted by
babu198649
(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
Re: QString operator+= strange behaviour
thanks for reply.
What compiler you are using? what tool should i use to run a level 3 diagnostic..
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.
Re: QString operator+= strange behaviour
Quote:
What compiler you are using?
gcc 4.4.1
Quote:
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 ;)
Re: QString operator+= strange behaviour
Quote:
Originally Posted by
fatjuicymole
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.
Re: QString operator+= strange behaviour
Quote:
Originally Posted by
calhal
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? :rolleyes:
Re: QString operator+= strange behaviour
Quote:
Originally Posted by
babu198649
(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'));
Re: QString operator+= strange behaviour
Quote:
Originally Posted by
calhal
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 :D
Nah, just kidding. I understood your answer, but perhaps some people viewing this thread might not have, so I just explained a little more.
Re: QString operator+= strange behaviour
Quote:
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 :)
Re: QString operator+= strange behaviour
Quote:
Originally Posted by
fatjuicymole
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
Re: QString operator+= strange behaviour
Quote:
Originally Posted by
fatjuicymole
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
I just have a test ,I think u are right
You are right.
Quote:
Originally Posted by
kichi
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.