how to copy a QString value to a struct
-
wrote on 19 Feb 2019, 04:13 last edited by
how to copy the data stored in a QString variable to the below structure ?
typedef struct{
int iTestMode;
int iTestName;
char arrConfigurationData[500];
int iSizeOfData;
int iChecksum;
}TxCommandPacket; -
how to copy the data stored in a QString variable to the below structure ?
typedef struct{
int iTestMode;
int iTestName;
char arrConfigurationData[500];
int iSizeOfData;
int iChecksum;
}TxCommandPacket;@ManiRon How does your QString look like?
-
@aha_1980 its a variable in which i will store the values read from the data transmitted from server side
@ManiRon Yeah, but what are it's contents? Is it printable? Then how does it look?
-
it is contains a normal data which can be printed like for example(1,2,3) like these values it has
@ManiRon If you cannot give us a concrete full example then we cannot give you a concrete answer. Sorry
-
@ManiRon If you cannot give us a concrete full example then we cannot give you a concrete answer. Sorry
-
i am not understanding and how to explain it . its a simple QString variable and i store the read data in that variable and i want to copy that values to the struct which i have mentioned above .
wrote on 19 Feb 2019, 04:43 last edited byThis post is deleted! -
@ManiRon If you cannot give us a concrete full example then we cannot give you a concrete answer. Sorry
wrote on 19 Feb 2019, 04:47 last edited by VRoninQDataStream in(tcpSocket); //in.setVersion(QDataStream::Qt_5_10); for (;;) { if (!m_nNextBlockSize) { if (tcpSocket->bytesAvailable() < sizeof(quint16)) { break; } in >> m_nNextBlockSize; } if (tcpSocket->bytesAvailable() < m_nNextBlockSize) { break; } QString str; in >> str; if (str == "0") { str = "Connection closed"; closeConnection(); } emit hasReadSome(str); m_nNextBlockSize = 0; } }
and i want to copy the QString str contained values to the struct given above
-
i am not understanding and how to explain it . its a simple QString variable and i store the read data in that variable and i want to copy that values to the struct which i have mentioned above .
@ManiRon said in how to copy a QString value to a struct:
i am not understanding and how to explain it
Can't you simply show an example of such a string?
Also, why do you convert the byte array to a string first?
The way it usually works is: implement << and >> operators for your structure (for QDataStream, https://doc.qt.io/qt-5/qdatastream.html). Then you can simply do it like:TxCommandPacket packet; in >> packet;
-
QDataStream in(tcpSocket); //in.setVersion(QDataStream::Qt_5_10); for (;;) { if (!m_nNextBlockSize) { if (tcpSocket->bytesAvailable() < sizeof(quint16)) { break; } in >> m_nNextBlockSize; } if (tcpSocket->bytesAvailable() < m_nNextBlockSize) { break; } QString str; in >> str; if (str == "0") { str = "Connection closed"; closeConnection(); } emit hasReadSome(str); m_nNextBlockSize = 0; } }
and i want to copy the QString str contained values to the struct given above
@ManiRon this only works, because QString haccepts a QByteArray as a Constructor/DataStream overload.
Are you really sure you want to save the data in a QString and not in a QByteArray ?
a QString will terminate if the QByteArray contains char it can not represent.That said, there are multiple ways to skin this cat.
-
give your struct a QDataStream operator laid @jsulm said.
-
copy the raw data form the string or QbyteArray into the allocated char array
struct Foo{ int a; int b; char data [500]; }; QString s ("Hello World"); Foo bar; memcpy(&bar.data, s.data(),s.size()*2);
- or if you wan't to go really old school:
QString s ("Hello World"); Foo bar; memcpy(reinterpret_cast<char*>(&bar) + sizeof(int)*2, s.data(),s.size()*2);
just the 3 way's I'm thinking of right now.
-
-
@ManiRon this only works, because QString haccepts a QByteArray as a Constructor/DataStream overload.
Are you really sure you want to save the data in a QString and not in a QByteArray ?
a QString will terminate if the QByteArray contains char it can not represent.That said, there are multiple ways to skin this cat.
-
give your struct a QDataStream operator laid @jsulm said.
-
copy the raw data form the string or QbyteArray into the allocated char array
struct Foo{ int a; int b; char data [500]; }; QString s ("Hello World"); Foo bar; memcpy(&bar.data, s.data(),s.size()*2);
- or if you wan't to go really old school:
QString s ("Hello World"); Foo bar; memcpy(reinterpret_cast<char*>(&bar) + sizeof(int)*2, s.data(),s.size()*2);
just the 3 way's I'm thinking of right now.
@J.Hilk you are aware that QChar is 16 bit, so QString::size() is only half the memory it uses...
Anyway, the OP don't want QString, rather QByteArray.
-
-
@J.Hilk you are aware that QChar is 16 bit, so QString::size() is only half the memory it uses...
Anyway, the OP don't want QString, rather QByteArray.
@aha_1980 said in how to copy a QString value to a struct:
@J.Hilk you are aware that QChar is 16 bit, so QString::size() is only half the memory it uses...
thanks for clarifying that I was looking through the documentation, scratching my head, why my example project only copied "Hello " back!
I'll fix my post!
-
@aha_1980 said in how to copy a QString value to a struct:
@J.Hilk you are aware that QChar is 16 bit, so QString::size() is only half the memory it uses...
thanks for clarifying that I was looking through the documentation, scratching my head, why my example project only copied "Hello " back!
I'll fix my post!
wrote on 19 Feb 2019, 11:54 last edited byThis post is deleted!
1/14