質問

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.

役に立ちましたか?

解決

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))
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top