Results 1 to 12 of 12

Thread: Getting std::string object from QString object ( qt3)

  1. #1
    Join Date
    Feb 2006
    Posts
    157
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Getting std::string object from QString object ( qt3)

    hi ,

    My problem is , How to get std::string object from a QString in Qt 3

    What i did :-
    1) I have a binary/text file.
    2) I stored the data in QByteArray
    3) I created a QString from QByteArray

    What am trying to achieve :-
    1) I am trying to create a std::string object( for seding the object to an ENCRPTIONFUNCTION ) from QString
    2) In Qt 4.2.2 am able to get a std::string from QString.
    But how can i make it work in Qt3.

    See the code below ( in Qt 4) . Please help me to work the same in Qt3..

    Qt Code:
    1. #include <QMessageBox>
    2. #include <QIODevice>
    3. #include <QFile>
    4. #include <QByteArray>
    5. #include <QString>
    6. #include <QtDebug>
    7.  
    8.  
    9. //FOR standard C++ libraries.
    10. #include <ios>
    11. #include <iostream>
    12. #include <string>
    13. using namespace std;
    14.  
    15. int main(int argc, char *argv[])
    16. {
    17. QByteArray byteArray;
    18. QFile f ( "m1_enc.ct" ); //binary file encoded using CBC Mode.
    19. //QFile f ( "m1.ct" ); //Text file
    20. //QFile f ( "Feb.ct"); //Text file
    21.  
    22. if ( f.open ( QIODevice::ReadOnly ) )
    23. {
    24. // file opened successfully
    25. byteArray = f.readAll();
    26. }
    27. else
    28. {
    29. // problems opening the file - emit a warning message
    30. QMessageBox::warning( 0, QObject::tr("error"),
    31. QObject::tr("Error opening file %1") ) ;
    32. return -1;
    33. }
    34. f.close();
    35. Q_ASSERT( ! byteArray.isEmpty() );
    36.  
    37. //CASE_1
    38. qDebug() << "QString from Binary_Data( QByteArray ) \n\n" << QString(byteArray) ;
    39.  
    40. //CASE_2
    41. qDebug() << "Binary_Data(QByteArray) from QString \n\n" << QString(byteArray).toLatin1();
    42.  
    43.  
    44. //CASE_3
    45. string str = QString(byteArray).toStdString() ;
    46.  
    47. // string strFromQString( str );
    48. qDebug() << "String from QString \n\n" << str.data();
    49.  
    50.  
    51. return 0;
    52.  
    53. }
    To copy to clipboard, switch view to plain text mode 

    thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Getting std::string object from QString object ( qt3)

    Qt Code:
    1. QString qstr = "xxx";
    2. std::string str = qstr;
    To copy to clipboard, switch view to plain text mode 
    This should work fine.

    If not, you can do:
    Qt Code:
    1. QString qstr = "xxx";
    2. std::string str(qstr.ascii());
    To copy to clipboard, switch view to plain text mode 

  3. The following 2 users say thank you to wysota for this useful post:

    joseph (14th August 2007), Yes (23rd March 2013)

  4. #3
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Getting std::string object from QString object ( qt3)

    Quote Originally Posted by wysota View Post
    Qt Code:
    1. QString qstr = "xxx";
    2. std::string str = qstr;
    To copy to clipboard, switch view to plain text mode 
    This should work fine.

    If not, you can do:
    Qt Code:
    1. QString qstr = "xxx";
    2. std::string str(qstr.ascii());
    To copy to clipboard, switch view to plain text mode 
    When I try

    Qt Code:
    1. QString qstr = "xxx";
    2. std::string str = qstr;
    To copy to clipboard, switch view to plain text mode 

    it works fine. But when I try

    Qt Code:
    1. QString qstr = "xxx";
    2. std::string str;
    3. str = qstr;
    To copy to clipboard, switch view to plain text mode 

    I get error: ambiguous overload for 'operator=' in 'str = qstr'. Why is that? Is conversion between QString and std::string well defined while assignment of a QString on an already created std::string becomes ambiguous?

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Getting std::string object from QString object ( qt3)

    You did notice this thread is about Qt3, right?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Getting std::string object from QString object ( qt3)

    Yes I noticed that. I have this problem when I'm using Qt3.

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Getting std::string object from QString object ( qt3)

    Quote Originally Posted by Yes View Post
    Why is that? Is conversion between QString and std::string well defined while assignment of a QString on an already created std::string becomes ambiguous?
    The first "assignment" is a call to a constructor of std::string, the second assignment is an invocation of an assignment operator. There might be more overloads for the second case and the compiler can't decide which one it should use.

    Cheers,
    _

  8. #7
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Getting std::string object from QString object ( qt3)

    How can I tell it which one to use, in that case?

    It indeed seems like there are more than one overload in the second case, of which the compiler can't decide which one to use, even if I don't really understand how that is possible. Can't there be only one assignment operator that takes a std::string on the left hand side and an QString on the right hand side?

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Getting std::string object from QString object ( qt3)

    Explicitly cast QString to std::string:

    Qt Code:
    1. std::string str;
    2. QString qstr;
    3. str = (std::string)qstr;
    To copy to clipboard, switch view to plain text mode 

    An implicit cast won't work because std::string can be assigned a const char* and QString also contains a cast to const char* so the compiler doesn't know which one to choose.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Yes (23rd March 2013)

  11. #9
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Getting std::string object from QString object ( qt3)

    Quote Originally Posted by wysota View Post
    Qt Code:
    1. std::string str;
    2. QString qstr;
    3. str = (std::string)qstr;
    To copy to clipboard, switch view to plain text mode 
    Doesn't this solution mean two operations, first an explicit cast between a QString and an std::string, and then an assignment between two std::strings?

  12. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Getting std::string object from QString object ( qt3)

    The cast is there either way. It's just a matter of whether it is explicitly programmed in the sourcecode or implicitly injected by the compiler.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. The following user says thank you to wysota for this useful post:

    Yes (27th March 2013)

  14. #11
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Getting std::string object from QString object ( qt3)

    Just like it it possible to write

    Qt Code:
    1. QString qstr = "xxx";
    2. std::string str(qstr.ascii());
    To copy to clipboard, switch view to plain text mode 

    it is also possible to write


    Qt Code:
    1. QString qstr = "xxx";
    2. std::string str(qstr.latin1());
    To copy to clipboard, switch view to plain text mode 

    What is the practical difference between these two? How do you know which one of the you should use? And what happens if you choose none of them, but instead uses

    Qt Code:
    1. QString qstr = "xxx";
    2. std::string str = qstr;
    To copy to clipboard, switch view to plain text mode 

    ? Is either ascii() or latin1() implicitly used to cast the QString to an std::string (and in that case, which one of them), or does the cast happen in some completely different way?

  15. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Getting std::string object from QString object ( qt3)

    Theoretically ASCII refers to the 7bit ASCII character set, while latin1 is an 8bit encoding which also includes characters common in western european languages (also referred to as ISO8859-1).

    However, I think ascii() and latin1() actually use both the latin1() encoding since ASCII is a subset of latin1.

    The casting operator very likely does the same as latin1(), that got only changed in Qt5 as far as I know (where it does the same as toUtf8()).

    Cheers,
    _

  16. The following user says thank you to anda_skoa for this useful post:

    Yes (2nd April 2013)

Similar Threads

  1. std::string to QString?
    By Backslash in forum Newbie
    Replies: 4
    Last Post: 1st August 2007, 15:50
  2. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59

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.