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. Convert QString to wchar_t*

Convert QString to wchar_t*

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 6 Posters 14.3k 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.
  • M Offline
    M Offline
    Mr Gisa
    wrote on 14 Jul 2018, 01:58 last edited by
    #1

    How to convert from QString to wchar_t*? I have a function that asks for a wchar_t* and I need to convert a QString I have to pass as argument.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      gkosi
      wrote on 14 Jul 2018, 06:00 last edited by
      #2
      This post is deleted!
      A 1 Reply Last reply 14 Jul 2018, 07:28
      1
      • G gkosi
        14 Jul 2018, 06:00

        This post is deleted!

        A Offline
        A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on 14 Jul 2018, 07:28 last edited by
        #3

        hi @gkosi

        two comments to your code:

        1. the array needs to be at least the size of the sting contents
        2. the array is not null terminated, so you should make it one element larger and set this one to zero

        Regards

        Qt has to stay free or it will die.

        1 Reply Last reply
        3
        • M Offline
          M Offline
          Mr Gisa
          wrote on 14 Jul 2018, 07:48 last edited by Mr Gisa
          #4

          I did it like this:

              // MAX_FILENAME_SIZE = 2048
              wchar_t filenameW[MAX_FILENAME_SIZE];
              int length = filename.left(MAX_FILENAME_SIZE - 1).toWCharArray(filenameW);
              filenameW[length] = '\0';
          

          Any tips for improvement?

          1 Reply Last reply
          0
          • V Offline
            V Offline
            VRonin
            wrote on 16 Jul 2018, 07:57 last edited by
            #5

            Since you have to use a container anyway, instead of a raw array I'd just use std::wstring:

            QString testStr("Test");
            std::wstring  testWstring  = testStr.toStdWString();
            functionThatTakesWcharPointer(testWstring.c_str());
            

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            M 1 Reply Last reply 17 Jul 2018, 04:26
            2
            • V VRonin
              16 Jul 2018, 07:57

              Since you have to use a container anyway, instead of a raw array I'd just use std::wstring:

              QString testStr("Test");
              std::wstring  testWstring  = testStr.toStdWString();
              functionThatTakesWcharPointer(testWstring.c_str());
              
              M Offline
              M Offline
              Mr Gisa
              wrote on 17 Jul 2018, 04:26 last edited by Mr Gisa
              #6

              @VRonin c_str returns a const right? It's not a const that it asks for, so I'm getting an error saying that can't convert from const wchar_t* to wchar_t*.

              ... I just put a const_cast<wchar_t *> and it compiled but the string isn't right, it's like this:

              0_1531804584324_5d3e913f-d6ed-4153-8e0b-efaa5aeb84e9-image.png

              D 1 Reply Last reply 17 Jul 2018, 06:09
              0
              • M Mr Gisa
                17 Jul 2018, 04:26

                @VRonin c_str returns a const right? It's not a const that it asks for, so I'm getting an error saying that can't convert from const wchar_t* to wchar_t*.

                ... I just put a const_cast<wchar_t *> and it compiled but the string isn't right, it's like this:

                0_1531804584324_5d3e913f-d6ed-4153-8e0b-efaa5aeb84e9-image.png

                D Offline
                D Offline
                Devopia53
                wrote on 17 Jul 2018, 06:09 last edited by
                #7

                @Mr-Gisa

                Just use like this:

                QString qString("Test");
                wchar_t *wc = reinterpret_cast<wchar_t *>(qString.data())
                const wchar_t *cwc = reinterpret_cast<const wchar_t *>(qString.utf16());
                
                A 1 Reply Last reply 17 Jul 2018, 06:16
                0
                • D Devopia53
                  17 Jul 2018, 06:09

                  @Mr-Gisa

                  Just use like this:

                  QString qString("Test");
                  wchar_t *wc = reinterpret_cast<wchar_t *>(qString.data())
                  const wchar_t *cwc = reinterpret_cast<const wchar_t *>(qString.utf16());
                  
                  A Offline
                  A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on 17 Jul 2018, 06:16 last edited by aha_1980
                  #8

                  Hi @Devopia53,

                  Your code assumes that wchar_t is 16 bit wide. That is not always the case, see the already mentioned docs.

                  Qt has to stay free or it will die.

                  1 Reply Last reply
                  2
                  • V Offline
                    V Offline
                    VRonin
                    wrote on 17 Jul 2018, 07:11 last edited by
                    #9

                    A function that takes non-const wchar_t* is very dodgy! it should have no way of knowing the maximum allocated memory to that pointer/array so it is either allocating it internally (hence our discussion has been pointless so far) or that function is terribly designed and it's just a ticking time bomb.
                    Can I ask what that function does?

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    M V 2 Replies Last reply 17 Jul 2018, 07:35
                    2
                    • V VRonin
                      17 Jul 2018, 07:11

                      A function that takes non-const wchar_t* is very dodgy! it should have no way of knowing the maximum allocated memory to that pointer/array so it is either allocating it internally (hence our discussion has been pointless so far) or that function is terribly designed and it's just a ticking time bomb.
                      Can I ask what that function does?

                      M Offline
                      M Offline
                      Mr Gisa
                      wrote on 17 Jul 2018, 07:35 last edited by
                      #10

                      @VRonin It's not a function actually, sorry for the bad information, it's a struct.

                      struct RAROpenArchiveDataEx
                      {
                        char         *ArcName;
                        wchar_t      *ArcNameW;
                        unsigned int  OpenMode;
                        unsigned int  OpenResult;
                        char         *CmtBuf;
                        unsigned int  CmtBufSize;
                        unsigned int  CmtSize;
                        unsigned int  CmtState;
                        unsigned int  Flags;
                        UNRARCALLBACK Callback;
                        LPARAM        UserData;
                        unsigned int  Reserved[28];
                      };
                      

                      ArcNameW
                      Input parameter which should point to zero terminated Unicode string containing the archive name or NULL if Unicode name is not specified.

                      1 Reply Last reply
                      0
                      • V Offline
                        V Offline
                        VRonin
                        wrote on 17 Jul 2018, 07:37 last edited by
                        #11

                        Who has the responsibility of managing memory allocation of those struct members?

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        1 Reply Last reply
                        1
                        • M Offline
                          M Offline
                          Mr Gisa
                          wrote on 17 Jul 2018, 07:45 last edited by
                          #12

                          0_1531813356323_2919dca9-6d7f-4076-94ce-b52c80d4630a-image.png
                          I couldn't find the docs online.

                          RAROpenArchiveEx pretty much.

                          You can download the dlls, examples and the docs on the official website: https://www.rarlab.com/rar/UnRARDLL.exe

                          1 Reply Last reply
                          0
                          • V VRonin
                            17 Jul 2018, 07:11

                            A function that takes non-const wchar_t* is very dodgy! it should have no way of knowing the maximum allocated memory to that pointer/array so it is either allocating it internally (hence our discussion has been pointless so far) or that function is terribly designed and it's just a ticking time bomb.
                            Can I ask what that function does?

                            V Offline
                            V Offline
                            VRonin
                            wrote on 17 Jul 2018, 08:06 last edited by
                            #13

                            Ok, got it now, looks like it is used-as-const so the const_cast<wchar_t *>(testWstring.c_str()) should work. The string you see in the debugger might just be because you are trying to read the variable past its lifetime, I wouldn't worry about it

                            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                            ~Napoleon Bonaparte

                            On a crusade to banish setIndexWidget() from the holy land of Qt

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              Mr Gisa
                              wrote on 17 Jul 2018, 08:11 last edited by Mr Gisa
                              #14

                              @VRonin It's not working. After calling RAROpenArchiveEx it writes to RAROpenArchiveDataEx.OpenResult and the result should be ERAR_SUCCESS but it's returning ERAR_EOPEN and isn't right. For some reason it's only working the way I did before:

                                  // MAX_FILENAME_SIZE = 2048
                                  wchar_t filenameW[MAX_FILENAME_SIZE];
                                  int length = filename.left(MAX_FILENAME_SIZE - 1).toWCharArray(filenameW);
                                  filenameW[length] = '\0';
                              
                              JonBJ 1 Reply Last reply 17 Jul 2018, 08:27
                              0
                              • M Mr Gisa
                                17 Jul 2018, 08:11

                                @VRonin It's not working. After calling RAROpenArchiveEx it writes to RAROpenArchiveDataEx.OpenResult and the result should be ERAR_SUCCESS but it's returning ERAR_EOPEN and isn't right. For some reason it's only working the way I did before:

                                    // MAX_FILENAME_SIZE = 2048
                                    wchar_t filenameW[MAX_FILENAME_SIZE];
                                    int length = filename.left(MAX_FILENAME_SIZE - 1).toWCharArray(filenameW);
                                    filenameW[length] = '\0';
                                
                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on 17 Jul 2018, 08:27 last edited by
                                #15

                                @Mr-Gisa said in Convert QString to wchar_t*:

                                ERAR_EOPEN

                                I don't know what state your code is in. But ERAR_EOPEN is some kind of "file is already open" error on a RAR archive, and it hardly seems likely that could be generated as a direct result of something about converting strings, does it to you?

                                M 1 Reply Last reply 17 Jul 2018, 08:41
                                1
                                • JonBJ JonB
                                  17 Jul 2018, 08:27

                                  @Mr-Gisa said in Convert QString to wchar_t*:

                                  ERAR_EOPEN

                                  I don't know what state your code is in. But ERAR_EOPEN is some kind of "file is already open" error on a RAR archive, and it hardly seems likely that could be generated as a direct result of something about converting strings, does it to you?

                                  M Offline
                                  M Offline
                                  Mr Gisa
                                  wrote on 17 Jul 2018, 08:41 last edited by
                                  #16

                                  @JonB The code I posted worked because it's converting the path for the rar file correctly, altough when I try the alternatives here I get a weird string and not a correct path/rar file.

                                  V 1 Reply Last reply 17 Jul 2018, 08:48
                                  0
                                  • M Mr Gisa
                                    17 Jul 2018, 08:41

                                    @JonB The code I posted worked because it's converting the path for the rar file correctly, altough when I try the alternatives here I get a weird string and not a correct path/rar file.

                                    V Offline
                                    V Offline
                                    VRonin
                                    wrote on 17 Jul 2018, 08:48 last edited by
                                    #17

                                    @Mr-Gisa said in Convert QString to wchar_t*:

                                    I get a weird string

                                    I suspect you are holding a pointer to a temporary variable. If you are doing something like this, you are doing it wrong:

                                    void fillName(RAROpenArchiveDataEx* rar, const QString& fileName){
                                    rar->ArcNameW = const_cast<wchar_t *>(fileName.toStdWString().c_str());
                                    }
                                    

                                    As the temporary std::wstring will be destroyed and rar->ArcNameW will contain garbage

                                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                    ~Napoleon Bonaparte

                                    On a crusade to banish setIndexWidget() from the holy land of Qt

                                    1 Reply Last reply
                                    0
                                    • M Offline
                                      M Offline
                                      Mr Gisa
                                      wrote on 17 Jul 2018, 08:53 last edited by
                                      #18
                                      bool RAR::open(RAR::OpenMode mode)
                                      {
                                          RAROpenArchiveDataEx openArchiveData;
                                          memset(&openArchiveData, 0, sizeof(openArchiveData));
                                      
                                          openArchiveData.ArcName = 0;
                                      
                                          auto archive = const_cast<wchar_t *>(mArchive.toStdWString().c_str());
                                      
                                          openArchiveData.ArcNameW = archive;
                                          openArchiveData.OpenMode = mode;
                                          openArchiveData.CmtBuf = 0;
                                          openArchiveData.CmtBufSize = 0;
                                          openArchiveData.Callback = 0;
                                          openArchiveData.UserData = 0;
                                      
                                          mHandle = RAROpenArchiveEx(&openArchiveData);
                                          mResult = openArchiveData.OpenResult;
                                      
                                          if (mResult != ERAR_SUCCESS) {
                                              return false;
                                          }
                                      
                                          scan();
                                      
                                          return true;
                                      }
                                      
                                      V 1 Reply Last reply 17 Jul 2018, 09:04
                                      0
                                      • M Mr Gisa
                                        17 Jul 2018, 08:53
                                        bool RAR::open(RAR::OpenMode mode)
                                        {
                                            RAROpenArchiveDataEx openArchiveData;
                                            memset(&openArchiveData, 0, sizeof(openArchiveData));
                                        
                                            openArchiveData.ArcName = 0;
                                        
                                            auto archive = const_cast<wchar_t *>(mArchive.toStdWString().c_str());
                                        
                                            openArchiveData.ArcNameW = archive;
                                            openArchiveData.OpenMode = mode;
                                            openArchiveData.CmtBuf = 0;
                                            openArchiveData.CmtBufSize = 0;
                                            openArchiveData.Callback = 0;
                                            openArchiveData.UserData = 0;
                                        
                                            mHandle = RAROpenArchiveEx(&openArchiveData);
                                            mResult = openArchiveData.OpenResult;
                                        
                                            if (mResult != ERAR_SUCCESS) {
                                                return false;
                                            }
                                        
                                            scan();
                                        
                                            return true;
                                        }
                                        
                                        V Offline
                                        V Offline
                                        VRonin
                                        wrote on 17 Jul 2018, 09:04 last edited by VRonin
                                        #19

                                        @Mr-Gisa said in Convert QString to wchar_t*:

                                        auto archive = const_cast<wchar_t *>(mArchive.toStdWString().c_str());

                                        Boom! exactly as I supected!

                                        auto archiveWString = mArchive.toStdWString();
                                        auto archive = const_cast<wchar_t *>(archiveWString.c_str());
                                        

                                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                        ~Napoleon Bonaparte

                                        On a crusade to banish setIndexWidget() from the holy land of Qt

                                        1 Reply Last reply
                                        3
                                        • M Offline
                                          M Offline
                                          Mr Gisa
                                          wrote on 17 Jul 2018, 09:19 last edited by
                                          #20

                                          @VRonin said in Convert QString to wchar_t*:

                                          auto archiveWString = mArchive.toStdWString();
                                          auto archive = const_cast<wchar_t *>(archiveWString.c_str());

                                          Yay now its working. Thx.

                                          1 Reply Last reply
                                          0

                                          3/20

                                          14 Jul 2018, 07:28

                                          topic:navigator.unread, 17
                                          • Login

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