Luca Danieli Luca Danieli - 3 months ago 9
C++ Question

How to choose a method overload when converting from a QVariant argument?

I have a problem.
In my *.cpp I have the following function:

void QcWMenuBar::setItems( const QVariantList & items )
{
clear();
Q_FOREACH( const QVariant & item, items )
//addMenu( item );
}


Qt has got many different addMenu() functions and therefore my program doesn't know what function to use for a QVariant type.
I would need in this case an
addMenu(QMenu * item)
.

How do I tell Qt to use this function?

Answer

You have to obtain the value of the right type from the variant:

void QcWMenuBar::setItems(const QVariantList & items)
{
  clear();
  for (auto & item: items) {
    Q_ASSERT(item.canConvert<QMenu*>());
    addMenu(item.value<QMenu*>());
  }
}