1
0
mirror of https://gitee.com/drabel/LibQQt.git synced 2025-01-04 10:18:44 +08:00

fix qqt socket udp client on macOS Qt4

This commit is contained in:
tianduanrui 2018-02-12 10:48:02 +08:00
parent 1cffd4d4be
commit 74b35f7c05
8 changed files with 51 additions and 49 deletions

View File

@ -24,7 +24,8 @@ SUBDIRS = src/qqt.pro
#SUBDIRS += examples/qrcodeexample #SUBDIRS += examples/qrcodeexample
#SUBDIRS += examples/qqtnetworkexample #SUBDIRS += examples/qqtnetworkexample
#udp 嵌入式linuxQt编译了udp后支持 #udp 嵌入式linuxQt编译了udp后支持
#SUBDIRS += examples/qqtudpexample #
greaterThan(QT_MAJOR_VERSION , 4):SUBDIRS += examples/qqtudpexample
#SUBDIRS += examples/qqtchartexample #SUBDIRS += examples/qqtchartexample
# #
# #
@ -48,7 +49,7 @@ SUBDIRS = src/qqt.pro
#SUBDIRS += test/qqtdicttest2 #SUBDIRS += test/qqtdicttest2
#SUBDIRS += test/consoletest #SUBDIRS += test/consoletest
# #
SUBDIRS += test/voicetest greaterThan(QT_MAJOR_VERSION , 4):SUBDIRS += test/voicetest
#!contains(QKIT_PRIVATE, iOS|iOSSimulator){ #!contains(QKIT_PRIVATE, iOS|iOSSimulator){
# SUBDIRS += test/cmdwidget # SUBDIRS += test/cmdwidget

View File

@ -1,21 +1,22 @@
#include "qqtwifidbwidget.h" #include "qqtwifidbwidget.h"
QQtWifiDBWidget::QQtWifiDBWidget(QWidget* parent) : QQtWidget(parent) QQtWifiDBWidget::QQtWifiDBWidget ( QWidget* parent ) : QQtWidget ( parent )
{ {
} }
void QQtWifiDBWidget::setDbPixMap(QQtWifiDBWidget::DB_STAT s, QString pixname) void QQtWifiDBWidget::setDbPixMap ( QQtWifiDBWidget::DB_STAT s, const QString& pixname )
{ {
if (s < Db_S0 || s >= Db_SMax) if ( s < Db_S0 || s >= Db_SMax )
return; return;
pic[s] = pixname; pic[s] = pixname;
} }
void QQtWifiDBWidget::setDB(QQtWifiDBWidget::DB_STAT s) void QQtWifiDBWidget::setDB ( QQtWifiDBWidget::DB_STAT s )
{ {
if (s < Db_S0 || s >= Db_SMax) if ( s < Db_S0 || s >= Db_SMax )
return; return;
setPixmap(pic[s]);
setPixmap ( pic[s] );
} }

View File

@ -1,4 +1,4 @@
#ifndef QQTWIFIDBWIDGET_H #ifndef QQTWIFIDBWIDGET_H
#define QQTWIFIDBWIDGET_H #define QQTWIFIDBWIDGET_H
#include <qqtwidget.h> #include <qqtwidget.h>
@ -26,7 +26,7 @@ public:
virtual ~QQtWifiDBWidget() {} virtual ~QQtWifiDBWidget() {}
void setDB ( DB_STAT s ); void setDB ( DB_STAT s );
void setDbPixMap ( DB_STAT s, QString pixname ); void setDbPixMap ( DB_STAT s, const QString& pixname );
signals: signals:

View File

@ -31,10 +31,6 @@ void QQtSocketUdpClient::installProtocol ( QQtUdpProtocol* stack )
m_protocol = stack; m_protocol = stack;
connect ( m_protocol, SIGNAL ( writeDatagram ( QByteArray, QHostAddress, quint16 ) ), connect ( m_protocol, SIGNAL ( writeDatagram ( QByteArray, QHostAddress, quint16 ) ),
this, SLOT ( slotWriteDatagram ( QByteArray, QHostAddress, quint16 ) ) ); this, SLOT ( slotWriteDatagram ( QByteArray, QHostAddress, quint16 ) ) );
#if QT_VERSION > QT_VERSION_CHECK(5,0,0)
connect ( m_protocol, SIGNAL ( writeDatagram ( const QNetworkDatagram& ) ),
this, SLOT ( slotWriteDatagram ( const QNetworkDatagram& ) ) );
#endif
} }
void QQtSocketUdpClient::uninstallProtocol ( QQtUdpProtocol* stack ) void QQtSocketUdpClient::uninstallProtocol ( QQtUdpProtocol* stack )
@ -46,10 +42,6 @@ void QQtSocketUdpClient::uninstallProtocol ( QQtUdpProtocol* stack )
disconnect ( m_protocol, SIGNAL ( writeDatagram ( QByteArray, QHostAddress, quint16 ) ), disconnect ( m_protocol, SIGNAL ( writeDatagram ( QByteArray, QHostAddress, quint16 ) ),
this, SLOT ( slotWriteDatagram ( QByteArray, QHostAddress, quint16 ) ) ); this, SLOT ( slotWriteDatagram ( QByteArray, QHostAddress, quint16 ) ) );
#if QT_VERSION > QT_VERSION_CHECK(5,0,0)
disconnect ( m_protocol, SIGNAL ( writeDatagram ( const QNetworkDatagram& ) ),
this, SLOT ( slotWriteDatagram ( const QNetworkDatagram& ) ) );
#endif
m_protocol = NULL; m_protocol = NULL;
} }
@ -144,9 +136,14 @@ void QQtSocketUdpClient::updateProgress ( qint64 bytes )
//pline() << bytes; //pline() << bytes;
} }
qint64 QQtSocketUdpClient::slotWriteDatagram ( const QByteArray& datagram, const QHostAddress& host, quint16 port )
{
return writeDatagram ( datagram, host, port );
}
void QQtSocketUdpClient::readyReadData() void QQtSocketUdpClient::readyReadData()
{ {
/*为什么用while?*/ //Qt4 没有那么高级的一次性读取的接口 /*为什么用while?*/ //Qt4 没有那么高级的一次性读取的接口?有
while ( hasPendingDatagrams() ) while ( hasPendingDatagrams() )
{ {
QByteArray bytes; QByteArray bytes;
@ -154,7 +151,24 @@ void QQtSocketUdpClient::readyReadData()
QHostAddress host; QHostAddress host;
quint16 port; quint16 port;
#if QT_VERSION > QT_VERSION_CHECK(5,0,0)
/*能够一次收够一条报文?测试的能。*/
QNetworkDatagram datagram = receiveDatagram();
/*由于添加了兼容Qt4的代码以上注释起来。*/
/*数据无意义 "" -1 在此设置*/
datagram.setDestination ( this->localAddress(), this->localPort() );
//pline() << "udp sender:" << datagram.senderAddress() << datagram.senderPort();
//pline() << "udp receiver:" << datagram.destinationAddress() << datagram.destinationPort();
m_protocol->translator ( datagram );
bytes = datagram.data();
host = datagram.senderAddress();
port = datagram.senderPort();
m_protocol->translator ( bytes, host, port );
#else
qint64 size = pendingDatagramSize(); qint64 size = pendingDatagramSize();
//pline() << "udp new msg size:" << size;
//这里的buf用完, 已经释放。 //这里的buf用完, 已经释放。
char* data = new char[size + 1](); char* data = new char[size + 1]();
qint64 len = readDatagram ( data, size, &host, &port ); qint64 len = readDatagram ( data, size, &host, &port );
@ -163,22 +177,8 @@ void QQtSocketUdpClient::readyReadData()
delete[] data; delete[] data;
m_protocol->translator ( bytes, host, port ); m_protocol->translator ( bytes, host, port );
#if QT_VERSION > QT_VERSION_CHECK(5,0,0)
/*能够一次收够一条报文?测试的能。*/
QNetworkDatagram datagram;
//datagrame = receiveDatagram();
/*由于添加了兼容Qt4的代码以上注释起来。*/
datagram.setData ( bytes );
datagram.setSender ( host, port );
/*数据无意义 "" -1 在此设置*/
datagram.setDestination ( this->localAddress(), this->localPort() );
//pline() << "udp sender:" << datagram.senderAddress() << datagram.senderPort();
//pline() << "udp receiver:" << datagram.destinationAddress() << datagram.destinationPort();
//pline() << "udp new msg size:" << size;
m_protocol->translator ( datagram );
#endif #endif
} }
} }

View File

@ -42,15 +42,9 @@ private slots:
void updateProgress ( qint64 bytes ); void updateProgress ( qint64 bytes );
//如果有一个同名的槽参数不同并且被用宏控制起来Qt编译不过。 //如果有一个同名的槽参数不同并且被用宏控制起来Qt编译不过。
//Qt的元对象系统解析信号和槽函数不支持宏。 //Qt的元对象系统解析信号和槽函数不支持宏。
//QtUdpSocketwriteDatagram不是个槽。
qint64 slotWriteDatagram ( const QByteArray& datagram, qint64 slotWriteDatagram ( const QByteArray& datagram,
const QHostAddress& host, quint16 port ) { const QHostAddress& host, quint16 port );
return writeDatagram ( datagram, host, port );
}
#if QT_VERSION > QT_VERSION_CHECK(5,0,0)
qint64 slotWriteDatagram ( const QNetworkDatagram& datagram ) {
return writeDatagram ( datagram );
}
#endif
private: private:
QQtUdpProtocol* m_protocol; QQtUdpProtocol* m_protocol;

View File

@ -21,16 +21,18 @@ public:
} }
virtual ~QQtUdpProtocol() {} virtual ~QQtUdpProtocol() {}
signals:
qint64 writeDatagram ( const QByteArray& datagram,
const QHostAddress& host, quint16 port );
#if QT_VERSION > QT_VERSION_CHECK(5,0,0) #if QT_VERSION > QT_VERSION_CHECK(5,0,0)
qint64 writeDatagram ( const QNetworkDatagram& datagram ) { qint64 writeDatagram ( const QNetworkDatagram& datagram ) {
//emit writeDatagram(); //emit writeDatagram();
} }
#endif #endif
signals:
qint64 writeDatagram ( const QByteArray& datagram,
const QHostAddress& host, quint16 port );
public slots: public slots:
protected: protected:
/** /**
* @brief * @brief
@ -42,13 +44,17 @@ protected:
const QHostAddress& host, quint16 port ) { const QHostAddress& host, quint16 port ) {
return 0; return 0;
} }
/*
* dispatcher
*/
#if QT_VERSION > QT_VERSION_CHECK(5,0,0) #if QT_VERSION > QT_VERSION_CHECK(5,0,0)
inline virtual bool dispatcher ( const QNetworkDatagram& ) { return 0; } inline virtual bool dispatcher ( const QNetworkDatagram& ) { return 0; }
#endif #endif
public: public:
/** /**
* @brief * @brief
* @brief QQtSocketUdpSocket用的 * @brief QQtSocketUdpSocket用的
* @param Qt通讯口readAll()bytes * @param Qt通讯口readAll()bytes
* @return * @return
*/ */

View File

@ -1,4 +1,4 @@
#include "qqtwidget.h" #include "qqtwidget.h"
#include <QStylePainter> #include <QStylePainter>
#include "qqtcore.h" #include "qqtcore.h"

View File

@ -1,4 +1,4 @@
#ifndef QQTWIDGET_H #ifndef QQTWIDGET_H
#define QQTWIDGET_H #define QQTWIDGET_H
#include <qqt-local.h> #include <qqt-local.h>