Qt5获取本机所有IP和串口
作者:互联网
note
- 环境: windows
- 需要项目添加 serialport 和 network 模块
- QToolkits 扩展中......
结果
IP
- IP 获取结果
本机串口获取
- 串口获取结果
范例代码
- 已经封装好, copy即可使用
获取本机IP调用范例
using namespace oct_tk;
QToolkits tk;
qDebug() << "\n\n\n ================== ip all ==================";
{
const QStringList&& iplist = tk.local_ip(QToolkits::IP_VERSION::IPVX);
for (const auto& item : iplist)
{
qDebug() << "ip=" << item;
}
}
qDebug() << "\n\n\n ================== ip V4 ==================";
{
const QStringList&& iplist = tk.local_ip(QToolkits::IP_VERSION::IPV4);
for (const auto& item : iplist)
{
qDebug() << "ip=" << item;
}
}
qDebug() << "\n\n\n ================== ip V6 ==================";
{
const QStringList&& iplist = tk.local_ip(QToolkits::IP_VERSION::IPV6);
for (const auto& item : iplist)
{
qDebug() << "ip=" << item;
}
}
串口获取使用范例
qDebug() << "\n\n\n ================== serial port ==================";
{
const QStringList&& sp_list = tk.serial_port();
for (const auto& item : sp_list)
{
qDebug() << "sp=" << item;
}
}
代码
QToolkits.h
#ifndef QTOOLKITS_H_
#define QTOOLKITS_H_
#include <QStringList>
namespace oct_tk
{
class QToolkits
{
public:
enum IP_VERSION
{
IPVX = 0, /// 本机所有IP
IPV4 = 1,
IPV6 = 2,
};
public:
QToolkits();
~QToolkits();
/// -------------------------------------------------------------------------------
/// @brief: 读取本机IP
/// @param: const IP_VERSION & & ipv - IP版本
/// @ret: QStringList
/// 返回结果
/// -------------------------------------------------------------------------------
QStringList local_ip(const IP_VERSION&& ipv) noexcept;
/// -------------------------------------------------------------------------------
/// @brief: 获取本机可用串口
/// @ret: QStringList
///
/// -------------------------------------------------------------------------------
QStringList serial_port() noexcept;
};
}
#endif /// QTOOLKITS_H_
QToolkits.cpp
#include <QNetworkInterface>
#include <QSerialPort>
#include <QSerialPortInfo>
#include "QToolkits.h"
namespace oct_tk
{
QToolkits::QToolkits( )
{
}
QToolkits::~QToolkits()
{
}
/// --------------------------------------------------------------------------------
/// @brief: QToolkits::local_ip
/// --------------------------------------------------------------------------------
QStringList QToolkits::local_ip(const IP_VERSION&& ipv) noexcept
{
QStringList list_ret_value;
const QList<QHostAddress>&& ipaddress_list = QNetworkInterface::allAddresses();
for (const auto& item : ipaddress_list)
{
QString&& ip_item = item.toString();
int split_pos = ip_item.indexOf('%');
if (-1 != split_pos)
{
if (1 < split_pos)
{
split_pos = split_pos - 1;
}
ip_item = ip_item.left(split_pos);
}
if (IPV4 == ipv)
{
if (true == ip_item.contains(":"))
{
continue;
}
}
else if (IPV6 == ipv)
{
if (true == ip_item.contains(":"))
{
;
}
else
{
continue;
}
}
list_ret_value << ip_item;
}
return list_ret_value;
}
/// --------------------------------------------------------------------------------
/// @brief: QToolkits::serial_port
/// --------------------------------------------------------------------------------
QStringList QToolkits::serial_port() noexcept
{
QStringList list_ret_value;
{
const QList<QSerialPortInfo>&& list_sp = QSerialPortInfo::availablePorts();
QSerialPort sp;
for (const auto& sp_item : list_sp)
{
sp.setPort(sp_item);
list_ret_value << sp.portName();
}
}
/// 排序函数
auto case_insensitive_less_than = [](const QString& str1, const QString& str2)
{
return str1.toLower() < str2.toLower();
};
/// 排序
qSort(list_ret_value.begin(), list_ret_value.end(), case_insensitive_less_than);
return list_ret_value;
}
}
标签:Qt5,const,ip,串口,list,item,QToolkits,IP 来源: https://www.cnblogs.com/pandamohist/p/16417963.html