2017-05-10 1 views
0

QFileDialogにカスタムコンテキストメニューを実装したいと思います。以下のコードでメインウィンドウのコンテキストメニューを作成しましたが、ファイルを選択するとメニューが表示されます:QFileDialogの右ウィジェットを知る方法setContextMenuPolicyを適用する必要がありますか?QFileDialogのコンテキストメニュー

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_()) 

答えて

0

解決策が見つかりましたが、これは最善ではない可能性があります。 QTreeViewすなわち、(私がいることを理解し、QFileDialogの再帰的にすべての子オブジェクトを示しています(ここではなく、使用示す)個人的な機能objectTree

  1. 感謝、私は右のウィジェットを識別:これは、2つの要素に依存していますQTreeViewは、すべてのQListViewウィジェットとQTreeViewウィジェットを連続して隠そうとすることによって、正しいウィジェットです)。したがって、私はself.findChild(QTreeView, "treeView"のobjectNameで選択できます。

  2. のこの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_()) 
:以下
は、私が提案するコードです
関連する問題