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. const wchar_t* and QString Problem
Qt 6.11 is out! See what's new in the release blog

const wchar_t* and QString Problem

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 651 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.
  • P Offline
    P Offline
    pixbyte
    wrote on last edited by VRonin
    #1

    I use an external DLL where I have to pass a parameter file.lpszSourceFilePath (const wchar_t*). If I use
    file.lpszSourceFilePath = L"C:\Users\ogni\Documents\video\early.ac3";
    then everything works well.
    But if I use a QString (from Filedialog) the parameter I pass seems to be wrong. Looks like a complete filename (_instead of /)
    I tried different way, also like to convert it with

    wchar_t* convertToFoxValue(QString strValue)
    {
    
        wchar_t *pass;
        pass = (wchar_t*) malloc (sizeof(wchar_t)*strValue.size()+1);
        //pass = new wchar_t[strValue.size() + 1];
        strValue.toWCharArray(pass);
        //pass[strValue.length()]=0; // Null terminate the string
        pass[strValue.size()]=L'\0';
    
        return pass;
    }
    

    But also this do not work. Cast the QString to utf16() also do not work. Just the plain text with L"....:" works, no variable.

    Anyone have a idea what there is wrong?

    aha_1980A 1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      Windows uses backslash (\) as separator. Qt uses the unix separator (/)
      Have a try with:
      [static] QString QDir::toNativeSeparators(const QString &pathName)

      1 Reply Last reply
      4
      • P pixbyte

        I use an external DLL where I have to pass a parameter file.lpszSourceFilePath (const wchar_t*). If I use
        file.lpszSourceFilePath = L"C:\Users\ogni\Documents\video\early.ac3";
        then everything works well.
        But if I use a QString (from Filedialog) the parameter I pass seems to be wrong. Looks like a complete filename (_instead of /)
        I tried different way, also like to convert it with

        wchar_t* convertToFoxValue(QString strValue)
        {
        
            wchar_t *pass;
            pass = (wchar_t*) malloc (sizeof(wchar_t)*strValue.size()+1);
            //pass = new wchar_t[strValue.size() + 1];
            strValue.toWCharArray(pass);
            //pass[strValue.length()]=0; // Null terminate the string
            pass[strValue.size()]=L'\0';
        
            return pass;
        }
        

        But also this do not work. Cast the QString to utf16() also do not work. Just the plain text with L"....:" works, no variable.

        Anyone have a idea what there is wrong?

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @pixbyte

        In addition to what mpergand suggested, if you only need a read-only access to the path (like const wchar_t * suggests), you can use QString::utf16() and a cast to wchar_t and therefore avoid the additional allocation and copy. Please note, that this is a Windows-only thing, as wchar_t can be 4 byte on Unix.

        Regards

        Qt has to stay free or it will die.

        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