From be766a304f65161c966f2a9efdb882ed5fda9ad1 Mon Sep 17 00:00:00 2001 From: feiyangqingyun Date: Sat, 30 Apr 2022 18:17:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- control/devicebutton/devicebutton.cpp | 40 +++++++++++++-------------- control/devicebutton/devicebutton.h | 4 +++ tool/netserver/api/tcpserver1.cpp | 6 ++-- tool/netserver/api/tcpserver2.cpp | 4 +-- tool/nettool/api/tcpclient.cpp | 2 +- tool/nettool/form/frmtcpclient.cpp | 2 +- tool/nettool/form/frmudpclient.cpp | 4 +-- tool/nettool/form/frmudpserver.cpp | 4 +-- tool/nettool/form/frmwebclient.cpp | 2 +- 9 files changed, 36 insertions(+), 32 deletions(-) diff --git a/control/devicebutton/devicebutton.cpp b/control/devicebutton/devicebutton.cpp index e981ee5..a79aa23 100644 --- a/control/devicebutton/devicebutton.cpp +++ b/control/devicebutton/devicebutton.cpp @@ -13,6 +13,9 @@ DeviceButton::DeviceButton(QWidget *parent) : QWidget(parent) buttonStyle = ButtonStyle_Police; buttonColor = ButtonColor_Green; + isPressed = false; + lastPoint = QPoint(); + type = "police"; imgName = QString(":/image/devicebutton/devicebutton_green_%1.png").arg(type); isDark = false; @@ -79,29 +82,26 @@ void DeviceButton::paintEvent(QPaintEvent *) bool DeviceButton::eventFilter(QObject *watched, QEvent *event) { - if (canMove) { - static QPoint lastPoint; - static bool isPressed = false; - - if (event->type() == QEvent::MouseButtonPress) { - QMouseEvent *e = static_cast(event); - if (this->rect().contains(e->pos()) && (e->button() == Qt::LeftButton)) { - lastPoint = e->pos(); - isPressed = true; - } - } else if (event->type() == QEvent::MouseMove && isPressed) { - QMouseEvent *e = static_cast(event); - int dx = e->pos().x() - lastPoint.x(); - int dy = e->pos().y() - lastPoint.y(); + //识别鼠标 按下+移动+松开+双击 等事件 + QMouseEvent *mouseEvent = static_cast(event); + if (event->type() == QEvent::MouseButtonPress) { + //限定鼠标左键 + if (mouseEvent->button() == Qt::LeftButton) { + lastPoint = mouseEvent->pos(); + isPressed = true; + emit clicked(); + return true; + } + } else if (event->type() == QEvent::MouseMove) { + //允许拖动并且鼠标按下准备拖动 + if (canMove && isPressed) { + int dx = mouseEvent->pos().x() - lastPoint.x(); + int dy = mouseEvent->pos().y() - lastPoint.y(); this->move(this->x() + dx, this->y() + dy); return true; - } else if (event->type() == QEvent::MouseButtonRelease && isPressed) { - isPressed = false; } - } - - if (event->type() == QEvent::MouseButtonPress) { - emit clicked(); + } else if (event->type() == QEvent::MouseButtonRelease) { + isPressed = false; } else if (event->type() == QEvent::MouseButtonDblClick) { emit doubleClicked(); } diff --git a/control/devicebutton/devicebutton.h b/control/devicebutton/devicebutton.h index a758f02..582442c 100644 --- a/control/devicebutton/devicebutton.h +++ b/control/devicebutton/devicebutton.h @@ -65,6 +65,9 @@ private: ButtonStyle buttonStyle; //按钮样式 ButtonColor buttonColor; //按钮颜色 + bool isPressed; //鼠标是否按下 + QPoint lastPoint; //鼠标按下最后坐标 + QString type; //图片末尾类型 QString imgName; //背景图片名称 bool isDark; //是否加深报警 @@ -94,6 +97,7 @@ public Q_SLOTS: void setButtonColor(const ButtonColor &buttonColor); Q_SIGNALS: + //鼠标单击双击事件 void clicked(); void doubleClicked(); }; diff --git a/tool/netserver/api/tcpserver1.cpp b/tool/netserver/api/tcpserver1.cpp index e253b0e..bd58893 100644 --- a/tool/netserver/api/tcpserver1.cpp +++ b/tool/netserver/api/tcpserver1.cpp @@ -8,13 +8,13 @@ TcpClient1::TcpClient1(QObject *parent) : QTcpSocket(parent) port = 6907; deviceID = "SSJC00000001"; + connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater())); + connect(this, SIGNAL(readyRead()), this, SLOT(readData())); #if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) connect(this, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(deleteLater())); #else connect(this, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(deleteLater())); -#endif - connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater())); - connect(this, SIGNAL(readyRead()), this, SLOT(readData())); +#endif } void TcpClient1::setIP(const QString &ip) diff --git a/tool/netserver/api/tcpserver2.cpp b/tool/netserver/api/tcpserver2.cpp index 1171853..4b82bc8 100644 --- a/tool/netserver/api/tcpserver2.cpp +++ b/tool/netserver/api/tcpserver2.cpp @@ -8,13 +8,13 @@ TcpClient2::TcpClient2(QObject *parent) : QTcpSocket(parent) port = 6908; deviceID = "SSJC00000001"; + connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater())); + connect(this, SIGNAL(readyRead()), this, SLOT(readData())); #if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) connect(this, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(deleteLater())); #else connect(this, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(deleteLater())); #endif - connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater())); - connect(this, SIGNAL(readyRead()), this, SLOT(readData())); } void TcpClient2::setIP(const QString &ip) diff --git a/tool/nettool/api/tcpclient.cpp b/tool/nettool/api/tcpclient.cpp index 49b346c..0610331 100644 --- a/tool/nettool/api/tcpclient.cpp +++ b/tool/nettool/api/tcpclient.cpp @@ -10,12 +10,12 @@ TcpClient::TcpClient(QTcpSocket *socket, QObject *parent) : QObject(parent) port = socket->peerPort(); connect(socket, SIGNAL(disconnected()), this, SLOT(slot_disconnected())); + connect(socket, SIGNAL(readyRead()), this, SLOT(slot_readData())); #if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) connect(socket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(slot_error())); #else connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slot_error())); #endif - connect(socket, SIGNAL(readyRead()), this, SLOT(slot_readData())); } QString TcpClient::getIP() const diff --git a/tool/nettool/form/frmtcpclient.cpp b/tool/nettool/form/frmtcpclient.cpp index d33cd1f..3fa75a8 100644 --- a/tool/nettool/form/frmtcpclient.cpp +++ b/tool/nettool/form/frmtcpclient.cpp @@ -40,12 +40,12 @@ void frmTcpClient::initForm() socket = new QTcpSocket(this); connect(socket, SIGNAL(connected()), this, SLOT(connected())); connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected())); + connect(socket, SIGNAL(readyRead()), this, SLOT(readData())); #if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) connect(socket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(error())); #else connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error())); #endif - connect(socket, SIGNAL(readyRead()), this, SLOT(readData())); //定时器发送数据 timer = new QTimer(this); diff --git a/tool/nettool/form/frmudpclient.cpp b/tool/nettool/form/frmudpclient.cpp index 0091c88..f743cc2 100644 --- a/tool/nettool/form/frmudpclient.cpp +++ b/tool/nettool/form/frmudpclient.cpp @@ -36,12 +36,12 @@ void frmUdpClient::initForm() //实例化对象并绑定信号槽 socket = new QUdpSocket(this); + connect(socket, SIGNAL(readyRead()), this, SLOT(readData())); #if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) connect(socket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(error())); #else connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error())); -#endif - connect(socket, SIGNAL(readyRead()), this, SLOT(readData())); +#endif //定时器发送数据 timer = new QTimer(this); diff --git a/tool/nettool/form/frmudpserver.cpp b/tool/nettool/form/frmudpserver.cpp index 78b4f8c..7a01834 100644 --- a/tool/nettool/form/frmudpserver.cpp +++ b/tool/nettool/form/frmudpserver.cpp @@ -37,12 +37,12 @@ void frmUdpServer::initForm() //实例化对象并绑定信号槽 socket = new QUdpSocket(this); + connect(socket, SIGNAL(readyRead()), this, SLOT(readData())); #if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) connect(socket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(error())); #else connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error())); -#endif - connect(socket, SIGNAL(readyRead()), this, SLOT(readData())); +#endif //定时器发送数据 timer = new QTimer(this); diff --git a/tool/nettool/form/frmwebclient.cpp b/tool/nettool/form/frmwebclient.cpp index 14c2b40..5a3a865 100644 --- a/tool/nettool/form/frmwebclient.cpp +++ b/tool/nettool/form/frmwebclient.cpp @@ -175,7 +175,7 @@ void frmWebClient::disconnected() { isOk = false; ui->btnConnect->setText("连接"); - append(1, "服务器断开"); + append(1, "服务器断开"); } void frmWebClient::error()