Use QAction without adding to the menu (or toolbar)

I am trying to develop an application with a very modular approach to commands and thought it would be nice if I used pyqt to use QAction to associate shortcuts with commands.
However, it seems that action shortcuts only work when an action is displayed in a menu or toolbar. Does anyone know a way to make this action work without its visibility?
Below is an example of code that shows what I'm trying. Thanks,

Andre

from PyQt4 import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

class TesteMW(QMainWindow):
    def __init__(self, *args):
        QMainWindow.__init__(self, *args)
        self.create_action()

    def create_action(self):
        self.na = QAction(self)
        self.na.setText('Teste')
        self.na.setShortcut('Ctrl+W')
        self.connect(self.na, SIGNAL('triggered()'), self.action_callback)
        # uncomment the next line for the action to work
        # self.menuBar().addMenu("Teste").addAction(self.na)

    def action_callback(self):
        print 'action called!'


app = QApplication(sys.argv)
mw = TesteMW()
mw.show()

app.exec_()
+3
source share
1 answer

, . QT QAction:

, QWidget:: addAction() QGraphicsWidget:: AddAction(). ; , (.. Qt:: ApplicationShortcut Qt:: ShortcutContext).

, - - .

+6

Source: https://habr.com/ru/post/1716310/


All Articles