avecAddresse

flags
Francois DELOBEL 4 weeks ago
parent 7df8aa1ce6
commit 3fa24f88c3

@ -7,8 +7,14 @@ Window {
visible: true visible: true
title: qsTr("Hello World") title: qsTr("Hello World")
Row {
Text { Text {
text: netint.name text: netint.name
font.pointSize: 60 font.pointSize: 42
}
Text {
text: netint.address
font.pointSize: 36
}
} }
} }

@ -1,11 +1,11 @@
#include "networkinterface.h" #include "networkinterface.h"
#include <QNetworkInterface>
NetworkInterface::NetworkInterface(int interfaceIndex, QObject *parent) NetworkInterface::NetworkInterface(int interfaceIndex, QObject *parent)
: QObject{parent} : QObject{parent},
m_qni{QNetworkInterface::interfaceFromIndex(interfaceIndex)}
{ {
QNetworkInterface qni{QNetworkInterface::interfaceFromIndex(interfaceIndex)}; setName(m_qni.name());
setName(qni.name()); setAddress(m_qni.addressEntries().first().ip().toString());
} }
QString NetworkInterface::name() const QString NetworkInterface::name() const
@ -20,3 +20,16 @@ void NetworkInterface::setName(const QString &newName)
m_name = newName; m_name = newName;
emit nameChanged(); emit nameChanged();
} }
QString NetworkInterface::address() const
{
return m_address;
}
void NetworkInterface::setAddress(const QString &newAddress)
{
if (m_address == newAddress)
return;
m_address = newAddress;
emit addressChanged();
}

@ -2,22 +2,32 @@
#define NETWORKINTERFACE_H #define NETWORKINTERFACE_H
#include <QObject> #include <QObject>
#include <QNetworkInterface>
class NetworkInterface : public QObject class NetworkInterface : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged FINAL) Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged FINAL)
Q_PROPERTY(QString address READ address WRITE setAddress NOTIFY addressChanged FINAL)
public: public:
explicit NetworkInterface(int interfaceIndex, QObject *parent = nullptr); explicit NetworkInterface(int interfaceIndex, QObject *parent = nullptr);
QString name() const; QString name() const;
void setName(const QString &newName); void setName(const QString &newName);
QString address() const;
void setAddress(const QString &newAddress);
signals: signals:
void nameChanged(); void nameChanged();
void addressChanged();
private: private:
QNetworkInterface m_qni;
QString m_name; QString m_name;
QString m_address;
}; };
#endif // NETWORKINTERFACE_H #endif // NETWORKINTERFACE_H

Loading…
Cancel
Save