PDA

View Full Version : QString operator+= strange behaviour



babu198649
13th January 2010, 11:08
Hi,
The following code works but does not ouput the desired result.


#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QString>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, 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,

wysota
13th January 2010, 11:19
Why should there be an error here? 3000 is a perfectly good QChar value...

babu198649
13th January 2010, 11:43
(char* + QChar ) is undefined?
Because QString string("Time out in wait for ReadyRead " + timeOut)
qDebug()<<string; prints nothing.

calhal
13th January 2010, 12:06
(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

babu198649
13th January 2010, 12:09
thanks for reply.

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

squidge
13th January 2010, 12:13
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.

calhal
13th January 2010, 12:17
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 ;)

calhal
13th January 2010, 12:22
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.

wysota
13th January 2010, 12:23
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:

kichi
13th January 2010, 12:44
(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'));

squidge
13th January 2010, 20:02
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.

Ferric
16th January 2010, 00:42
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 :)

for_hope
17th January 2010, 03:45
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

kichi
17th January 2010, 05:54
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

You are right.


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.