-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaddressesmodel.cpp
35 lines (29 loc) · 909 Bytes
/
addressesmodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <QtNetwork/QNetworkInterface>
#include "addressesmodel.h"
AddressesModel::AddressesModel(QObject *parent)
: QAbstractListModel(parent)
{
this->addresses = QNetworkInterface::allAddresses();
QMutableListIterator<QHostAddress> iterator(this->addresses);
while (iterator.hasNext()) {
QHostAddress address = iterator.next();
if (address.protocol() != QAbstractSocket::IPv4Protocol) {
iterator.remove();
}
}
}
int AddressesModel::rowCount(const QModelIndex &parent) const
{
return this->addresses.count();
}
QVariant AddressesModel::data(const QModelIndex &index, int role) const
{
QHostAddress address = this->addresses.at(index.row());
if (role == Qt::DisplayRole) {
return address.toString();
} else if (role == Qt::UserRole) {
return address.toIPv4Address();
} else {
return QVariant();
}
}