优化qt自定义事件处理逻辑

This commit is contained in:
rankun 2018-11-06 12:15:16 +08:00
parent a431906ecd
commit c8f8101f72
3 changed files with 13 additions and 6 deletions

View file

@ -9,8 +9,6 @@ DeviceSocket::DeviceSocket(QObject *parent) : QTcpSocket(parent)
connect(this, &DeviceSocket::readyRead, this, &DeviceSocket::onReadyRead);
connect(this, &DeviceSocket::aboutToClose, this, &DeviceSocket::quitNotify);
connect(this, &DeviceSocket::disconnected, this, &DeviceSocket::quitNotify);
installEventFilter(this);
}
DeviceSocket::~DeviceSocket()
@ -39,13 +37,13 @@ qint32 DeviceSocket::recvData(quint8 *buf, qint32 bufSize)
return m_dataSize;
}
bool DeviceSocket::eventFilter(QObject *watched, QEvent *event)
bool DeviceSocket::event(QEvent *event)
{
if (event->type() == QScrcpyEvent::DeviceSocket) {
onReadyRead();
return true;
}
return false;
return QTcpSocket::event(event);
}
void DeviceSocket::onReadyRead()

View file

@ -16,7 +16,7 @@ public:
qint32 recvData(quint8* buf, qint32 bufSize);
protected:
bool eventFilter(QObject *watched, QEvent *event);
bool event(QEvent *event);
protected slots:
void onReadyRead();

View file

@ -1,19 +1,28 @@
#ifndef QSCRCPYEVENT_H
#define QSCRCPYEVENT_H
#include <QEvent>
#include <QMouseEvent>
class QScrcpyEvent : public QEvent
{
public:
enum Type {
DeviceSocket = QEvent::User + 1,
Control,
};
QScrcpyEvent(Type type) : QEvent(QEvent::Type(type)){}
};
// DeviceSocketEvent
class DeviceSocketEvent : public QScrcpyEvent
{
public:
DeviceSocketEvent() : QScrcpyEvent(DeviceSocket){}
};
// ControlEvent
class ControlEvent : public QScrcpyEvent
{
public:
ControlEvent() : QScrcpyEvent(Control){}
};
#endif // QSCRCPYEVENT_H