hi all
how can we convert QString to const char*
thanks with regards:
gauravg
hi all
how can we convert QString to const char*
thanks with regards:
gauravg
A camel can go 14 days without drink,
I can't!!!
or QString::toAscii().data()
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
one should never do that.
it should be
Qt Code:
QString s; QByteArrary ba = s.toAscii(); const char* p = ba.constData();To copy to clipboard, switch view to plain text mode
do what?one should never do that.
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.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
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.
Between avoid making a mistake (which means understand what implicit sharing is) to "one should never do that." there is a big difference.its not about const. It about temporary object. Qt docs has mentioned to avoid this mistake.(Read the QByteArray docs)
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:
So you definitively CAN and SHOULD use that for cases where it is needed - provided you READ the documentation, and aware of the pitfalls.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 *.
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.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
I'll quote the Qt docs again with emphasis:
and add some from the C++ standards:The pointer remains valid as long as the byte array isn't reallocated or destroyed.
So, at the statement boundary of:$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."
(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).Qt Code:
char *str = s.toAscii().data();To copy to clipboard, switch view to plain text mode
That does not, in any way, prohibit this immediate usage:
which is safe because the scope of the temporary QByteArray is the end of the complete statement.Qt Code:
printf("%s\n", s.toAscii().constData());To copy to clipboard, switch view to plain text mode
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).y QByteArray created by the toAscii() method is destroyed.
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.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
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.
Just to be clear: my point was NOT about using a temp objects.
Back to our discussion:
I just checked and you are right:The data buffer in the temporary QByteArray is a transformed copy of the data buffer that is in the string's buffer.
toAscii() (via toLatin1()) indeed transforms the data:
Qt Code:
{ 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; }To copy to clipboard, switch view to plain text mode
One of the main disadvantages answering such post during work is that I can often just skim the posts. :-\
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
@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
agreed. if i would have given the full explanation before, then things should have not gone this far.
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(),
Last edited by nish; 15th April 2011 at 12:16.
I was not flaming - and if it was perceived as such, then I am sorry.so there is no point i start flaming like you.
true, and I meant it this way too."all caps" are considered as screaming/shouting/flaming,
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.
Last edited by high_flyer; 15th April 2011 at 13:05.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
no problems.
lets forget it. If i reply then you will reply then i will ... we will trap ourselves in a circular list!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.
looks like we both are in spa.Ergo - since it was not me shouting, I was relaxed.
here in India we use this term for a wise guy only.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.
we all were stupids(newbies) once!! Just ignore the postsP.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.
Thats the great thing about you!.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.
Peace out.
Based on your answer I figured as much.ere in India we use this term for a wise guy only.
Just to make it clear: being newbie != posting stupid.we all were stupids(newbies) once!! Just ignore the posts
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).
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
Bookmarks