更新代码
This commit is contained in:
@@ -14,15 +14,32 @@ frmWebClient::~frmWebClient()
|
||||
delete ui;
|
||||
}
|
||||
|
||||
bool frmWebClient::eventFilter(QObject *watched, QEvent *event)
|
||||
{
|
||||
//双击清空
|
||||
if (watched == ui->txtMain->viewport()) {
|
||||
if (event->type() == QEvent::MouseButtonDblClick) {
|
||||
on_btnClear_clicked();
|
||||
}
|
||||
}
|
||||
|
||||
return QWidget::eventFilter(watched, event);
|
||||
}
|
||||
|
||||
void frmWebClient::initForm()
|
||||
{
|
||||
QFont font;
|
||||
font.setPixelSize(16);
|
||||
ui->txtMain->setFont(font);
|
||||
ui->txtMain->viewport()->installEventFilter(this);
|
||||
|
||||
isOk = false;
|
||||
|
||||
//实例化对象并绑定信号槽
|
||||
socket = new QWebSocket("WebSocket", QWebSocketProtocol::VersionLatest, this);
|
||||
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
|
||||
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(disconnected()));
|
||||
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
|
||||
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error()));
|
||||
|
||||
//暂时使用前面两个信号,部分系统上后面两个信号Qt没实现,目前测试到5.15.2
|
||||
//在win上如果两组信号都关联了则都会触发,另外一组信号就是多个参数表示是否是最后一个数据包
|
||||
@@ -133,10 +150,10 @@ void frmWebClient::append(int type, const QString &data, bool clear)
|
||||
ui->txtMain->setTextColor(QColor("#22A3A9"));
|
||||
} else if (type == 1) {
|
||||
strType = "接收";
|
||||
ui->txtMain->setTextColor(QColor("#D64D54"));
|
||||
ui->txtMain->setTextColor(QColor("#753775"));
|
||||
} else {
|
||||
strType = "信息";
|
||||
ui->txtMain->setTextColor(QColor("#A279C5"));
|
||||
strType = "错误";
|
||||
ui->txtMain->setTextColor(QColor("#D64D54"));
|
||||
}
|
||||
|
||||
strData = QString("时间[%1] %2: %3").arg(TIMEMS).arg(strType).arg(strData);
|
||||
@@ -149,20 +166,20 @@ void frmWebClient::connected()
|
||||
isOk = true;
|
||||
ui->btnConnect->setText("断开");
|
||||
append(0, "服务器连接");
|
||||
append(2, QString("本地地址: %1 本地端口: %2").arg(socket->localAddress().toString()).arg(socket->localPort()));
|
||||
append(2, QString("远程地址: %1 远程端口: %2").arg(socket->peerAddress().toString()).arg(socket->peerPort()));
|
||||
append(0, QString("本地地址: %1 本地端口: %2").arg(socket->localAddress().toString()).arg(socket->localPort()));
|
||||
append(0, QString("远程地址: %1 远程端口: %2").arg(socket->peerAddress().toString()).arg(socket->peerPort()));
|
||||
}
|
||||
|
||||
void frmWebClient::disconnected()
|
||||
{
|
||||
isOk = false;
|
||||
//socket->abort();
|
||||
ui->btnConnect->setText("连接");
|
||||
append(1, "服务器断开");
|
||||
//打印下可能的错误信息
|
||||
if (socket->error() != QTcpSocket::UnknownSocketError) {
|
||||
append(2, socket->errorString());
|
||||
}
|
||||
append(1, "服务器断开");
|
||||
}
|
||||
|
||||
void frmWebClient::error()
|
||||
{
|
||||
append(2, socket->errorString());
|
||||
}
|
||||
|
||||
void frmWebClient::sendData(const QString &data)
|
||||
|
||||
Reference in New Issue
Block a user