PDA

View Full Version : QTcpSocket - Events



Skorpien126
2nd July 2010, 16:12
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/1389680/why-is-qt-looking-for-my-slot-in-the-base-class-instead-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?!?!?

tbscope
2nd July 2010, 18:32
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.

Skorpien126
5th July 2010, 07:49
Sure.... here a simple example.



#ifndef MYTCPSOCKET_H
#define MYTCPSOCKET_H

#include <QDebug>
#include <QHostInfo>
#include <QHostAddress>
#include <QTcpSocket>
#include "Definitions.h"

class myTCPSocket: public QTcpSocket
{
Q_OBJECT

public:
myTCPSocket(QObject* in_Parent = NULL);
~myTCPSocket(void);

// init the object (startTimer)
void init()
{
startTimer();
}

// reads a package
bool readPackage(tNetworkTcpData* in_NetworkTcpData);

// writes a package
bool writePackage(tNetworkTcpData in_NetworkTcpData);

// handle event
void timerEvent(QTimerEvent *event)
{
qDebug()<< "timerEventFired";
}

private:
};
#endif //MYTCPSOCKET_H


My Socket is initialized like that....

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

Thx for the help..

tbscope
5th July 2010, 08:21
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)

Skorpien126
5th July 2010, 08:30
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.

numbat
5th July 2010, 09:52
myTCPSocket m_TcpSocket = static_cast<myTCPSocket*>(in_TcpSocket);

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.

Skorpien126
5th July 2010, 12:16
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. :)