Results 1 to 8 of 8

Thread: Struct in network

  1. #1
    Join Date
    Mar 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Struct in network

    Hi ,
    Can anyone please tell me how to pass a struct thru a QTcpSocket!
    the struct is like this for eg
    Qt Code:
    1. struct sname
    2. {
    3. qint16 q;
    4. }
    To copy to clipboard, switch view to plain text mode 

    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 ..
    Last edited by jpn; 24th March 2008 at 10:43. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Struct in network

    See QDataStream and [WIKI]Using custom data types with Qt[/WIKI].
    J-P Nurmi

  3. #3
    Join Date
    Mar 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Post Re: Struct in network

    will the use of QVariant help.

    i tried using the following
    Qt Code:
    1. #include <QtGui>
    2. #include <QMetaType>
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. struct cin
    2. {
    3. char *ue;
    4. char *pd;
    5. };
    6. Q_DECLARE_METATYPE(cin);
    7. struct cin ud;
    8. ud.usrname=lineedit->text().toAscii().data();
    9. ud.usrname=lineedit1->text().toAscii().data();
    10. b.setValue(ud);
    11. cin ud1 = b.value<cin>();
    12. statuslabel->setText(ud1.ue);
    To copy to clipboard, switch view to plain text mode 


    but i am getting the following error.
    Qt Code:
    1. error: expected primary-expression before "template"
    2. error: expected `;' before "template"
    3. error: no matching function for call to `QVariant::setValue(dslgimpl::senddata()::cin&)'
    4. error: no matching function for call to `QVariant::value()'
    To copy to clipboard, switch view to plain text mode 
    can anyone help me in finding the problem.

    --
    Thanks..

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Struct in network

    First of all, QVariant::setValue() is a template function. Secondly, you MUST NOT do this:
    Qt Code:
    1. ud.usrname=lineedit->text().toAscii().data();
    2. ud.usrname=lineedit1->text().toAscii().data();
    To copy to clipboard, switch view to plain text mode 
    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.
    J-P Nurmi

  5. #5
    Join Date
    Mar 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Struct in network

    thats a mistake its actually...
    Qt Code:
    1. ud.ue=lineedit->text().toAscii().data();
    2. ud.pd=lineedit1->text().toAscii().data();
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Struct in network

    Quote Originally Posted by sribalaji View Post
    thats a mistake its actually...
    Qt Code:
    1. ud.ue=lineedit->text().toAscii().data();
    2. ud.pd=lineedit1->text().toAscii().data();
    To copy to clipboard, switch view to plain text mode 
    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.
    J-P Nurmi

  7. #7
    Join Date
    Mar 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Struct in network

    but the value is getting stored.

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Struct in network

    It's a pointer to data which has been destroyed.
    J-P Nurmi

Similar Threads

  1. Replies: 16
    Last Post: 23rd May 2008, 10:12
  2. Checking network availability
    By fullmetalcoder in forum Qt Programming
    Replies: 2
    Last Post: 10th March 2008, 19:23
  3. Make error with Qt Jambi 4.3.2_01
    By pamalite in forum Installation and Deployment
    Replies: 0
    Last Post: 22nd November 2007, 12:05
  4. QSocket - signal for network wire disconnection
    By manivannan_1984 in forum Qt Programming
    Replies: 7
    Last Post: 5th September 2006, 13:52
  5. memset struct with QString elements
    By Timewarp in forum Qt Programming
    Replies: 2
    Last Post: 5th February 2006, 01:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.