[Interest] Warning about QHash keys().toVector()

Kevin André hyperquantum at gmail.com
Sun Jun 13 18:20:31 CEST 2021


Hi,

I have the following piece of code:

    QVector<int> CompatibilityInterfaceImpl::getActionIds() const
    {
        return _actions.keys().toVector();   // _actions is a QHash
    }

In Qt Creator this generates the following warning:
   allocating an unneeded temporary container [clazy-container-anti-pattern]

How can I avoid the warning in this case?

I am using Qt 5.12, so I return a QVector instead of a QList because
using the old (Qt 5) QList is discouraged. Unfortunately many Qt API's
still use QList for nearly everything, so I am faced with a difficult
choice:

  1) go back to using QList
  2) deal with warnings and/or write my own conversion functions that
are probably not very efficient

Or is there another (convenient) way to solve this that I am not aware of?
Migrating to Qt 6 is not an option right now.


Thanks,

Kevin


More information about the Interest mailing list