PDA

View Full Version : Getting std::string object from QString object ( qt3)



joseph
14th August 2007, 12:53
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..



#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[])
{
QByteArray byteArray;
QFile f ( "m1_enc.ct" ); //binary file encoded using CBC Mode.
//QFile f ( "m1.ct" ); //Text file
//QFile f ( "Feb.ct"); //Text file

if ( f.open ( QIODevice::ReadOnly ) )
{
// file opened successfully
byteArray = f.readAll();
}
else
{
// problems opening the file - emit a warning message
QMessageBox::warning( 0, QObject::tr("error"),
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.

wysota
14th August 2007, 13:43
QString qstr = "xxx";
std::string str = qstr;
This should work fine.

If not, you can do:

QString qstr = "xxx";
std::string str(qstr.ascii());

Yes
20th March 2013, 15:37
QString qstr = "xxx";
std::string str = qstr;
This should work fine.

If not, you can do:

QString qstr = "xxx";
std::string str(qstr.ascii());

When I try


QString qstr = "xxx";
std::string str = qstr;

it works fine. But when I try


QString qstr = "xxx";
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?

wysota
20th March 2013, 18:33
You did notice this thread is about Qt3, right?

Yes
22nd March 2013, 12:51
Yes I noticed that. I have this problem when I'm using Qt3.

anda_skoa
22nd March 2013, 17:00
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,
_

Yes
22nd March 2013, 19:07
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?

wysota
23rd March 2013, 15:17
Explicitly cast QString to std::string:


std::string str;
QString qstr;
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.

Yes
26th March 2013, 10:55
std::string str;
QString qstr;
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?

wysota
26th March 2013, 11:01
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.

Yes
27th March 2013, 11:24
Just like it it possible to write



QString qstr = "xxx";
std::string str(qstr.ascii());


it is also possible to write




QString qstr = "xxx";
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



QString qstr = "xxx";
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?

anda_skoa
28th March 2013, 21:09
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,
_