在QAction菜单项中存储其他数据 - python

时间:2015-12-10 21:20:07

标签: python pyside

我有一个通过for循环生成的动态qmenu。我已将其分解为最基本的属性。我想知道的是,我可以创建一个自定义的QMenuItem,这样我就可以在每个菜单项中存储一个实际的对象吗?目前它只支持用作名称的字符串。

如何在菜单项中存储其他数据/信息?

我想像普通一样设置显示文本,然后另外在for循环中,为每个项目添加一个类对象或某种数据。我在伪循环中放置了伪代码,但它被注释掉了。

我认为我接近创建一个自定义QAction项目并继承QAction但是在尝试使用它时似乎没有用...

class ActionObject( QtGui.QAction ):
    def __init__( self, text="", parent=None ):
        super( ActionObject, self ).__init__( parent )
        self.data = None

enter image description here

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Imports
# ------------------------------------------------------------------------------
import sys
from PySide import QtGui, QtCore

class Person():
    def __init__(self, name="", age=None):
        self.name = name
        self.age = age

# Main Widget
# ------------------------------------------------------------------------------
class ExampleWidget(QtGui.QWidget):

    def __init__(self,):
        super(ExampleWidget, self).__init__()

        self.initUI()


    def initUI(self):
        # formatting
        self.setWindowTitle("Example")

        # context menu
        self.main_menu = QtGui.QMenu()

        self.sub_menu = QtGui.QMenu("Great")
        self.main_menu.addMenu(self.sub_menu)


        names = ["Joe","Kevin","Amy","Doug","Jenny"]

        # sub-menu
        for x in xrange(len(names)):
            name = str(x) + " - " + names[x]
            action = self.sub_menu.addAction( name )
            # action.data = Person()
            action.triggered.connect(self.menu_action)

        # widgets        
        self.factionsList = QtGui.QListWidget()

        # signal
        self.factionsList.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.factionsList.customContextMenuRequested.connect(self.on_context_menu_factions)

        # layout
        self.mainLayout = QtGui.QGridLayout(self)
        self.mainLayout.addWidget(self.factionsList, 1, 0)
        self.show()

    def menu_action(self):
        print "testing"


    def on_context_menu_factions(self, pos):
        self.main_menu.exec_( QtGui.QCursor.pos() )


# Main
# ------------------------------------------------------------------------------
if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)
    ex = ExampleWidget()
    res = app.exec_()
    sys.exit(res)

2 个答案:

答案 0 :(得分:2)

你可以简单地使用setData来设置数据,下面你有一个工作示例,我改变了你的代码位

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Imports
# ------------------------------------------------------------------------------
import sys
from PySide import QtGui, QtCore
from functools import partial

class Person():
    def __init__(self, name="", age=None):
        self.name = name
        self.age = age

    def getName(self):
        return self.name

# Main Widget
# ------------------------------------------------------------------------------
class ExampleWidget(QtGui.QWidget):

    def __init__(self,):
        super(ExampleWidget, self).__init__()

        self.initUI()


    def initUI(self):
        # formatting
        self.setWindowTitle("Example")

        # context menu
        self.main_menu = QtGui.QMenu()

        self.sub_menu = QtGui.QMenu("Great")
        self.main_menu.addMenu(self.sub_menu)


        names = ["Joe","Kevin","Amy","Doug","Jenny"]

        # sub-menu
        for index, name in enumerate(names):
            fancyName = "%s - %s" % (index, name)
            action = self.sub_menu.addAction( fancyName )
            action.setData(Person(name=name))
            action.triggered.connect(partial(self.menu_action, action))

        # widgets        
        self.factionsList = QtGui.QListWidget()

        # signal
        self.factionsList.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.factionsList.customContextMenuRequested.connect(self.on_context_menu_factions)

        # layout
        self.mainLayout = QtGui.QGridLayout(self)
        self.mainLayout.addWidget(self.factionsList, 1, 0)
        self.show()

    def menu_action(self, item):
        itmData = item.data()
        print itmData.getName()


    def on_context_menu_factions(self, pos):
        self.main_menu.exec_( QtGui.QCursor.pos() )


# Main
# ------------------------------------------------------------------------------
if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)
    ex = ExampleWidget()
    res = app.exec_()
    sys.exit(res)

答案 1 :(得分:1)

当然,正如Achayan回答的那样,您可以使用QAction的{​​{1}}来设置其他数据,并使用setData()来获取存储的数据。

然而,上面将解决您的问题,让我们看看扩展QAction。 data()QWidget基于,允许传递QMenu对象的实现addAction

QAction

已明确设置父级,因为它是class ActionObject( QtGui.QAction ): def __init__( self, text="", parent=None ): super( ActionObject, self ).__init__(text, parent ) self.data = None class ExampleWidget(QtGui.QWidget): # ...[cut]... def initUI(self): # ...[cut]... # sub-menu for x in xrange(len(names)): name = str(x) + " - " + names[x] action = ActionObject(name, self.sub_menu) action.data = Person() action.triggered.connect(self.menu_action) self.sub_menu.addAction(action) 的实现,而不是QWidget

这就是PyQt / Pyside允许使用自定义对象的方式,几乎无处不在 - 不使用猴子修补。