优化qt自定义事件逻辑

This commit is contained in:
rankun 2018-11-06 11:44:49 +08:00
commit a431906ecd
3 changed files with 23 additions and 9 deletions

View file

@ -50,7 +50,8 @@ HEADERS += \
qyuvopenglwidget.h \ qyuvopenglwidget.h \
videoform.h \ videoform.h \
devicesocket.h \ devicesocket.h \
tcpserver.h tcpserver.h \
qscrcpyevent.h
FORMS += \ FORMS += \
dialog.ui \ dialog.ui \

View file

@ -1,15 +1,9 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QDebug> #include <QDebug>
#include "qscrcpyevent.h"
#include "devicesocket.h" #include "devicesocket.h"
static const int GetDataEvent = QEvent::registerEventType(QEvent::User+1);
class DeviceSocketEvent : public QEvent
{
public:
DeviceSocketEvent() : QEvent(QEvent::Type(GetDataEvent)){}
};
DeviceSocket::DeviceSocket(QObject *parent) : QTcpSocket(parent) DeviceSocket::DeviceSocket(QObject *parent) : QTcpSocket(parent)
{ {
connect(this, &DeviceSocket::readyRead, this, &DeviceSocket::onReadyRead); connect(this, &DeviceSocket::readyRead, this, &DeviceSocket::onReadyRead);
@ -47,7 +41,7 @@ qint32 DeviceSocket::recvData(quint8 *buf, qint32 bufSize)
bool DeviceSocket::eventFilter(QObject *watched, QEvent *event) bool DeviceSocket::eventFilter(QObject *watched, QEvent *event)
{ {
if (event->type() == GetDataEvent) { if (event->type() == QScrcpyEvent::DeviceSocket) {
onReadyRead(); onReadyRead();
return true; return true;
} }

19
src/qscrcpyevent.h Normal file
View file

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