View Full Version : Struct in network
sribalaji
24th March 2008, 07:08
Hi ,
Can anyone please tell me how to pass a struct thru a QTcpSocket!
the struct is like this for eg
struct sname
{
qint16 q;
QString a;
QString b;
}
the tcpsocket->write method accepts only qbytearray as the data to send. so can u please tell me how to convert a struct into a qbytearray or
is there anyother method to send this data to the server.
i use QT4.2.2 in linux kernel version 2.6
--
thnks ..
jpn
24th March 2008, 10:44
See QDataStream and Using custom data types with Qt.
sribalaji
25th March 2008, 05:48
will the use of QVariant help.
i tried using the following
#include <QtGui>
#include <QMetaType>
struct cin
{
char *ue;
char *pd;
};
Q_DECLARE_METATYPE(cin);
QVariant b;
struct cin ud;
ud.usrname=lineedit->text().toAscii().data();
ud.usrname=lineedit1->text().toAscii().data();
b.setValue(ud);
cin ud1 = b.value<cin>();
statuslabel->setText(ud1.ue);
but i am getting the following error.
error: expected primary-expression before "template"
error: expected `;' before "template"
error: no matching function for call to `QVariant::setValue(dslgimpl::senddata()::cin&)'
error: no matching function for call to `QVariant::value()'
can anyone help me in finding the problem.
--
Thanks..
jpn
25th March 2008, 07:34
First of all, QVariant::setValue() is a template function. Secondly, you MUST NOT do this:
ud.usrname=lineedit->text().toAscii().data();
ud.usrname=lineedit1->text().toAscii().data();
You are storing pointers to internal data of temporary objects. They become invalid as soon as those temporary QByteArray objects returned by QString::toAscii() go out of scope. This is immediately after the corresponding instruction.
sribalaji
25th March 2008, 08:58
thats a mistake its actually...
ud.ue=lineedit->text().toAscii().data();
ud.pd=lineedit1->text().toAscii().data();
jpn
25th March 2008, 11:23
thats a mistake its actually...
ud.ue=lineedit->text().toAscii().data();
ud.pd=lineedit1->text().toAscii().data();
But it doesn't change the fact that you are still storing a pointer to something that doesn't exist after that particular line has been executed.
sribalaji
26th March 2008, 08:49
but the value is getting stored.
jpn
26th March 2008, 10:38
It's a pointer to data which has been destroyed.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.