Results 1 to 7 of 7

Thread: QTcpSocket - Events

  1. #1
    Join Date
    Jun 2010
    Posts
    10
    Thanks
    3
    Qt products
    Qt4

    Question QTcpSocket - Events

    Hi,
    i´ve a strange problem. I try to implement some kind of permanent event for an object deriving from QTcpSocket. My first idea was to use a simple timer be called all x-msecs. The problem is that I can´t connect to slots defined in my subclass - I checked out the internet and found this http://stackoverflow.com/questions/1...of-derived-one. All the points of the solution are applied but it´s still not working - any idea why it´s not working?

    Furthermore I tried it with the timer of QObject-Baseclass - call startTimer() and implementing timerEvent(QEvent* event) - but the function is never called!??!

    Is there a reason for this behaviour - do I miss something?!?!?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QTcpSocket - Events

    Can you please post your code?
    There can be tons of possible errors and without seeing the code it's not easy to pin them down.

  3. #3
    Join Date
    Jun 2010
    Posts
    10
    Thanks
    3
    Qt products
    Qt4

    Default Re: QTcpSocket - Events

    Sure.... here a simple example.

    Qt Code:
    1. #ifndef MYTCPSOCKET_H
    2. #define MYTCPSOCKET_H
    3.  
    4. #include <QDebug>
    5. #include <QHostInfo>
    6. #include <QHostAddress>
    7. #include <QTcpSocket>
    8. #include "Definitions.h"
    9.  
    10. class myTCPSocket: public QTcpSocket
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. myTCPSocket(QObject* in_Parent = NULL);
    16. ~myTCPSocket(void);
    17.  
    18. // init the object (startTimer)
    19. void init()
    20. {
    21. startTimer();
    22. }
    23.  
    24. // reads a package
    25. bool readPackage(tNetworkTcpData* in_NetworkTcpData);
    26.  
    27. // writes a package
    28. bool writePackage(tNetworkTcpData in_NetworkTcpData);
    29.  
    30. // handle event
    31. void timerEvent(QTimerEvent *event)
    32. {
    33. qDebug()<< "timerEventFired";
    34. }
    35.  
    36. private:
    37. };
    38. #endif //MYTCPSOCKET_H
    To copy to clipboard, switch view to plain text mode 

    My Socket is initialized like that....

    myTCPSocket m_TcpSocket = static_cast<myTCPSocket*>(in_TcpSocket);
    m_TcpSocket->init();

    Thx for the help..

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QTcpSocket - Events

    Quoting the documentation:

    If interval is 0, then the timer event occurs once every time there are no more window system events to process.
    So, try setting a real interval and see what happens.

    Also:
    Note that QTimer's accuracy depends on the underlying operating system and hardware. Most platforms support an accuracy of 20 milliseconds; some provide more. If Qt is unable to deliver the requested number of timer events, it will silently discard some.
    It's not a good idea to use timers to receive or send data (at least I don't think so)

  5. The following user says thank you to tbscope for this useful post:

    Skorpien126 (5th July 2010)

  6. #5
    Join Date
    Jun 2010
    Posts
    10
    Thanks
    3
    Qt products
    Qt4

    Default Re: QTcpSocket - Events

    Nothing happens at all.... I set the value to 1000 and 5000 milliseconds.

    I know that the accuracy depends on the system - but this not the crititcal point in my case - I just want that the events is emitted when the process has to time to handle it. An update every 500msecs would be satisfying for me too.

  7. #6
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTcpSocket - Events

    Qt Code:
    1. myTCPSocket m_TcpSocket = static_cast<myTCPSocket*>(in_TcpSocket);
    To copy to clipboard, switch view to plain text mode 
    I don't think this works like you think it does. In particular, I think the vtable still points to the QTcpSocket vtable rather than your subclass vtable. In short, you don't want to do this.

  8. The following user says thank you to numbat for this useful post:

    Skorpien126 (5th July 2010)

  9. #7
    Join Date
    Jun 2010
    Posts
    10
    Thanks
    3
    Qt products
    Qt4

    Default Re: QTcpSocket - Events

    Ok... it´s obvisously whats wrong. Thx....
    P.S. Everyone with the same problem. I create a normal QTcpSocket now and connecting it. Afterwards I use the socketDescriptor to initialize my myTcpSocket-object. not sure if this the best way - but it works fine for me.

Similar Threads

  1. Need Help with QTcpSocket
    By jknotzke in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2009, 13:55
  2. QTcpSocket help
    By tsd-charlie in forum Newbie
    Replies: 1
    Last Post: 6th August 2009, 19:40
  3. Using QTcpSocket
    By mraittin in forum Qt Programming
    Replies: 1
    Last Post: 2nd September 2007, 09:54
  4. QTcpSocket and Qt 4.2.3
    By slcotter in forum Qt Programming
    Replies: 4
    Last Post: 9th May 2007, 16:14
  5. QT4.2.2 and QTcpSocket
    By Nyphel in forum Qt Programming
    Replies: 13
    Last Post: 11th March 2007, 11:30

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
  •  
Qt is a trademark of The Qt Company.