Qt5获取本机所有IP和串口

note

  • 环境: windows
  • 需要项目添加 serialport 和 network 模块
  • QToolkits 扩展中......

更新历史

  • 2022-10-22
    • 增加全新获取本机IP的方式, 详见 本文代码 类QToolkits的成员函数 local_ip2
    • 增加理由: cmake+Qt采用 local_ip的方式 与 local_ip2的方式获取的本机IP列表不同(local_ip2能全部获取,而local_ip 只能获取有效网卡上的IP,这里的有效指的能连通的网卡, 如果我建立了虚拟网卡, local_ip2的方式就可以获取,而local_ip在Cmake+qt的模式下无法读取, 实际开发环境:win7 + cmake+qt5.9.4 就遇到了)。
    • local_iplocal_ip2函数的功能完全相同。

结果

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:		遍历所有网卡的方式获取本机所有IP
		/// @param: 	const const IP_VERSION & & ipv - 需要本机IP的版本
		///  @ret:		QT_NAMESPACE::QStringList
		///				
		/// -------------------------------------------------------------------------------
		QStringList local_ip2(const 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::local_ip2
	/// --------------------------------------------------------------------------------
	QStringList QToolkits::local_ip2(const const IP_VERSION&& ipv) noexcept
	{
		QStringList list_ret_value;
		{
			QStringList ipList;

			/// 遍历网卡
			{
				QList<QNetworkInterface> networkInterface = QNetworkInterface::allInterfaces();
				foreach(QNetworkInterface net, networkInterface)
				{
					QList<QNetworkAddressEntry> addressEntriesList = net.addressEntries();
					foreach(QNetworkAddressEntry entry, addressEntriesList)
					{
						ipList.append(entry.ip().toString());
					}
				}
			}

			foreach(QString ipValue, ipList)
			{
				int split_pos = ipValue.indexOf('%');

				if (-1 != split_pos)
				{
					if (1 < split_pos)
					{
						split_pos = split_pos - 1;
					}
					ipValue = ipValue.left(split_pos);
				}

				if (IPV4 == ipv)
				{
					if (true == ipValue.contains(":"))
					{
						continue;
					}
				}
				else if (IPV6 == ipv)
				{
					if (true == ipValue.contains(":"))
					{
						;
					}
					else
					{
						continue;
					}
				}

				list_ret_value << ipValue;
			}
		}

		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;
	}
}
posted @ 2022-06-27 23:26  mohist  阅读(497)  评论(0编辑  收藏  举报