PyQt6: Missing QAction.setMenu or equivalent

RoadrunnerWMC roadrunnerwmc at gmail.com
Wed Apr 21 19:26:59 BST 2021


That'd work in that particular case, but it's still a problem that there's
(as far as I know) no way to implement the following common pattern in
PyQt6:

from PyQt5 import QtWidgets
>

> app = QtWidgets.QApplication([])
> main_window = QtWidgets.QMainWindow()
>

> recent_menu = QtWidgets.QMenu()
> recent_menu.addAction('foo.py')

recent_menu.addAction('bar.txt')

recent_menu.addAction('baz.png')


> menu = main_window.menuBar().addMenu('&File')
> menu.addAction('Open...')
> menu.addAction('Open Recent').setMenu(recent_menu)

menu.addAction('Save')
> menu.addAction('Save As...')
>

> main_window.show()
> app.exec_()


On Wed, Apr 21, 2021 at 1:57 PM Dmitry Shachnev <mitya57 at ubuntu.com> wrote:

> Hi,
>
> On Wed, Apr 21, 2021 at 03:58:36PM +0200, Ales Erjavec wrote:
> > Hi
> >
> > In Qt6 QAction was moved to QtGui module and is therefore missing
> > `setMenu(QMenu)`, `menu()` , or rather the methods are implemented as
> > templates
> > that will be instantiated only when called
> > (https://doc.qt.io/qt-6/widgets-changes-qt6.html#qaction-qactiongroup).
> >
> > How do we set a menu on an QAction in PyQt6 since `QAction.setMenu`
> > does not exist?
>
> If your use of QAction.setMenu was just to get a drop-down button in the
> toolbar (like it was in my case), you can use a QToolButton for this.
>
> Example:
>
> https://github.com/retext-project/retext/commit/daf20c3633dd4a50
>
> --
> Dmitry Shachnev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210421/db50b2f4/attachment.htm>


More information about the PyQt mailing list