Question

I have dozens of QAction objects creating in the constructor of widget without giving them variable names.

Many of properties of QAction can be set right in the constructor (e.g. shortcut, signals connections, statusTip, etc.).

Is it possible to set QAction as a separator right in constructor and how to do it?

P.S. I think it's not a language-specific question but FYI here's what I'm using:

Python 2.7.6; PyQt4 4.10.3; Qt 4.8.5.

Was it helpful?

Solution

The short answer is: No.

PyQt's support for properties only allows you to set Qt-defined properties in the constructor. A quick skim of the properties section for QAction confirms that there is no separator property, so you cannot set it in that way.

Of course, there is a very simple and obvious workaround for this. Just define your own subclass of QAction, and do the necessary configuration in its __init__:

class Action(QtGui.QAction):
    def __init__(self, *args, **kwargs):
        super(Action, self).__init__(*args, **kwargs)
        self.setSeparator(kwargs.get('separator', False))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top