【发布时间】:2020-10-22 22:46:01
【问题描述】:
帮助,我到处寻找可能的地方。如果我从主类创建到 TCP 服务器的连接,那么一切正常。但是如果我创建一个单独的类,它会给出错误“函数参数太多”。
Qt 版本 5.9.9 使用 QTcpSocket
tcpclient.h
#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#include <QObject>
#include <QTcpSocket>
#include <QDataStream>
#include <QHostAddress>
class TcpClient : public QObject {
public:
TcpClient(QObject *parent = 0);
private:
QTcpSocket *tcpSocket;
const QString ip = "185.137.235.92";
const int port = 9080;
public slots:
void connect();
private slots:
void onReadyRead();
void onConnected();
void onDisconnected();
};
#endif // TCPCLIENT_H
tcpclient.cpp
#include "tcpclient.h"
TcpClient::TcpClient(QObject *parent) :QObject(parent) {
tcpSocket = new QTcpSocket(this);
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(onReabyRead()));
connect(tcpSocket, SIGNAL(connected()), this, SLOT(onConnected()));
connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
}
void TcpClient::connect() {
tcpSocket->connectToHost(QHostAddress(ip), port);
}
void TcpClient::onReadyRead() {
}
void TcpClient::onConnected() {
if(tcpSocket->waitForConnected() ) {
QDataStream in(tcpSocket);
ushort id;
uint size;
quint8 type;
ushort action;
QString message;
in >> id >> size >> type >> action;
message = tcpSocket->read(size);
qDebug() << id;
qDebug() << size;
qDebug() << type;
qDebug() << action;
qDebug() << message;
}
}
void TcpClient::onDisconnected() {
}
【问题讨论】: