1. 因为只有QObject类及其子类派生的类才能使用信号和槽机制。
使用信号和槽还必须在类声明的最开始处添加Q_OBJECT宏,在这个程序中,类的声明是自动生成的,已经添加了这个宏。UI类继承自QDialog,QDialog类又继承自QWidget类,QWidget类是QObject类的子类,所以可以使用信号和槽。
//tcpclient.h
#ifndef TCPCLIENT_H
#define TCPCLIENT_H#include <QTcpSocket>
//#include "ex2.h"
#include "ui_ex2.h"class Ex2; // 声明类
class TcpClient : public QObject
{Q_OBJECT
public:TcpClient(Ex2 *parent);~TcpClient();bool Connect(QString ip,int port,int timeout);bool DisConnect();void Test();void Send(char *buf,int length);void Receive();QString ByteArrayToHexString(QByteArray buf);QTcpSocket *socket;
private:Ex2 *pUi;
};#endif // TCPCLIENT_H
2. 槽就是普通的C++函数,可以像一般的函数一样使用,槽的最大特点就是可以和信号关联。QObject::connect()这个函数connect(sender, signal, receiver, slot);
//tcpclient.cpp
#include "tcpclient.h"
#include "ex2.h"TcpClient::TcpClient(Ex2 *parent)
{socket = new QTcpSocket();pUi = parent;//连接信号槽connect(socket, &QTcpSocket::readyRead, this, &TcpClient::Receive);
}TcpClient::~TcpClient()
{delete socket;
}bool TcpClient::Connect(QString ip,int port,int timeout)
{socket->abort();//连接服务器socket->connectToHost(ip, port);if(socket->waitForConnected(timeout)){return true;}else{return false;}
}bool TcpClient::DisConnect()
{socket->disconnectFromHost();return true;
}void TcpClient::Send(char *buf,int length)
{socket->write(buf,length);socket->flush();
}//12 34 A1 A2 -> "12 34 A1 A2"
QString TcpClient::ByteArrayToHexString(QByteArray data)
{QString ret(data.toHex().toUpper());int len = ret.length()/2;for(int i=1;i<len;i++){ret.insert(2*i+i-1," ");}return ret;
}void TcpClient::Receive()
{QByteArray buffer;//读取缓冲区数据buffer = socket->readAll();QString str0 = ByteArrayToHexString(buffer) +" ";if(!buffer.isEmpty()){QString str = pUi->ui->textEditMy1->toPlainText();str += str0;//刷新显示pUi->ui->textEditMy1->setText(str);}
}void TcpClient::Test()
{pUi->ui->textEditMy1->setText("Test");
}
//Ex2.cpp
void Ex2::on_pushButtonTcpConnect_clicked()
{if(ui->pushButtonTcpConnect->text()==tr("连接")){if(tcpClientSocket->Connect(ui->lineEditIP->text(),ui->lineEditPort->text().toInt(),2000) == true){ui->pushButtonTcpConnect->setText("断开");
// connect(tcpClientSocket->socket,&QObject::readyRead,this,&tcpClientSocket::Receive);}}else{tcpClientSocket->DisConnect();ui->pushButtonTcpConnect->setText("连接");}
}
void Ex2::on_pushButtonTcpSend_clicked()
{//获取文本框内容并以ASCII码形式发送uchar buf[3];buf[0] = 0x11;buf[1] = 0x22;buf[2] = 0x33;tcpClientSocket->Send((char *)buf,3);
}
//Ex2.h
#ifndef EX2_H
#define EX2_H#include <QDialog>#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include "tcpclient.h"QT_BEGIN_NAMESPACE
namespace Ui { class Ex2; }
QT_END_NAMESPACEclass Ex2 : public QDialog
{Q_OBJECTpublic:Ex2(QWidget *parent = nullptr);~Ex2();Ui::Ex2 *ui;TcpClient *tcpClientSocket;private slots:void on_pushButtonMy1_clicked();void on_pushButtonMy2_clicked();void on_pushButtonClr_clicked();void on_pushButtonComm_clicked();void on_sendButton_clicked();void Read_Data();void on_openButton_clicked();void on_pushButtonTcpConnect_clicked();void on_pushButtonTcpSend_clicked();private://Ui::Ex2 *ui;QSerialPort *serial;
};
#endif // EX2_H
3. 实践证明上述代码,通过上述可以实现接收到TCP数据(readyRead()函数)后,通过TcpClient类的Receive()函数处理。可以把TCP数据的收发都封装在TcpClient类中。
4. 列举一下使用信号和槽应该注意的几点:
需要继承自QObject或其子类;
在类声明的最开始处添加Q OBJECT宏;
槽中参数的类型要和信号参数的类型相对应,且不能比信号的参数多;
信号只用声明,没有定义,且返回值为void类型。