20220526 【qt】在写代码的时候往往容易忽视QTimer定时器开启之后,对象销毁的时候可以这样处理,如下所示
x_class::~x_class()
{
if (timer->isActive()) {
timer->stop();
}
}
贴一个我觉得写的好的tcp/ip
头文件
#ifndef NETWORKCONTROLLER_H
#define NETWORKCONTROLLER_H
#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
class NetworkController : public QObject
{
Q_OBJECT
public:
explicit NetworkController(QObject *parent = 0);
private:
QTcpServer m_server;
private slots:
void newConnection();
void disconnected();
void readyRead();
signals:
void commandAccepted(QString command, QString value);
};
#endif // NETWORKCONTROLLER_H
源文件
#include "networkcontroller.h"
NetworkController::NetworkController(QObject *parent) :
QObject(parent)
{
QObject::connect(&m_server, &QTcpServer::newConnection, this, &NetworkController::newConnection);
if (!m_server.listen(QHostAddress::Any, 8080)) {
qDebug() << "Failed to run http server";
}
}
void NetworkController::newConnection()
{
QTcpSocket *socket = m_server.nextPendingConnection();
if (!socket)
return;
QObject::connect(socket, &QAbstractSocket::disconnected, this, &NetworkController::disconnected);
QObject::connect(socket, &QIODevice::readyRead, this, &NetworkController::readyRead);
}
void NetworkController::disconnected()
{
QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
if (!socket)
return;
socket->disconnect();
socket->deleteLater();
}
void NetworkController::readyRead()
{
QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
if (!socket || socket->state() == QTcpSocket::ClosingState)
return;
QString requestData = socket->readAll();
QStringList list = requestData.split(' ');
QString path = list[1];
list = path.split('/');
QByteArray reply;
if (list.count() == 3) {
socket->write("HTTP/1.1 200 OK\r\n");
reply = QStringLiteral("Command accepted: %1 %2").arg(list[1], list[2]).toUtf8();
emit commandAccepted(list[1], list[2]);
} else {
socket->write("HTTP/1.1 404 Not Found\r\n");
reply = "Command rejected";
}
socket->write("Content-Type: text/plain\r\n");
socket->write(QStringLiteral("Content-Length: %1\r\n").arg(reply.size()).toUtf8());
socket->write("Connection: close\r\n");
socket->write("\r\n");
socket->write(reply);
socket->disconnectFromHost();
}