Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to fix conversion from ‘QString*’ to non-scalar type ‘QString’ requested

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

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 20.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Naouali
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      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.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      1
      • N Offline
        N Offline
        Naouali
        wrote on last edited by
        #3

        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"

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

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

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • N Offline
            N Offline
            Naouali
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              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":http://doc.qt.nokia.com/4.7/qstring.html#converting-between-8-bit-strings-and-unicode-strings

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              1
              • N Offline
                N Offline
                Naouali
                wrote on last edited by
                #7

                Thanks the QString fromLocal8Bit did the job.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved