创建一个可重用的qmenu python

时间:2015-12-09 18:10:44

标签: python pyside

所以我在这里有一个简单的例子。一个包含2个小部件的小部件&u; ui。 我想要做的是根据用户悬停在哪个小部件而拥有不同的qmenu弹出窗口。

我想做什么...... - 将实际的菜单项移动到他们自己的core_rcmenu的单独文件中,这样我就可以保持井井有条。

self.cmenu_edit = QtGui.QMenu()
self.mi_add_new_node = self.cmenu_edit.addAction('Add New')
self.mi_add_new_node.setShortcut('Ctrl+N')
self.cmenu_edit.addSeparator()
self.mi_rename_node = self.cmenu_edit.addAction('Rename')

然后当用户右键单击被调用的函数时,我将传递一个变量来确定要显示的qmenu。这样做的原因是,将qmenu放在自己的文件中并保持UI的清洁,其次qmenu在多个小部件之间共享,而一些小部件则出现在其他文件中。

enter image description here

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


    # Main
    # ------------------------------------------------------------------------------
    class ExampleA(QtGui.QWidget):

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

            self.initUI()


        def initUI(self):
            # formatting
            self.resize(550, 400)
            self.setWindowTitle("Factions")

            # context menu - passes
            self.cmenu_edit = QtGui.QMenu()
            self.mi_add_new_node = self.cmenu_edit.addAction('Add New')
            self.mi_add_new_node.setShortcut('Ctrl+N')
            self.cmenu_edit.addSeparator()
            self.mi_rename_node = self.cmenu_edit.addAction('Rename')

            # widget
            self.treeA = QtGui.QTreeWidget()
            self.treeB = QtGui.QTreeWidget()

            # signals
            self.treeA.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
            self.treeA.customContextMenuRequested.connect(self.on_context_menu_open)

            self.treeB.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
            self.treeB.customContextMenuRequested.connect(self.on_context_menu_open)


            # layout
            self.mainLayout = QtGui.QGridLayout(self)
            self.mainLayout.setContentsMargins(0,0,0,0)
            self.mainLayout.addWidget(self.treeA, 0,0)
            self.mainLayout.addWidget(self.treeB, 0,1)


            # display
            self.center_window()
            self.show()


    # Functions
    # ------------------------------------------------------------------------------
        def center_window(self):
            qr = self.frameGeometry()
            qr.moveCenter(QtGui.QCursor.pos())
            self.move(qr.topLeft())

        def on_context_menu_open(self, pos):

            # pass a varaible such as "TreeA" or "TreeB" which would determine
            # items to be displayed in menu
            self.cmenu_edit.exec_( QtGui.QCursor.pos() )


    # Main
    # ------------------------------------------------------------------------------
    if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        ex = ExampleA()
        sys.exit(app.exec_())

更新 这是我的尝试:

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


# Main
# ------------------------------------------------------------------------------
class ExampleA(QtGui.QWidget):

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

        self.initUI()


    def initUI(self):
        # formatting
        self.resize(550, 400)
        self.setWindowTitle("Factions")

        # widget
        self.treeA = QtGui.QTreeWidget()
        self.treeB = QtGui.QTreeWidget()

        # signals
        self.treeA.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.treeA.customContextMenuRequested.connect(self.on_context_menu_open_a)

        self.treeB.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.treeB.customContextMenuRequested.connect(self.on_context_menu_open_b)

        # layout
        self.mainLayout = QtGui.QGridLayout(self)
        self.mainLayout.setContentsMargins(0,0,0,0)
        self.mainLayout.addWidget(self.treeA, 0,0)
        self.mainLayout.addWidget(self.treeB, 0,1)


        # display
        self.center_window()
        self.show()


# Functions
# ------------------------------------------------------------------------------
    def center_window(self):
        qr = self.frameGeometry()
        qr.moveCenter(QtGui.QCursor.pos())
        self.move(qr.topLeft())

    def mi_add_new_node_clicked(self):
        print "adding item"

    def mi_rename_node_clicked(self):
        print "rename item"

    def prompt_right_click_menu(self, widget, _type):
        cmenu_edit = QtGui.QMenu()
        mi_add_new_node = cmenu_edit.addAction('Add New')
        mi_add_new_node.triggered[()].connect(self.mi_add_new_node_clicked)

        if _type == "B":
                cmenu_edit.addSeparator()
                mi_rename_node = cmenu_edit.addAction('Rename')
                mi_rename_node.triggered[()].connect(self.mi_rename_node_clicked)


        cmenu_edit.exec_( QtGui.QCursor.pos() )

    def on_context_menu_open_a(self, pos):
        self.prompt_right_click_menu( self.treeA , "A")

    def on_context_menu_open_b(self, pos):
        self.prompt_right_click_menu( self.treeB , "B" )



# Main
# ------------------------------------------------------------------------------
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    ex = ExampleA()
    sys.exit(app.exec_())

0 个答案:

没有答案