Pergunta

I do not understand why in the book Rapid GUI Programming with Python and Qt, a context menu is added to a central widget by calling addActions() on the main window (self), like so (p. 180):

self.addActions(self.imageLabel,
                (editInvertAction, …))

where self is a QMainWindow, and imageLabel is a QLabel set as the central widget with

# Added actions will be put in a context menu:
self.imageLabel.setContextMenuPolicy(Qt.ActionsContextMenu)
self.setCentralWidget(self.imageLabel)

Now, why would the main window be associated in some way (through self.addActions()) to the context menu of the central widget? Isn't it enough to call addActions() directly on the central widget? In fact, the following does create a context menu:

self.imageLabel.addActions((editInvertAction, …))

Why doesn't the book example the context menu this way? isn't this equivalent to the more involved self.addActions(…) form?

PS: I even see that the documentation for QMainWindow.addActions() does not even mention any first argument (self.imageLabel, above)! I'm completely lost as to why the book uses the first snippet above instead of the last one… Help! :)

Foi útil?

Solução

Using self.addAction() on QMainWindow allow all QMainWindow childs (Docks, StatusBar, ToolBar, MenuBar, ...) to use theses actions, not only the central widget.

But the best way to get a fine-grained context menu control is to use the customContextMenuRequested signal (http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qwidget.html#customContextMenuRequested).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top