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..
Code:
#include <QMessageBox>
#include <QIODevice>
#include <QFile>
#include <QByteArray>
#include <QString>
#include <QtDebug>
//FOR standard C++ libraries.
#include <ios>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
QFile f
( "m1_enc.ct" );
//binary file encoded using CBC Mode. //QFile f ( "m1.ct" ); //Text file
//QFile f ( "Feb.ct"); //Text file
{
// file opened successfully
byteArray = f.readAll();
}
else
{
// problems opening the file - emit a warning message
QObject::tr("Error opening file %1") ) ;
return -1;
}
f.close();
Q_ASSERT( ! byteArray.isEmpty() );
//CASE_1
qDebug
() <<
"QString from Binary_Data( QByteArray ) \n\n" <<
QString(byteArray
) ;
//CASE_2
qDebug
() <<
"Binary_Data(QByteArray) from QString \n\n" <<
QString(byteArray
).
toLatin1();
//CASE_3
string str
= QString(byteArray
).
toStdString() ;
// string strFromQString( str );
qDebug() << "String from QString \n\n" << str.data();
return 0;
}
thanks in advance.
Re: Getting std::string object from QString object ( qt3)
Code:
std::string str = qstr;
This should work fine.
If not, you can do:
Code:
std::string str(qstr.ascii());
Re: Getting std::string object from QString object ( qt3)
Quote:
Originally Posted by
wysota
Code:
std::string str = qstr;
This should work fine.
If not, you can do:
Code:
std::string str(qstr.ascii());
When I try
Code:
std::string str = qstr;
it works fine. But when I try
Code:
std::string str;
str = qstr;
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?
Re: Getting std::string object from QString object ( qt3)
You did notice this thread is about Qt3, right?
Re: Getting std::string object from QString object ( qt3)
Yes I noticed that. I have this problem when I'm using Qt3.
Re: Getting std::string object from QString object ( qt3)
Quote:
Originally Posted by
Yes
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,
_
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?
Re: Getting std::string object from QString object ( qt3)
Explicitly cast QString to std::string:
Code:
std::string str;
str = (std::string)qstr;
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.
Re: Getting std::string object from QString object ( qt3)
Quote:
Originally Posted by
wysota
Code:
std::string str;
str = (std::string)qstr;
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?
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.
Re: Getting std::string object from QString object ( qt3)
Just like it it possible to write
Code:
std::string str(qstr.ascii());
it is also possible to write
Code:
std::string str(qstr.latin1());
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
Code:
std::string str = qstr;
? 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?
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,
_