You are not logged in.

1

Monday, December 6th 2010, 10:07pm

Convert a QList to a QSet has errors ?

I'm trying to get a list of all the devices with a MAC address...and found the following on these forums.

PHP Source code

1
2
3
4
    QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
    foreach (QNetworkInterface i, list) {
        qDebug() << i;
    }


The problem is this code makes a list but the list has duplicate entries. So I tried to convert the QList to a QSet assuming this would result in a set of unique items by doing..

PHP Source code

1
   QSet<QNetworkInterfacesset = list.toSet();


But when I do this I get compile errors..

"no matching function for call to qHash(const QNetworkInterface&)"

..and..

"no match for 'operator==' in key() = ((QHashNode<QNetworkInterface, QHashDummyValue>*)this)->QHashNode<QNetworkInterface, QHashDummyValue>::key

I'm sure it's something I'm doing wrong, but I don't see it...

bst

Beginner

  • "bst" is male

Posts: 42

Location: Germany

  • Send private message

2

Tuesday, December 7th 2010, 3:08pm

Hi,

I do not get any duplicate entries here? FWIW, you could try it this way.

Source code

1
2
3
4
5
6
7
8
9
10
QSet<QString> set;
QString key;
QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
foreach (QNetworkInterface i, list) {
  key = i.hardwareAddress();
  if (!set.contains(key)) {
    qDebug() << i;
    set.insert(key);
  }
}


cu, Bernd

Junior

Professional

  • "Junior" is male

Posts: 1,613

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

3

Tuesday, December 7th 2010, 5:01pm

Quoted

The values stored in the various containers can be of any assignable data type. To qualify, a type must provide a default constructor, a copy constructor, and an assignment operator. This covers most data types you are likely to want to store in a container, including basic types such as int and double, pointer types, and Qt data types such as QString, QDate, and QTime, but it doesn't cover QObject or any QObject subclass (QWidget, QDialog, QTimer, etc.). If you attempt to instantiate a QList<QWidget>, the compiler will complain that QWidget's copy constructor and assignment operators are disabled. If you want to store these kinds of objects in a container, store them as pointers, for example as QList<QWidget *>.

ref: Assignable-data-types

Thus QSet<QNetworkInterface *> would work but probably not what your after. Some filtering logic may be the best recourse here as pointed out by bst.

4

Tuesday, December 7th 2010, 11:18pm

bst and Junior,

Thanks for the code and link.

Seems a shame the QList<QNetworkInterface> and QSet<QNetworkInterfaces> aren't compatible without changing to pointer usage. The QNetworkInterface object has loads of useful info, but it won't be too difficult to create a struct of the data I need most and manually filter the QList.