Results 1 to 5 of 5

Thread: Cast QString array to std::string array

  1. #1
    Join Date
    Apr 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Cast QString array to std::string array

    Greetings all

    I have a QString array stored on the heap and I would like to cast it into a std::string array and then pass it into a method within a different class.

    I have tried the standard way:

    Qt Code:
    1. QString * arrayPointer;
    2. arrayPointer = new QString [dynamicSize];
    3.  
    4. ExternalClass test;
    5. test.externalMethod(arrayPointer.toStdString());
    To copy to clipboard, switch view to plain text mode 

    This causes the compiler to chock. How do I correctly cast a QString pointer to a std::string pointer?

    Many thanks for your help.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Cast QString array to std::string array

    I don't see any other way, other than copying all the strings

    Qt Code:
    1. QString * arrayPointer = new QString[dynamicSize];
    2.  
    3. std::string * arrayPtr = new std::string[dynamicSize];
    4.  
    5. for(int i = 0; i < arrayPointer->size(); i++)
    6. *(arrayPtr[i]) = arrayPointer->toStdString();
    7.  
    8. ExternalClass test;
    9. test.externalMethod(arrayPtr);
    10.  
    11. delete[] arrayPtr;
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Cast QString array to std::string array

    As far as I know, the pointer conversion is not possible. You need to create a local string with toStdString and pass its address to the method... and perhaps convert it back after the call

    Qt Code:
    1. QString qstr;
    2. QString * arrayPointer;
    3. arrayPointer = new QString [dynamicSize];
    4. std::string str = arrayPointer->toStdString();
    5. ExternalClass test;
    6. test.externalMethod(&str);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Apr 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Cast QString array to std::string array

    Thank you both.

    I kinda expected that and have already done the workaround. I was just wondering if there was some obscure Qt wizardry which could have done it.

    Again, thank you.

    Regards
    Nikki

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Cast QString array to std::string array

    Are you sure to want a pointer to a collection of QStrings or std::string? This is unusual: you would normally use one of the container classes to manage the memory nightmare for you. It is doubly unusual because externalMethod() has no way of knowing how large the array of std::string you pass is.

    Have you considered using std::vector<std::string> or QVector/QList?

Similar Threads

  1. How to put each word of a string into an array
    By nagabathula in forum Qt Programming
    Replies: 8
    Last Post: 12th May 2011, 01:47
  2. Replies: 2
    Last Post: 12th November 2010, 15:42
  3. declare an array of QSemaphore and array of slot functions
    By radeberger in forum Qt Programming
    Replies: 11
    Last Post: 2nd May 2010, 14:24
  4. Conversion Char Array to string
    By anafor2004 in forum Newbie
    Replies: 6
    Last Post: 6th May 2008, 15:35
  5. String Array
    By Sarma in forum Qt Programming
    Replies: 1
    Last Post: 13th May 2006, 08:53

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.