Results 1 to 10 of 10

Thread: BSTR to QString Conversion

  1. #1
    Join Date
    May 2009
    Posts
    94
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Post BSTR to QString Conversion

    How can i convert BSTR to QString. Please Give a example.

  2. #2
    Join Date
    Mar 2010
    Location
    Capelle aan den IJssel, Netherlands
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: BSTR to QString Conversion

    This probably works:

    Qt Code:
    1. QString convertedBSTR((QChar*) bstr, wcslen(bstr));
    To copy to clipboard, switch view to plain text mode 

    Don't forget to free the BSTR, else you will get memory leaks!

  3. #3
    Join Date
    May 2009
    Posts
    94
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Post Re: BSTR to QString Conversion

    Quote Originally Posted by _Stefan View Post
    This probably works:

    Qt Code:
    1. QString convertedBSTR((QChar*) bstr, wcslen(bstr));
    To copy to clipboard, switch view to plain text mode 

    Don't forget to free the BSTR, else you will get memory leaks!
    This is not working give me some other solution.
    BSTR containing the starting base address of a string. I need to convert into QString.

  4. #4
    Join Date
    Mar 2010
    Location
    Capelle aan den IJssel, Netherlands
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: BSTR to QString Conversion

    Can you elaborate on the exact problem?

    Show some code, debug messages?

  5. #5
    Join Date
    May 2009
    Posts
    94
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Post Re: BSTR to QString Conversion

    Quote Originally Posted by _Stefan View Post
    Can you elaborate on the exact problem?

    Show some code, debug messages?
    BSTR codesetID;

    if(pGetID_Outron->GetCodesetID(&codesetID, &codesetLength) == S_OK)
    {
    }
    CodesetID containg the starting address.
    Now we need to get in QString, to which BSTR is pointing to.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: BSTR to QString Conversion

    But what happens when you use the suggested code?

  7. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: BSTR to QString Conversion

    Quote Originally Posted by _Stefan View Post
    This probably works:
    It is my understanding that this will not work as the first 4 bytes of a BSTR contain the length of the string, so you would have to compensate for this. Maybe QString convertedBSTR((QChar*) &bstr[4], wcslen(bstr)); ?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

  9. #9
    Join Date
    Nov 2011
    Posts
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: BSTR to QString Conversion

    Try:

    std::wstring intermediateString(myBSTR, SysStringLen(myBSTR);
    QString finalString = QString::fromStdWString(intermediateString);

    -K

  10. The following user says thank you to hvkleist for this useful post:

    bmn (16th February 2012)

  11. #10
    Join Date
    May 2007
    Posts
    131
    Thanks
    17
    Thanked 4 Times in 2 Posts

    Default Re: BSTR to QString Conversion

    No need to use std:
    QString qstr((QChar*)bstr, ::SysStringLen(bstr));

Similar Threads

  1. Char* <-> QString implicit conversion
    By kingfinn in forum Qt Programming
    Replies: 3
    Last Post: 15th March 2010, 08:26
  2. char* to QString: conversion
    By abghosh in forum Qt Programming
    Replies: 9
    Last Post: 8th March 2010, 09:32
  3. qreal -> QString conversion
    By MarkoSan in forum Newbie
    Replies: 2
    Last Post: 13th April 2008, 14:37
  4. QString iso 8859-1 conversion
    By mattia in forum Newbie
    Replies: 11
    Last Post: 21st January 2008, 14:17
  5. conversion from QString to const uint8 *
    By vishal.chauhan in forum Qt Programming
    Replies: 3
    Last Post: 19th February 2007, 13:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.