2016-04-10 10 views
0

私はコンテンツを取り込むQlistViewを持っています。私はコンテキストメニューを追加する方法を知りたいのですが(右クリック)、 "add"や "remove"のようなオプションを表示したいのですが、いろいろな方法を試しました。 QlistView()で行かなければならないPyQT4:qListViewのコンテキストメニューを取得する方法

は、私は、次の試してみましたが、それは動作しません:。。。

def setupUi(self): 
    QtCore.Qt.view.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) 
    QtCore.Qt.view.connect(QtCore.Qt.view, QtCore.SIGNAL("customContextMenuRequested(QPoint)", self.onContext)) 

def onContext(self): 
    # Create a menu 
    menu = QtGui.QMenu("Menu", self) 
    menu.addAction(self.mAction1) 
    menu.addAction(self.mAction2) 
    # Show the context menu. 
    menu.exec_(QtCore.Qt.view.mapToGlobal(point)) 

しかし、上記の文句を言わない仕事あなたの時間と助けを事前に感謝

答えて

1

これを追加setupUi()方法:

self.view.customContextMenuRequested.connect(self.onContext) 
あなたのようにあなたのqListViewを定義しておく必要があり

のようなオブジェクト属性:私はあなたが以前のように、私は先に行っに関するヘルプをしようとしていた「Off_Strip」プログラムのためにこれを使用していると仮定するつもりです

self.view = QListView() 
0

とこのコードにカスタムコンテキストメニューを追加するための改訂コードを追加しました!改訂されたコードはコメントになります!コードの

class Ui_Form(object): 
    def setupUi(self, Form): 
     self.Form = Form #Created a referance to the form object and edited all code to reflect this 
     self.Form.setObjectName(_fromUtf8("Form")) 
     self.Form.resize(518, 349) 
     self.Form.setContextMenuPolicy(Qtore.Qt.CustomContextMenu) ################## Self explanitory 
     self.Form.customContextMenuRequested.connect(self.openMenu) #################### again... 
     self.label = QtGui.QLabel(Form) 
     self.label.setGeometry(QtCore.QRect(40, 40, 141, 16)) 
     self.label.setObjectName(_fromUtf8("label")) 
     self.label_2 = QtGui.QLabel(Form) 
     self.label_2.setGeometry(QtCore.QRect(400, 330, 141, 16)) 
     self.label_2.setObjectName(_fromUtf8("label_2")) 
     self.horizontalLayoutWidget = QtGui.QWidget(Form) 
     self.horizontalLayoutWidget.setGeometry(QtCore.QRect(70, 200, 381, 31)) 
     self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget")) 
     self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget) 
     self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout")) 
     self.textEdit = QtGui.QTextEdit(self.horizontalLayoutWidget) 
     self.textEdit.setObjectName(_fromUtf8("textEdit")) 
     self.horizontalLayout.addWidget(self.textEdit) 
     self.pushButton = QtGui.QPushButton(self.horizontalLayoutWidget) 
     self.pushButton.setObjectName(_fromUtf8("pushButton")) 
     self.horizontalLayout.addWidget(self.pushButton) 
     self.pushButton_2 = QtGui.QPushButton(Form) 
     self.pushButton_2.setGeometry(QtCore.QRect(230, 270, 75, 23)) 
     self.pushButton_2.setObjectName(_fromUtf8("pushButton_2")) 

     self.retranslateUi(self.Form) 
     QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.file_open) 
     QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("clicked()")), self.check_start) 
     QtCore.QMetaObject.connectSlotsByName(self.Form) 

    def openMenu(self, position): ################### Code for custom menu, this is the default for a QUIT menu 
     menu = QtGui.QMenu() 
     quitAction = menu.addAction("Quit") 
     action = menu.exec_(self.Form.mapToGlobal(position)) 
     if action == quitAction: 
      QtGui.qApp.quit() 

残りはすべて同じ

を残しました
関連する問題