The company recently asked to use Qt to write programs. The most depressing thing is the conversion between strings. Let's do it yourself! Fortunately, I am able to handle the problem... I also tried several built-in Qt conversion methods.
#include <QtGui/QApplication>#include "testqstring.h"#include "boost/lexical_cast.hpp"#include <iostream>#include <string>#include <stdio.h>#include <QDebug>using namespace std;int main(int argc, char *argv[]){ QApplication a(argc, argv); testQString w; w.show(); QString qStr="testString"; qDebug()<<"wyz-------1:"<<qStr; string str=qStr.toStdString(); cout<<"wyz-------2:"<<str<<endl; char *cStr; cStr=(char*)str.c_str(); cout<<"wyz-------3:"<<cStr<<endl; string str_1="testString2"; QString qStr_1; qStr_1.sprintf("%s",str_1.c_str()); qDebug()<<"wyz-------4:"<<qStr_1; return a.exec();}
Running result:
Starting/root/testQString-build-desktop/testQString...
Wyz ------- 1: "testString"
Wyz ------- 2: testString
Wyz ------- 3: testString
Wyz ------- 4: "testString2"
Convert int to QString
QString & sprintf (const char * cformat ,...)
String strData = write1.write (smsJson );
StrData = boost: erase_all_copy (strData, "\ n ");
String
StrTemp =
M_qStrInfo.toStdString ();
vector<string > splitVec;
boost::split(splitVec, strTemp, boost::is_any_of("\r\n"));
========= Copy the documents from the online copy ====================
// Common parameter types: char * string, QByteArray character array, and QString string
// Need to be converted: char * --- convert --- QByteArray --- the constructor of the QByteArray class needs to be called
Char * -------------- QByteArray (const char *) -----------> QByteArray
Char * str;
QByteArray byte (str );
Char * -------------- QString (const char *) ------------> QString
Char * str;
QString string (str );
QByteArray ------------- data () --------------------> char *
QByteArray byte;
Char * str = byte. data ();
QByteArray ------------ QString () -------------------> QString
QByteArray byte;
QString string (byte );
QString -------------- toAscii () ------------------> QByteArray
QString string;
QByteArray byte = string. toAscii ();
QString ------------- qPrintable () ------------------> char *
QString string;
Char * str = qPrintable (string );
QString s2q (const string & s)
{
Return QString (QString: fromLocal8Bit (s. c_str ()));
}
String q2s (const QString & s)
{
Return string (const char *) s. toLocal8Bit ());
}
QByteArray qba = m_pTcpSocket-> readAll ();
QString strings (qba); // convert to a qt string
QByteArray qba = tcpSocket-> readAll ();
Char * readStr = qba. data (); // convert QByteArray to char