下载资源后端资源详情
TestProject.zip
大小:42.98MB
价格:24积分
下载量:0
评分:
5.0
上传者:m0_74805797
更新日期:2024-07-29

源代码·Qt/C++使用小记9

资源文件列表(大概)

文件名
大小
TestProject/
-
TestProject/Bin/
-
TestProject/Bin/Debug_64/
-
TestProject/Bin/Debug_64/D3Dcompiler_47.dll
3.98MB
TestProject/Bin/Debug_64/Qt5Cored.dll
12.52MB
TestProject/Bin/Debug_64/Qt5Guid.dll
13.14MB
TestProject/Bin/Debug_64/Qt5Networkd.dll
3.22MB
TestProject/Bin/Debug_64/Qt5Svgd.dll
708KB
TestProject/Bin/Debug_64/Qt5Widgetsd.dll
11.02MB
TestProject/Bin/Debug_64/TidyUpQtProjectd.exe
179KB
TestProject/Bin/Debug_64/TidyUpQtProjectd.ilk
2.19MB
TestProject/Bin/Debug_64/TidyUpQtProjectd.pdb
2.91MB
TestProject/Bin/Debug_64/bearer/
-
TestProject/Bin/Debug_64/bearer/qgenericbearerd.dll
170KB
TestProject/Bin/Debug_64/iconengines/
-
TestProject/Bin/Debug_64/iconengines/qsvgicond.dll
104.5KB
TestProject/Bin/Debug_64/imageformats/
-
TestProject/Bin/Debug_64/imageformats/qgifd.dll
100.5KB
TestProject/Bin/Debug_64/imageformats/qicnsd.dll
121KB
TestProject/Bin/Debug_64/imageformats/qicod.dll
100KB
TestProject/Bin/Debug_64/imageformats/qjpegd.dll
514.5KB
TestProject/Bin/Debug_64/imageformats/qsvgd.dll
77.5KB
TestProject/Bin/Debug_64/imageformats/qtgad.dll
77.5KB
TestProject/Bin/Debug_64/imageformats/qtiffd.dll
770.5KB
TestProject/Bin/Debug_64/imageformats/qwbmpd.dll
74KB
TestProject/Bin/Debug_64/imageformats/qwebpd.dll
1.04MB
TestProject/Bin/Debug_64/libEGLd.dll
59KB
TestProject/Bin/Debug_64/libGLESV2d.dll
11.9MB
TestProject/Bin/Debug_64/opengl32sw.dll
19.95MB
TestProject/Bin/Debug_64/platforms/
-
TestProject/Bin/Debug_64/platforms/qwindowsd.dll
3.63MB
TestProject/Bin/Debug_64/translations/
-
TestProject/Bin/Debug_64/translations/qt_ar.qm
156.26KB
TestProject/Bin/Debug_64/translations/qt_bg.qm
161.45KB
TestProject/Bin/Debug_64/translations/qt_ca.qm
175.01KB
TestProject/Bin/Debug_64/translations/qt_cs.qm
170.6KB
TestProject/Bin/Debug_64/translations/qt_da.qm
165.68KB
TestProject/Bin/Debug_64/translations/qt_de.qm
183.58KB
TestProject/Bin/Debug_64/translations/qt_en.qm
23B
TestProject/Bin/Debug_64/translations/qt_es.qm
161.29KB
TestProject/Bin/Debug_64/translations/qt_fi.qm
171.41KB
TestProject/Bin/Debug_64/translations/qt_fr.qm
162.26KB
TestProject/Bin/Debug_64/translations/qt_gd.qm
185.13KB
TestProject/Bin/Debug_64/translations/qt_he.qm
135.43KB
TestProject/Bin/Debug_64/translations/qt_hu.qm
88.86KB
TestProject/Bin/Debug_64/translations/qt_it.qm
157.35KB
TestProject/Bin/Debug_64/translations/qt_ja.qm
126.86KB
TestProject/Bin/Debug_64/translations/qt_ko.qm
128.18KB
TestProject/Bin/Debug_64/translations/qt_lv.qm
150KB
TestProject/Bin/Debug_64/translations/qt_pl.qm
159.15KB
TestProject/Bin/Debug_64/translations/qt_ru.qm
154.14KB
TestProject/Bin/Debug_64/translations/qt_sk.qm
122.81KB
TestProject/Bin/Debug_64/translations/qt_uk.qm
154.52KB
TestProject/Bin/Release_64/
-
TestProject/Bin/Release_64/D3Dcompiler_47.dll
3.98MB
TestProject/Bin/Release_64/Qt5Core.dll
5.52MB
TestProject/Bin/Release_64/Qt5Gui.dll
5.77MB
TestProject/Bin/Release_64/Qt5Network.dll
1.16MB
TestProject/Bin/Release_64/Qt5Svg.dll
320.5KB
TestProject/Bin/Release_64/Qt5Widgets.dll
5.28MB
TestProject/Bin/Release_64/TidyUpQtProject.exe
78.5KB
TestProject/Bin/Release_64/bearer/
-
TestProject/Bin/Release_64/bearer/qgenericbearer.dll
47KB
TestProject/Bin/Release_64/iconengines/
-
TestProject/Bin/Release_64/iconengines/qsvgicon.dll
37KB
TestProject/Bin/Release_64/imageformats/
-
TestProject/Bin/Release_64/imageformats/qgif.dll
32KB
TestProject/Bin/Release_64/imageformats/qicns.dll
39KB
TestProject/Bin/Release_64/imageformats/qico.dll
34KB
TestProject/Bin/Release_64/imageformats/qjpeg.dll
232KB
TestProject/Bin/Release_64/imageformats/qsvg.dll
25.5KB
TestProject/Bin/Release_64/imageformats/qtga.dll
25KB
TestProject/Bin/Release_64/imageformats/qtiff.dll
363KB
TestProject/Bin/Release_64/imageformats/qwbmp.dll
23.5KB
TestProject/Bin/Release_64/imageformats/qwebp.dll
471KB
TestProject/Bin/Release_64/libEGL.dll
15KB
TestProject/Bin/Release_64/libGLESV2.dll
2.4MB
TestProject/Bin/Release_64/opengl32sw.dll
19.95MB
TestProject/Bin/Release_64/platforms/
-
TestProject/Bin/Release_64/platforms/qwindows.dll
1.28MB
TestProject/Bin/Release_64/translations/
-
TestProject/Bin/Release_64/translations/qt_ar.qm
156.26KB
TestProject/Bin/Release_64/translations/qt_bg.qm
161.45KB
TestProject/Bin/Release_64/translations/qt_ca.qm
175.01KB
TestProject/Bin/Release_64/translations/qt_cs.qm
170.6KB
TestProject/Bin/Release_64/translations/qt_da.qm
165.68KB
TestProject/Bin/Release_64/translations/qt_de.qm
183.58KB
TestProject/Bin/Release_64/translations/qt_en.qm
23B
TestProject/Bin/Release_64/translations/qt_es.qm
161.29KB
TestProject/Bin/Release_64/translations/qt_fi.qm
171.41KB
TestProject/Bin/Release_64/translations/qt_fr.qm
162.26KB
TestProject/Bin/Release_64/translations/qt_gd.qm
185.13KB
TestProject/Bin/Release_64/translations/qt_he.qm
135.43KB
TestProject/Bin/Release_64/translations/qt_hu.qm
88.86KB
TestProject/Bin/Release_64/translations/qt_it.qm
157.35KB
TestProject/Bin/Release_64/translations/qt_ja.qm
126.86KB
TestProject/Bin/Release_64/translations/qt_ko.qm
128.18KB
TestProject/Bin/Release_64/translations/qt_lv.qm
150KB
TestProject/Bin/Release_64/translations/qt_pl.qm
159.15KB
TestProject/Bin/Release_64/translations/qt_ru.qm
154.14KB
TestProject/Bin/Release_64/translations/qt_sk.qm
122.81KB
TestProject/Bin/Release_64/translations/qt_uk.qm
154.52KB
TestProject/Include/
-
TestProject/Lib/
-
TestProject/Source/
-
TestProject/Source/MyCustomUdpSocket.cpp
3.09KB
TestProject/Source/MyCustomUdpSocket.h
2.78KB
TestProject/Source/TestProject.pro
2.63KB
TestProject/Source/TestProject.pro.user
23.23KB
TestProject/Source/Widget.cpp
10.72KB
TestProject/Source/Widget.h
2.23KB
TestProject/Source/Widget.ui
5.59KB
TestProject/Source/main.cpp
175B

资源内容介绍

Qt/C++使用小记9【使用UDP创建连接发送消息/传输单个文件】
#include "Widget.h"#include "ui_Widget.h"#include <QDateTime>#include <QFileDialog>#include <QFile>#include <QThread>#include <QDir>#include <QDebug>Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget), m_udpSocket(nullptr), m_strFilePath(""), m_bRecevieFile(false), m_uFileStatus(Default_Status), m_strReceviePath(""){ ui->setupUi(this); // 实例化对象 m_udpSocket = new MyCustomUdpSocket; // 绑定接受数据的信号与槽 connect(m_udpSocket, &MyCustomUdpSocket::SignalRecevieData, this, &Widget::SlotRecevieShowData); // 初始化连接状态未连接为红色 连接上了则为绿色 通过心跳判定 // 心跳规定为3秒一次 若四秒内没有收到心跳 置为未连接 ui->label_status->setStyleSheet("color:red;"); // 绑定timer connect(&m_timer, &QTimer::timeout, this, [=]{ ui->label_status->setStyleSheet("color:red;"); ui->label_status->setText(u8"未连接"); });}Widget::~Widget(){ // 释放 DELETE_OBJ(m_udpSocket); delete ui;}/** * @brief on_pushButton_bind_clicked 绑定按钮点击槽函数 */void Widget::on_pushButton_bind_clicked(){ // 判断是否绑定,若已绑定不能重复绑定 if(u8"断开绑定" == ui->pushButton_bind->text()) { // 断开绑定 m_udpSocket->BreakBind(); qDebug()<<__FUNCTION__<<m_udpSocket->GetConnectStatus(); // 设置ui ui->pushButton_bind->setText(u8"绑定"); ui->pushButton_bind->setStyleSheet("color:black;"); } else { //先进行绑定并获取返回值 bool flag = m_udpSocket->SetHostUdpInfo(ui->lineEdit_host_Addr->text(), ui->lineEdit_host_port->text().toInt()); //判断返回值是否绑定成功(为修改按钮文本和发送IP和端口号) if(!flag) { //未绑定成功则在浏览框中提示然后返回 ui->textBrowser->append(u8"未绑定成功,请确认IP或端口号是否正确!" + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")); return; } else { ui->textBrowser->append(u8"绑定成功!" + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")); } // 打印当前绑定信息 qDebug()<<__FUNCTION__<<m_udpSocket->GetConnectStatus(); //更改按钮文本 ui->pushButton_bind->setText(u8"断开绑定"); ui->pushButton_bind->setStyleSheet("color:red;"); //发送IP和端口号信息 m_udpSocket->SetTargetUdpInfo(ui->lineEdit_target_addr->text(), ui->lineEdit_target_prot->text().toUShort()); }}/** * @brief Widget::on_pushButton_send_message_clicked 发送消息按钮槽函数 */void Widget::on_pushButton_send_message_clicked(){ // 如果为未绑定状态 if(m_udpSocket->GetConnectStatus() == QAbstractSocket::UnconnectedState) { ui->textBrowser->append(u8"网络未连接 " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")); } else if(m_udpSocket->GetConnectStatus() == QAbstractSocket::BoundState) // 为绑定状态 { // 获取界面输入的信息 QString strData = ui->textEdit->toPlainText(); if(strData.isEmpty()) return; ui->textBrowser->append(u8"我 " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")); ui->textBrowser->append(" " + strData); // 调用接口发送数据 统一编码格式为utf8发送 m_udpSocket->SendData(strData.toUtf8()); } else { // 若为其他状态 则打印连接状态 qDebug()<<__FUNCTION__<<m_udpSocket->GetConnectStatus(); }}/** * @brief SlotRecevieShowData 接收数据并显示槽函数 * @param strData 具体内容 */void Widget::SlotRecevieShowData(const QByteArray &strData){ // 如果信息内容为宏定义的标识符 为开始传输文件 "START_SENDFILE" if(QString::fromUtf8(strData) == START_SEND_FILE) { // 接收文件置true m_bRecevieFile = true; // 接收状态改为处理文件名字 m_uFileStatus = Handle_FileName_Status; return; } // 如果信息内容为宏定义的标识符 为结束传输文件 "END_SENDFILE" if(QString::fromUtf8(strData) == END_SEND_FILE) { // 重置接受文件状态 // 正在接收文件置否 m_bRecevieFile = false; // 文件处理步骤置为初始状态 m_uFileStatus = Default_Status; // 显示接收文件完成信息 QStringList strList = m_strReceviePath.split("/"); ui->textBrowser->append(u8"接收文件: " + strList.last() + u8" 完成 " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")); // 接收文件路径置空 m_strReceviePath = ""; return; } // 判断文件处理状态 switch(m_uFileStatus) { case Handle_FileName_Status: { // 文件放在根目录的RecevieFile文件夹下 QDir dir; if(!dir.exists("./RecevieFile")) { bool b = dir.mkpath("./RecevieFile"); if(!b) qDebug() << "Failed to create folder:" << "./RecevieFile"; } const QString strFilePath = "./RecevieFile/" + QString::fromUtf8(strData); ui->textBrowser->append(u8"开始接收文件: " + QString::fromUtf8(strData) + " " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")); // 保存文件的路径和文件名 m_strReceviePath = strFilePath; // 改变接收文件状态为处理文件数据 m_uFileStatus = Handle_FileData_Status; return; } case Handle_FileData_Status: { // 创建QFile QFile file(m_strReceviePath); // 用只写和追加写入 if (!file.open(QIODevice::WriteOnly | QIODevice::Append)) { // 若创建或者打开失败 显示失败信息并置为初始状态 qDebug() << "Failed to create file" << m_strReceviePath; m_uFileStatus = Default_Status; m_bRecevieFile = false; ui->textBrowser->append(u8"接收文件: " + m_strReceviePath + u8" 失败 " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")); m_strReceviePath = ""; return; } // 写入文件 file.write(strData.data(), strData.size()); file.close(); } default: break; } // 判断文件处理状态 // 若不为接收文件状态 则正常接收消息 if(m_bRecevieFile == false) { // 如果信息不为空 if(!strData.isEmpty()) { // 过滤掉心跳信息 if(QString::fromUtf8(strData) != HEARTBEAT_INFO) { ui->textBrowser->append(u8"对方 " + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")); ui->textBrowser->append(" " + QString::fromUtf8(strData)); } else // 处理心跳信息 { // 收到心跳就暂停计时 m_timer.stop(); // 开启新一轮计时4秒间隔 m_timer.start(JUDGMENT_BREAK_LINK_SPACE); ui->label_status->setText(u8"已连接"); ui->label_status->setStyleSheet("color:green;"); } } }}/** * @brief on_pushButton_choose_file_clicked 选择文件按钮槽函数 */void Widget::on_pushButton_choose_file_clicked(){ // 获取打开窗口的文件的路径 QS

用户评论 (0)

发表评论

captcha