How to fix conversion from ‘QString*’ to non-scalar type ‘QString’ requested

  • Hi guys ,
    I m trying to convert const char * to QString but i get the previous error.
    here's the code i m using :

    const char * hyp ;
    QString res=new QString(hyp);
    return res;

    please help me to fix it .

    Thanks in advance .

    EDIT: please use @-tags to use code highlighting, thanks. Gerolf

  • This is standard C++, if you just want to return a QString object (not on the heap!), use the following:

    const char * hyp ;
    return QString::fromLatin1(hyp);

    A QString is something different from QString*. new creates a new object on the heap and returns a pointer. But you used an object on the stack. As you just want to return the stirng, you can use my code. Regard, I used the fromLatin1() method, to use a defined conversion. You could also use fromAscii, from Utf8, ..., depending on the encoding of your char*.

    another possibilty is:

    const char * hyp ;
    QString res(QString::fromLatin1(hyp));

    return res;


    Here I use a copy constructor which may be more efficiently than the assignment operator.

  • I got another error when i try your solution .
    Normally the function should return this string :go forward ten meters but i get this string: " Ê orward ten meters"

  • can you please give some more code of you?
    I'm sure it's the text encoding.

  • Ok , let me explain how the program is working :
    I m writing a speech recognizer program , using the pocketsphinx library which is writen in C language .
    the result of the recognizer is the hyp variable and i want to wrap it in QString to display it in QTextEdit.

  • but the hyp variable content, in which encoding is it?
    This is needed to know how to convert from a char* to a QString.
    char* is one byte ascii, latin1, utf8, mbcs, different encodings, all this is possible and needs to be regarded on conversion from char* to QString.

    if you look at the QString docu, you see, there are many conversion functions:

    • QString fromAscii ( const char * str, int size = -1 )
    • QString fromLatin1 ( const char * str, int size = -1 )
    • QString fromLocal8Bit ( const char * str, int size = -1 )
    • QString fromRawData ( const QChar * unicode, int size )
    • QString fromStdString ( const std::string & str )
    • QString fromStdWString ( const std::wstring & str )
    • QString fromUcs4 ( const uint * unicode, int size = -1 )
    • QString fromUtf8 ( const char * str, int size = -1 )
    • QString fromUtf16 ( const ushort * unicode, int size = -1 )

    if you go further in the docs, you see:

    bq. QString converts the const char * data into Unicode using the fromAscii() function. By default, fromAscii() treats character above 128 as Latin-1 characters, but this can be changed by calling QTextCodec::setCodecForCStrings().
    In all of the QString functions that take const char * parameters, the const char * is interpreted as a classic C-style '\0'-terminated string. It is legal for the const char * parameter to be 0.

    you should also read: "Converting Between 8-Bit Strings and Unicode Strings":

  • Thanks the QString fromLocal8Bit did the job.

Log in to reply