[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();
    @


  • Lifetime Qt Champion

    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



  • I apologize! and I agree SGaIst.



  • 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.



  • just use
    @
    byteArray.data();
    @
    instead of
    @
    byteArray.constData();
    @
    if you need a non const pointer (no need for a cast), but the question is why do you need that at all? I would not suggest you modify the data in the QByteArray via the char* array..



  • Hi dear Xandeer84.
    I searched & found another way. Like this:

    @
    QString MyQString = "Only Allah";
    const char *MyConstChar = MyQString.toStdString().c_str();
    @



  • You might lose information if you do that, because a std::string cannot contain all the unicode chars from the QString I think.
    I think it would help if you tell us what you want to do with that char* and why you need it.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.