PyQt6 QAction menu/setMenu?

ekhumoro ekhumoro at gmx.com
Mon Jul 18 11:31:53 BST 2022


On 18/07/2022 07:19, Maurizio Berti wrote:
> I believe I can understand the reason for this: having a custom QWidgetAction that *also* acts as an item for a submenu.
> I realize this is quite a "fringe" case, but it also seems reasonable.

That is one genuine use-case. But the more general problem is: how to set a menu on a *subclass* of QAction? AFAICS,
this is currently impossible without QAction.setMenu(), since none of the other APIs will preserve the type. For example:

     submenu = QMenu()
     action = QMenu.addMenu(submenu)

just returns a plain QAction via QMenu.menuAction(), which is read-only. ISTM that Qt has deprecated an API without
first providing an alternative code path. The Qt docs strongly advise against using setMenu() in new code, but for some
genuine use-cases there really is no alternative. Or at least, I can't see one - please someone correct me if I am wrong!

It was suggested in Qt code review that something like QMenu.setMenuInAction() might be added at some point - which
*would* solve the problem. But what are users supposed to do in the meantime? For anyone porting from [Py]Qt5 who has an
extensive menu system based on subclasses of QAction, this issue seems to be a significant blocker.


More information about the PyQt mailing list