【问题标题】:context menu in QFileDialogQFileDialog 中的上下文菜单
【发布时间】:2017-10-08 23:05:03
【问题描述】:

我想在 QFileDialog 中实现自定义上下文菜单。在下面的代码中,我设法为主窗口创建了一个上下文菜单,但我希望在选择文件时显示该菜单:如何知道 QFileDialog 中的正确小部件我应该应用 setContextMenuPolicy

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

class Window(QWidget):

    def __init__(self):

        QWidget.__init__(self)

        self.myFileDialog = QFileDialog()

        self.myFileDialog.setContextMenuPolicy(Qt.CustomContextMenu)
        self.myFileDialog.customContextMenuRequested.connect(self.openMenu)

        layout = QVBoxLayout()
        layout.addWidget(self.myFileDialog)
        self.setLayout(layout)

        self.action_perso = QAction( "MyOwnMenu", self )
        self.connect( self.action_perso, SIGNAL("triggered()"), self.test )

    def openMenu(self, position):
        menu = QMenu()
        menu.addAction(self.action_perso)
        menu.exec_(self.myFileDialog.mapToGlobal(position))

    def test(self):
        print("coucou")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

【问题讨论】:

    标签: python pyqt pyqt4 contextmenu qfiledialog


    【解决方案1】:

    我找到了一个可能不是最好的解决方案。它依赖于两个元素:

    1. 感谢个人函数objectTree(此处显示但未使用)递归列出QFileDialog 的所有子对象,我确定了正确的小部件,即QTreeView(我理解QTreeView通过尝试依次隐藏所有 QListView 和 QTreeView 小部件是正确的小部件)。因此,我可以通过它的 objectName 和self.findChild(QTreeView, "treeView") 选择它

    2. setContextMenuPolicy( Qt.ActionsContextMenu ) 应用于此QTreeView。我也尝试实现setContextMenuPolicy(Qt.CustomContextMenu),它部分工作:我的菜单确实出现了,但在未被激活的原始菜单下!
      以下是我建议的代码:


    import sys
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    
    class CustomWidget(QFileDialog):
        def __init__(self, parent=None):
            super(CustomWidget,self).__init__(parent)
    
            # fetch the QTreeView in the QFileDialog
            self.myTree = self.findChild(QTreeView, "treeView")
    
            # set the context menu policy to ActionsContextMenu
            self.myTree.setContextMenuPolicy( Qt.ActionsContextMenu )
    
            # Define a new action
            self.action_perso = QAction( "MyOwnMenu", self.myTree )
            self.myTree.addAction( self.action_perso )
    
            # connect this action to a personnal function
            self.connect( self.action_perso, SIGNAL("triggered()"), self.myFunction )
    
        def myFunction(self):
            print("coucou")
    
        def objectTree(self, objet, plan, j):
            """ list recursively all child objects of objet to fetch the right widget """
    
            n = len( objet.children() )
            for i, child in enumerate( objet.children() ):
                #print("\t"*j, end="")
                plan_sup = plan+"."+str(i)
                #print( plan_sup, child )
                if isinstance(child, QTreeView):
                    self.listViews.append(child)
                self.objectTree(child, plan_sup, j+1)  
    
    
    class MainWidget(QWidget):
    
        def __init__(self, parent=None):
            super(MainWidget,self).__init__(parent)
    
            #creation of main layout 
            mainLayout = QVBoxLayout()
    
            # creation of a widget inside
            self.monWidget = CustomWidget()
            mainLayout.addWidget( self.monWidget )
            self.setLayout( mainLayout )
    
            self.show()
    
    
    app = QApplication(sys.argv)
    window = MainWidget()
    sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-02
      • 2013-03-12
      • 2015-10-09
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多