PDA

View Full Version : how to conver QString to const char*



gauravg
13th April 2011, 09:33
hi all
how can we convert QString to const char*


thanks with regards:
gauravg

mcosta
13th April 2011, 09:42
Use QtGlobal::qPrintable

high_flyer
13th April 2011, 12:03
or QString::toAscii().data()

nish
13th April 2011, 12:52
or QString::toAscii().data()
one should never do that.

it should be


QString s;
QByteArrary ba = s.toAscii();
const char* p = ba.constData();

high_flyer
13th April 2011, 13:03
one should never do that.
do what?
Your code is "doing" what I suggested.
You are using the const variant, it depends on your needs if you need the const or not.

MarekR22
13th April 2011, 13:13
Just remember that QString::toAscii().data() involves use of temporary object (of QByteArrary), so in next line this pointer is not valid anymore. That is why nish has given more safe solution.

nish
13th April 2011, 14:31
do what?
Your code is "doing" what I suggested.
You are using the const variant, it depends on your needs if you need the const or not.

its not about const. It about temporary object. Qt docs has mentioned to avoid this mistake.(Read the QByteArray docs)

high_flyer
13th April 2011, 16:00
its not about const. It about temporary object. Qt docs has mentioned to avoid this mistake.(Read the QByteArray docs)
Between avoid making a mistake (which means understand what implicit sharing is) to "one should never do that." there is a big difference.
If YOU read the docs for QByteArray::data() you will see, that they don't say anything about NOT using it.
They do say however:

The pointer remains valid as long as the byte array isn't reallocated or destroyed. For read-only access, constData() is faster because it never causes a deep copy to occur.

This function is mostly useful to pass a byte array to a function that accepts a const char *.

So you definitively CAN and SHOULD use that for cases where it is needed - provided you READ the documentation, and aware of the pitfalls.
There are ofte several possibles to do the same thing - and one should look at the best suited option.

People, you should really stop with these "NEVER DO <something> OR THE WORLD WILL END" assertions.
The methods are there to be used, just make sure you understand what you are doing when you use them and what the consequences are.
So relax.

ChrisW67
14th April 2011, 01:17
I'll quote the Qt docs again with emphasis:

The pointer remains valid as long as the byte array isn't reallocated or destroyed.
and add some from the C++ standards:

$12.2/3- "Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created. This is true even if that evaluation ends in throwing an exception."

So, at the statement boundary of:


char *str = s.toAscii().data();

(s is QString) the temporary QByteArray created by the toAscii() method is destroyed. Most of the time immediately subsequent access through str will appear to work because the memory has not yet been allocated elsewhere and modified. You can be sure when it does fail intermittently you will be scratching your head. If you are going to store a pointer to the char data for later use then you must control the scope of the QByteArray by making it explicit in some form (which is what was suggested).

That does not, in any way, prohibit this immediate usage:


printf("%s\n", s.toAscii().constData());
which is safe because the scope of the temporary QByteArray is the end of the complete statement.

high_flyer
14th April 2011, 10:13
y QByteArray created by the toAscii() method is destroyed.
True, however it is implicitly shared, so the pointer it was pointing at still lives, as long as the original 's' is alive.(unless you have done something to alter the content of the pointer, which is why const is a better choice).

At any rate, my suggestion was wrongly understood.
If you read my original post, you will see the syntax is not correct too.
I just meant that one could use toAscii() by getting the QByteArray from the QString, that is all.
So when you do it, make sure you are not working with the temp instance of it.
But the reaction "YOU SHOULD NEVER..." assertion starts to annoy me, (it gets more and more to be seen around here).
If you should never call a specific function, it should not exist.
If it does, you should call it, in constraints of its usage and when its appropriate - that was my point.
If it is, or not appropriate in a specific case, is another issue.

ChrisW67
14th April 2011, 11:56
True, however it is implicitly shared, so the pointer it was pointing at still lives, as long as the original 's' is alive.(unless you have done something to alter the content of the pointer, which is why const is a better choice).

You are retrieving a pointer to a data buffer inside the temporary QByteArray not a pointer to the buffer underlying the QString. The data buffer in the temporary QByteArray is a transformed copy of the data buffer that is in the string's buffer. That buffer is not shared with the string and goes away with the temporary QByteArray.

high_flyer
14th April 2011, 12:29
Just to be clear: my point was NOT about using a temp objects.

Back to our discussion:

The data buffer in the temporary QByteArray is a transformed copy of the data buffer that is in the string's buffer.
I just checked and you are right:
toAscii() (via toLatin1()) indeed transforms the data:


QByteArray QString::toLatin1() const
{
QByteArray ba;
if (d->size) {
ba.resize(d->size);
const ushort *i = d->data;
const ushort *e = d->data + d->size;
uchar *s = (uchar*) ba.data();
while (i != e) {
*s++ = (*i>0xff) ? '?' : (uchar) *i;
++i;
}
}
return ba;
}


One of the main disadvantages answering such post during work is that I can often just skim the posts. :-\

nish
15th April 2011, 13:11
@high_flyer
i think your reply to me has been replied, so there is no point i start flaming like you. so i will not answer your full post. but here it goes :)



People, you should really stop with these "NEVER DO <something> OR THE WORLD WILL END" assertions.

agreed. if i would have given the full explanation before, then things should have not gone this far.



So relax.
ok me relaxing. But read the rules of the internet, "all caps" are considered as screaming/shouting/flaming, so looks like the 'wiseguy' need some relaxing. :)


EDIT:-
and yes i just typed the constData() coz i have a habbit of using this function, it dosent matter in our OP case wether we use data() or constData(),

high_flyer
15th April 2011, 13:59
so there is no point i start flaming like you.
I was not flaming - and if it was perceived as such, then I am sorry.

"all caps" are considered as screaming/shouting/flaming,
true, and I meant it this way too.
But please note the quotes "" - this is not something I am saying but others - I am quoting others - in this case you - I capitalized to make it a bit more extreme - but even with out capitalizing saying "never do ... !" is maybe not shouting but close.
Ergo - since it was not me shouting, I was relaxed.
And - a 'wiseguy' is not a wise guy - as in smart, rather it is meant more as a negative term - its a slang word for mafia members, trouble maker.

P.S
At times (and in recent times even more) I tend to react a bit harsh, the reason is, that there are a LOT of posts which are really stupid.
Sometimes, I misjudge a post, and give it cold shower when it was not quite called for.
But you will not find serious posts treated this way by me, and I have no problem saying sorry or that I made a mistake if I am proven wrong.

nish
15th April 2011, 14:48
I was not flaming - and if it was perceived as such, then I am sorry.
no problems :D.



But please note the quotes "" - this is not something I am saying but others - I am quoting others - in this case you - I capitalized to make it a bit more extreme - but even with out capitalizing saying "never do ... !" is maybe not shouting but close.
lets forget it. If i reply then you will reply then i will ... we will trap ourselves in a circular list!


Ergo - since it was not me shouting, I was relaxed.
looks like we both are in spa.



And - a 'wiseguy' is not a wise guy - as in smart, rather it is meant more as a negative term - its a slang word for mafia members, trouble maker.
here in India we use this term for a wise guy only.


P.S
At times (and in recent times even more) I tend to react a bit harsh, the reason is, that there are a LOT of posts which are really stupid.
Sometimes, I misjudge a post, and give it cold shower when it was not quite called for.
we all were stupids(newbies) once!! Just ignore the posts :D


But you will not find serious posts treated this way by me, and I have no problem saying sorry or that I made a mistake if I am proven wrong.
Thats the great thing about you!.

Peace out.

high_flyer
15th April 2011, 14:58
ere in India we use this term for a wise guy only.
Based on your answer I figured as much.


we all were stupids(newbies) once!! Just ignore the posts
Just to make it clear: being newbie != posting stupid.
There are newbie posts which are legitimate.
Many even.
But some just rather post and get spoon fed instead of first searching, investing some thought or simple search on google/docs - and at least try to get to the answer by them selves - resulting in a stupid post.
The forum is here for asking and discussing Qt related issues, newbies SHOULD post and ask.
But when you post show that you have invested at least some effort in finding the solution. (I don't mean you, but as a figure of peach).

nish
15th April 2011, 15:03
Just to make it clear: being newbie != posting stupid.
and thats why i wrote newbie in brackets().



There are newbie posts which are legitimate.
Many even.
But some just rather post and get spoon fed instead of first searching, investing some thought or simple search on google/docs - and at least try to get to the answer by them selves - resulting in a stupid post.
The forum is here for asking and discussing Qt related issues, newbies SHOULD post and ask.
But when you post show that you have invested at least some effort in finding the solution. (I don't mean you, but as a figure of peach).
yup.