Home > Back-end >  Qt6 QWidgetAction replacement for 'setMenu'?
Qt6 QWidgetAction replacement for 'setMenu'?

Time:07-14

in Qt5 I used a QWidgetAction to create a custom looking menu entry, then I added a submenu to it using the setMenu method. But in Qt6 the setMenu method has been removed, how can this be achieved in Qt6? How can a menu item be created with custom widgets in it and a submenu?

Thanks

CodePudding user response:

This was made obsolete in this upstream Qt Change.

It personally concerns me that this was made obsolete leaving the API documentation reader clueless at first.

Digging a bit deeper, it seems that the reason for obsoleting this method is that QAction was moved from QtWidgets to QtGui. This is to have generic actions both in the QtWidgets and QtQuick worlds. It would be suboptimal to duplicate this in both worlds. So far so good. It makes sense.

It is also worth mentioning that the API is not yet removed. It is just deprecated. I think it would ever be removed if no one, like yourself, brings this up, or there is a proper replacement for it. So, be assured that it would not just be gone anytime soon.

Now, for the future, a QWidgetAction is represented in the containers you add it to by whatever widget you return from createWidget.

It seems that you could still use these APIs:

void QWidget::addAction(QAction *action)

and

QAction *QMenu::menuAction() const

With QWidgetAction.

If this still does not cover your use case, I would suggest to open a Jira ticket against the Qt bug tracker to propose a QMenu::setMenuInAction method to replace the obsolete.

  • Related