[Solved] Convert content of QlineEdite to Char *
-
Hello friends.
I want to convert content of QlineEdit to char *. I know the content is Qsrting.
I found a code that convert Qstring to const char * (not char *).
I really appreciate suggest some code that convert Qstring to char * or const char * o char *.Thanks a lot.
Ya Ali. -
To me, this sounds like:
- Allocate a char* buffer
- Copy the string content from the const char* to the char* buffer
-
get the text from the line edit
@
QString text = ui->lineEdit->text()
@convert the text to QByteArray
@
QByteArray ba = text.toLatin1();
@convert QByteArray to char *
@
const char * characters = ba.data();
@all in all:
@
const char * characters = ui->lineEdit->text().toLatin1().data();
@ -
Hi,
@const char * characters = ui->lineEdit->text().toLatin1().data();@
This one's a dangerous shortcut, it's taking the address of the content of a temporary QByteArray.
@
QByteArray byteArray = ui->lineEdit->text.toLatin1();
const char *characters = byteArray.constData();
@
Is the safe method -
Thanks to all.
Any idea to convert const char * to char *?
Is this a abnormal conversion? -
[quote author="MohammadReza" date="1396059354"]
Is this a abnormal conversion?[/quote]
Usually yes, it is not good idea to do a const cast on a pointer to a buffer that you received.
If you need to modify such buffer then make a copy, modify and put it back using api.
But if you want it then use standard c++ const_cast.
@
const char* bar;
char* foo = const_cast<char*>(bar)
@ -
Thanks to dear andreyc & other friends reply my question.
Solved!Ya Ali.
-
-
Hi dear Xandeer84.
I searched & found another way. Like this:@
QString MyQString = "Only Allah";
const char *MyConstChar = MyQString.toStdString().c_str();
@ -
I had a function that received char *.
There isn`t anymore, because i changed it const char *. -
[quote author="andreyc" date="1396066897"]@
const char* bar;
char* foo = const_cast<char*>(bar)
@
[/quote]That sounds like a really bad idea in this case. Nobody expects the const char* to change, so any changes made will cause trouble.
-
Hi,
@const char * characters = ui->lineEdit->text().toLatin1().data();@
This one's a dangerous shortcut, it's taking the address of the content of a temporary QByteArray.
@
QByteArray byteArray = ui->lineEdit->text.toLatin1();
const char *characters = byteArray.constData();
@
Is the safe method@SGaist Hi, I had an issue with data corruption because of this. In this example I can see that you create a local variable QByteArray instead. Could you please clarify why the content of the QByteArray in one single line case is temporary? I may assume it is because it is called implicitly. In my example I used toStdString().c_str(), in the current function of my example it printed out with qDebug OK but when reading that global const char* in another place it was garbage.
-
@Jc_Opc Hi,
It's a question of object lifetime.
In this case:
const char * characters = ui->lineEdit->text().toLatin1().data();the QByteArray returned by toLatin1 ends its life on the same line it was created.
Thereforecharactersis pointing invalid memory as soon as the line ends.You have the exact same issue:
toStdString().c_str();The std::string returned by toStdString() ends its life the same way as the QByteArray above.
qDebug() << myQString.toStdString().c_str();works because everything happens on the same line.