提交 75c70166 编写于 作者: mahuifa's avatar mahuifa

新功能:网络接口信息模块功能开发

    1、修改SimpleTcpServer中client的释放方法;
    2、增加获取所有网络接口名称功能。
上级 dd2b4de4
#---------------------------------------------------------
# 功能: 获取设备网络信息
# 编译器:
#
# @开发者 mhf
# @邮箱 1603291350@qq.com
# @时间 2022/04/21
# @备注
#---------------------------------------------------------
FORMS += \
$$PWD/netproperty.ui
HEADERS += \
$$PWD/netproperty.h
SOURCES += \
$$PWD/netproperty.cpp
#include "netproperty.h"
#include "ui_netproperty.h"
#include <qnetworkinterface.h>
NetProperty::NetProperty(QWidget *parent) :
QWidget(parent),
ui(new Ui::NetProperty)
{
ui->setupUi(this);
init();
}
NetProperty::~NetProperty()
{
delete ui;
}
void NetProperty::init()
{
// 获取所有网络接口
const QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
ui->com_Interface->clear();
for(auto interface : interfaces)
{
qDebug() << interface.humanReadableName() << interface.name() << interface.type();
ui->com_Interface->addItem(interface.humanReadableName()); // 显示所有网络接口名称
}
}
#ifndef NETPROPERTY_H
#define NETPROPERTY_H
#include <QWidget>
namespace Ui {
class NetProperty;
}
class NetProperty : public QWidget
{
Q_OBJECT
public:
explicit NetProperty(QWidget *parent = nullptr);
~NetProperty();
private:
void init();
private:
Ui::NetProperty *ui;
};
#endif // NETPROPERTY_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>NetProperty</class>
<widget class="QWidget" name="NetProperty">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QWidget" name="">
<property name="geometry">
<rect>
<x>10</x>
<y>30</y>
<width>341</width>
<height>22</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>网络接口:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="com_Interface"/>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>
......@@ -73,9 +73,8 @@ void SimpleTcpServer::on_disconnected()
disconnect(m_client, &QTcpSocket::disconnected, this, &SimpleTcpServer::on_disconnected); // 断开绑定的信号槽
disconnect(m_client, &QTcpSocket::readyRead, this, &SimpleTcpServer::on_readyRead);
//delete m_client; // 这里不能用delete,否则在vs里会报错 0x00007FFBF870A1CE (Qt5Networkd.dll)处(位于 QMNetwork.exe 中)引发的异常: 0xC0000005: 读取位置 0xFFFFFFFFFFFFFFFF 时发生访问冲突。
QTcpSocket* tcpSocket = m_client;
m_client = nullptr; // 先将m_client置空,因为deleteLater不会置空
tcpSocket->deleteLater(); // 移除已经断开连接的Client(注意这里不能使用delete,否则在vs中会报错),但这种方式不会置为 空
m_client->deleteLater(); // 移除已经断开连接的Client(注意这里不能使用delete,否则在vs中会报错),但这种方式不会置为 空
m_client = nullptr;
}
/**
......
......@@ -28,6 +28,12 @@ Widget::~Widget()
*/
void Widget::on_pushButton_clicked()
{
if(m_netProperty)
{
delete m_netProperty;
m_netProperty = nullptr;
}
// 简易版
while (m_simpleTcpClients.count())
{
......@@ -144,3 +150,15 @@ void Widget::on_but_simpleUdpGroup_clicked()
m_simpleUdpGroups.last()->show();
}
/**
* @brief 打开网络属性窗口
*/
void Widget::on_but_property_clicked()
{
if(!m_netProperty)
{
m_netProperty = new NetProperty();
}
m_netProperty->show();
}
......@@ -11,7 +11,7 @@
#include "simpleudpsocket1.h"
#include "simpleudpsocket2.h"
#include "simpleudpgroup.h"
#include "netproperty.h"
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
......@@ -44,8 +44,11 @@ private slots:
void on_but_simpleUdpGroup_clicked();
void on_but_property_clicked();
private:
Ui::Widget *ui;
NetProperty* m_netProperty = nullptr; // 网络属性窗口
/******************简易版网络通信Demo********************/
QList<SimpleTcpClient*> m_simpleTcpClients; // 可打开任意多个TCP客户端
QList<SimpleTcpServer*> m_simpleTcpServers; // 可打开任意多个TCP服务端
......
......@@ -129,13 +129,26 @@
<string>HTTP</string>
</property>
</widget>
<widget class="QPushButton" name="but_property">
<property name="geometry">
<rect>
<x>290</x>
<y>80</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>网络属性</string>
</property>
</widget>
</widget>
<widget class="QGroupBox" name="groupBox_2">
<property name="geometry">
<rect>
<x>20</x>
<y>160</y>
<width>461</width>
<width>471</width>
<height>111</height>
</rect>
</property>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册