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
QtWS25 Last Chance

Can't convert from QString to LPCTSTR

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 2.9k 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.
  • E Offline
    E Offline
    Engelard
    wrote on 10 Aug 2018, 16:07 last edited by Engelard 8 Oct 2018, 16:07
    #1

    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?

    J K 2 Replies Last reply 10 Aug 2018, 16:14
    0
    • E Engelard
      10 Aug 2018, 16:07

      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?

      J Offline
      J Offline
      JonB
      wrote on 10 Aug 2018, 16:14 last edited by JonB 8 Oct 2018, 16:19
      #2

      @Engelard
      What about

      str.toStdString().c_str();
      

      google all say that must be .toUtf16(); but i dont have that

      QString does not have .toUtf16(). It does have .utf16(). Which would work. But will require a cast. Which people don't like these days... :)

      1 Reply Last reply
      1
      • E Engelard
        10 Aug 2018, 16:07

        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?

        K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 10 Aug 2018, 16:20 last edited by kshegunov 8 Oct 2018, 16:41
        #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

        J 1 Reply Last reply 10 Aug 2018, 16:36
        3
        • K kshegunov
          10 Aug 2018, 16:20

          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
          J Offline
          J Offline
          JonB
          wrote on 10 Aug 2018, 16:36 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.

          K 1 Reply Last reply 10 Aug 2018, 16:39
          0
          • J JonB
            10 Aug 2018, 16:36

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

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 10 Aug 2018, 16:39 last edited by kshegunov 8 Oct 2018, 16:45
            #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

            J 1 Reply Last reply 10 Aug 2018, 16:47
            4
            • K kshegunov
              10 Aug 2018, 16:39

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

              J Offline
              J Offline
              JonB
              wrote on 10 Aug 2018, 16:47 last edited by JonB 8 Oct 2018, 16:48
              #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?

              K 1 Reply Last reply 10 Aug 2018, 16:47
              0
              • J JonB
                10 Aug 2018, 16:47

                @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?

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 10 Aug 2018, 16:47 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

                J 1 Reply Last reply 10 Aug 2018, 16:50
                2
                • K kshegunov
                  10 Aug 2018, 16:47

                  @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 ... ;)

                  J Offline
                  J Offline
                  JonB
                  wrote on 10 Aug 2018, 16:50 last edited by JonB 8 Oct 2018, 16:51
                  #8

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

                  K 1 Reply Last reply 10 Aug 2018, 16:59
                  0
                  • J JonB
                    10 Aug 2018, 16:50

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

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 10 Aug 2018, 16:59 last edited by kshegunov 8 Oct 2018, 16:59
                    #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
                    • E Offline
                      E Offline
                      Engelard
                      wrote on 10 Aug 2018, 17:39 last edited by
                      #10

                      UPDATE:

                      how to convert to opposite now?))

                      From LPCTSTR to QString, i tried

                      QString::fromUtf16(lpctVAR)
                      

                      But it dont work...

                      K 1 Reply Last reply 10 Aug 2018, 17:48
                      0
                      • E Engelard
                        10 Aug 2018, 17:39

                        UPDATE:

                        how to convert to opposite now?))

                        From LPCTSTR to QString, i tried

                        QString::fromUtf16(lpctVAR)
                        

                        But it dont work...

                        K Offline
                        K Offline
                        kshegunov
                        Moderators
                        wrote on 10 Aug 2018, 17:48 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

                        E 1 Reply Last reply 10 Aug 2018, 17:55
                        1
                        • K kshegunov
                          10 Aug 2018, 17:48

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

                          E Offline
                          E Offline
                          Engelard
                          wrote on 10 Aug 2018, 17:55 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 10 Aug 2018, 18:29
                          0
                          • E Engelard
                            10 Aug 2018, 17:55

                            @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 10 Aug 2018, 18:29 last edited by J.Hilk 8 Oct 2018, 18:29
                            #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

                            5/13

                            10 Aug 2018, 16:39

                            topic:navigator.unread, 8
                            • Login

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