Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Can't convert from QString to LPCTSTR

    General and Desktop
    4
    13
    2052
    Loading More Posts
    • 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.
    • Engelard
      Engelard last edited by 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?

      JonB kshegunov 2 Replies Last reply Reply Quote 0
      • JonB
        JonB @Engelard last edited by JonB

        @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 Reply Quote 1
        • kshegunov
          kshegunov Moderators @Engelard last edited by 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

          Read and abide by the Qt Code of Conduct

          JonB 1 Reply Last reply Reply Quote 3
          • JonB
            JonB @kshegunov last edited by

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

            kshegunov 1 Reply Last reply Reply Quote 0
            • kshegunov
              kshegunov Moderators @JonB last edited by 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.

              Read and abide by the Qt Code of Conduct

              JonB 1 Reply Last reply Reply Quote 4
              • JonB
                JonB @kshegunov last edited by 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?

                kshegunov 1 Reply Last reply Reply Quote 0
                • kshegunov
                  kshegunov Moderators @JonB last edited by

                  @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

                  JonB 1 Reply Last reply Reply Quote 2
                  • JonB
                    JonB @kshegunov last edited by JonB

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

                    kshegunov 1 Reply Last reply Reply Quote 0
                    • kshegunov
                      kshegunov Moderators @JonB last edited by kshegunov

                      @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 Reply Quote 0
                      • Engelard
                        Engelard last edited by

                        UPDATE:

                        how to convert to opposite now?))

                        From LPCTSTR to QString, i tried

                        QString::fromUtf16(lpctVAR)
                        

                        But it dont work...

                        kshegunov 1 Reply Last reply Reply Quote 0
                        • kshegunov
                          kshegunov Moderators @Engelard last edited by

                          @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

                          Engelard 1 Reply Last reply Reply Quote 1
                          • Engelard
                            Engelard @kshegunov last edited by

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

                            alt text

                            J.Hilk 1 Reply Last reply Reply Quote 0
                            • J.Hilk
                              J.Hilk Moderators @Engelard last edited by J.Hilk

                              @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

                              Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

                              1 Reply Last reply Reply Quote 3
                              • First post
                                Last post