There is no possible conversion from a complex type (in this case QString) to a fundamental type (in this case char), your question is similar to " How do you turn a Car into a Piston ? ".
While it is true that Cars have engines and engines have Pistons, a Piston is a part of the Car.
You can't turn a Car into a Piston although you will use Pistons to build your Car... parallelly you can't turn a QStringinto a charalthough you will use charas a part of a QString.
So I guess you actually meant to ask:
How do I access those charof a QString?
So I'm going to base my answer on that assumption.
Clarification.
QStringit is created as a class analogous to the string class ; so it has similar, equivalent or exactly the same methods.
Suggestion.
Use the indexing operator (access via square brackets [and ]). Given the almost complete equivalence between QStringand std::stringboth objects have this operator and it is used in the same way.
Well, assuming you're using Qt 5.x, it's relatively simple.
To convert one to one (or some) of the characters in a QStringyou would have to use something like the following code:
QString ejemplo="ejemplo";
std::cout << ejemplo.at(0).toLatin1(); //funcion at devuelve el tipo de dato Qchar de la cadena en la posicion 0
//y .toLatin1() devuelve el caracter en dato char.
Instead if you want to convert the whole QString, a faster option is to use .toLatin1()on the object QString.
QString ejemplo2="ejemplo2";
ejemplo2.toLatin1(); //esta funcion devuelve un QByteArray
A QByteArrayis roughly a class that serves as a dynamic array char(you can concatenate, add, remove, etc.), without the complexity of dynamic memory.
In addition, it must be said that QByteArrayyou can obtain the data as a type charif you use the dataor functions constData.
QByteArray ejemploByteArray=ejemplo2.toLatin1();
ejemploByteArray.constData(); //devuelve un puntero constante a un data *char, por ejemplo si solo es un parametro de lectura
ejemploByteArray.data(); //devuelve un puntero *char, este si modificable, por lo que si lo manipulas cambiaras tambien los datos de QByteArray, por lo que aqui si hay que tener cuidado.
QString
has the methodtoStdString()
that returns astd::string
. Getting achar
from this object is trivial:All the best.
Ask.
There is no possible conversion from a complex type (in this case
QString
) to a fundamental type (in this casechar
), your question is similar to " How do you turn a Car into a Piston ? ".While it is true that Cars have engines and engines have Pistons, a Piston is a part of the Car.
You can't turn a Car into a Piston although you will use Pistons to build your Car... parallelly you can't turn a
QString
into achar
although you will usechar
as a part of aQString
.So I guess you actually meant to ask:
So I'm going to base my answer on that assumption.
Clarification.
QString
it is created as a class analogous to the string class ; so it has similar, equivalent or exactly the same methods.Suggestion.
Use the indexing operator (access via square brackets
[
and]
). Given the almost complete equivalence betweenQString
andstd::string
both objects have this operator and it is used in the same way.QString::operator []
.std::string::operator []
.So, in the following example, they both show
@
using the bracket operator on the fifth position:Well, assuming you're using Qt 5.x, it's relatively simple.
To convert one to one (or some) of the characters in a
QString
you would have to use something like the following code:Instead if you want to convert the whole
QString
, a faster option is to use.toLatin1()
on the objectQString
.A
QByteArray
is roughly a class that serves as a dynamic arraychar
(you can concatenate, add, remove, etc.), without the complexity of dynamic memory.In addition, it must be said that
QByteArray
you can obtain the data as a typechar
if you use thedata
or functionsconstData
.