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. Can't convert from QString to LPCTSTR
Forum Updated to NodeBB v4.3 + New Features

Can't convert from QString to LPCTSTR

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 3.1k Views 1 Watching
  • 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.
  • EngelardE Engelard

    I have:

    LPCTSTR tempLPC = (LPCTSTR)someString.toUtf8();
    

    No other Utf i have, in google all say that must be .toUtf16(); but i dont have that. How to convert it?

    kshegunovK Offline
    kshegunovK Offline
    kshegunov
    Moderators
    wrote on last edited by kshegunov
    #3

    LPCTSTR is either const char * or const wchar_t * or const unsigned short * depending on compiler macros. So you need to handle the preprocessor first. Usually it goes like this:

    #ifdef UNICODE
    LPCWSTR something = someString.utf16();
    #else
    QByteArray latinString = someString.toLatin1();
    LPCSTR something = latinString.constData();
    #endif

    Read and abide by the Qt Code of Conduct

    JonBJ 1 Reply Last reply
    3
    • kshegunovK kshegunov

      LPCTSTR is either const char * or const wchar_t * or const unsigned short * depending on compiler macros. So you need to handle the preprocessor first. Usually it goes like this:

      #ifdef UNICODE
      LPCWSTR something = someString.utf16();
      #else
      QByteArray latinString = someString.toLatin1();
      LPCSTR something = latinString.constData();
      #endif
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #4

      @kshegunov
      I just don't get it (I think I've said before that I get lost on all this Unicode etc. stuff).

      The OP has to put your code in his Qt app code. In his app code, who gets to know/choose whether UNICODE_ is defined? He's going to use LPCTSTR presumably to communicate with something external, and that's already compiled one way or the other. Sigh.

      kshegunovK 1 Reply Last reply
      0
      • JonBJ JonB

        @kshegunov
        I just don't get it (I think I've said before that I get lost on all this Unicode etc. stuff).

        The OP has to put your code in his Qt app code. In his app code, who gets to know/choose whether UNICODE_ is defined? He's going to use LPCTSTR presumably to communicate with something external, and that's already compiled one way or the other. Sigh.

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #5

        @JonB said in Can't convert from QString to LPCTSTR:

        I just don't get it (I think I've said before that I get lost on all this Unicode etc. stuff).

        Nobody gets the windows API! ;)
        Here he wants to convert a Qt type to a WinAPI type to (presumably) pass it on to some function in the WinAPI. The macro is defined in one of the system headers (I think windows.h from the SDK) and depends on how the windows was built/the compiler used. Granted any new windows (i.e. 95+) should already have that defined and you'd always have TCHAR = wchar_t *, but nobody can grasp the depths of the dark forest the WinAPI is. In any case, he needs to match what windows, or rather the function he intents to use, expects.

        Take a look here as well. It should shed some light. Windows just uses macros for defining ApiFunctionName to be either ApiFunctionNameA (if using ASCII) or ApiFunctionNameW if using utf16. Wreaks all kinds of havoc when you need to actually use it.

        Read and abide by the Qt Code of Conduct

        JonBJ 1 Reply Last reply
        4
        • kshegunovK kshegunov

          @JonB said in Can't convert from QString to LPCTSTR:

          I just don't get it (I think I've said before that I get lost on all this Unicode etc. stuff).

          Nobody gets the windows API! ;)
          Here he wants to convert a Qt type to a WinAPI type to (presumably) pass it on to some function in the WinAPI. The macro is defined in one of the system headers (I think windows.h from the SDK) and depends on how the windows was built/the compiler used. Granted any new windows (i.e. 95+) should already have that defined and you'd always have TCHAR = wchar_t *, but nobody can grasp the depths of the dark forest the WinAPI is. In any case, he needs to match what windows, or rather the function he intents to use, expects.

          Take a look here as well. It should shed some light. Windows just uses macros for defining ApiFunctionName to be either ApiFunctionNameA (if using ASCII) or ApiFunctionNameW if using utf16. Wreaks all kinds of havoc when you need to actually use it.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #6

          @kshegunov

          depends on how the windows was built. Granted any new windows (i.e. 95+) should already have that defined and you'd always have TCHAR = wchar_t *

          Ah, to do with how Windows was built last century, OK. Yep I get that.

          In that case, in the name of readability, why, my friend, do you not automatically suggest:

          #ifdef UNICODE
          LPCWSTR something = someString.toStdWstring().c_str();
          #else
          LPCSTR something = someString.toStdString().c_str();
          #endif
          

          instead of the scary-looking ones you're choosing?

          kshegunovK 1 Reply Last reply
          0
          • JonBJ JonB

            @kshegunov

            depends on how the windows was built. Granted any new windows (i.e. 95+) should already have that defined and you'd always have TCHAR = wchar_t *

            Ah, to do with how Windows was built last century, OK. Yep I get that.

            In that case, in the name of readability, why, my friend, do you not automatically suggest:

            #ifdef UNICODE
            LPCWSTR something = someString.toStdWstring().c_str();
            #else
            LPCSTR something = someString.toStdString().c_str();
            #endif
            

            instead of the scary-looking ones you're choosing?

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #7

            @JonB said in Can't convert from QString to LPCTSTR:

            In that case, in the name of readability, why, my friend, do you not automatically suggest

            Returning pointers to temporaries is rather dangerous ... ;)

            Read and abide by the Qt Code of Conduct

            JonBJ 1 Reply Last reply
            2
            • kshegunovK kshegunov

              @JonB said in Can't convert from QString to LPCTSTR:

              In that case, in the name of readability, why, my friend, do you not automatically suggest

              Returning pointers to temporaries is rather dangerous ... ;)

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #8

              @kshegunov
              Damn it! OK depends on context, got it! Yours still looks ugly though :)

              kshegunovK 1 Reply Last reply
              0
              • JonBJ JonB

                @kshegunov
                Damn it! OK depends on context, got it! Yours still looks ugly though :)

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by kshegunov
                #9

                @JonB said in Can't convert from QString to LPCTSTR:

                Yours still looks ugly though

                Through no fault of mine, complain to the WinAPI designers. :)

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0
                • EngelardE Offline
                  EngelardE Offline
                  Engelard
                  wrote on last edited by
                  #10

                  UPDATE:

                  how to convert to opposite now?))

                  From LPCTSTR to QString, i tried

                  QString::fromUtf16(lpctVAR)
                  

                  But it dont work...

                  kshegunovK 1 Reply Last reply
                  0
                  • EngelardE Engelard

                    UPDATE:

                    how to convert to opposite now?))

                    From LPCTSTR to QString, i tried

                    QString::fromUtf16(lpctVAR)
                    

                    But it dont work...

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #11

                    @Engelard said in Can't convert from QString to LPCTSTR:

                    how to convert to opposite now?

                    Same idea, either use QString::fromUtf16 or QString::fromLatin1 depending on the macro.

                    Read and abide by the Qt Code of Conduct

                    EngelardE 1 Reply Last reply
                    1
                    • kshegunovK kshegunov

                      @Engelard said in Can't convert from QString to LPCTSTR:

                      how to convert to opposite now?

                      Same idea, either use QString::fromUtf16 or QString::fromLatin1 depending on the macro.

                      EngelardE Offline
                      EngelardE Offline
                      Engelard
                      wrote on last edited by
                      #12

                      @kshegunov as i said, fromUtf16 not working(beneath screen of error), and latin1 also, it was before utf

                      alt text

                      J.HilkJ 1 Reply Last reply
                      0
                      • EngelardE Engelard

                        @kshegunov as i said, fromUtf16 not working(beneath screen of error), and latin1 also, it was before utf

                        alt text

                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by J.Hilk
                        #13

                        @Engelard well theres all the information you need, pass the length of the LPCSTR as 2nd parameter of fromUtf16


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        1 Reply Last reply
                        3

                        • Login

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