2016-04-10 15 views
0

için içerik menüsü nasıl ayarlanır İçerikle doldurulmuş bir QlistView var. Ben "add" veya "remove" gibi seçenekleri göstermek için contextmenu (Sağ tıklama) eklemek için yöntem bilmek istiyorum.Çeşitli yöntemleri denedim, strawt olanlar çoğu QD Widget içindir.Arş, MVC olarak, ben. QlistView() tarafından gitmek zorundaPyQT4: qListView

aşağıdaki denedim ama işe yaramaz.:..

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

fakat yukarıdaki alışkanlık iş zaman ve yardım için şimdiden teşekkür ederiz

cevap

1

bunu ekle setupUi() yöntemine göre:

self.view.customContextMenuRequested.connect(self.onContext) 
Eğer gibi qListView tanımlanmış olmalıydı

gibi nesne özellik: Ben size daha önce bu yüzden devam ettim hakkında bilgi almak için çalışıyorlardı "Off_Strip" programı için bu kullandığınız varsayılmaktadır gidiyorum

self.view = QListView() 
0

ve Söz konusu koda Özel İçerik Menüsü eklemek için gözden geçirilmiş kodu eklendi! Gözden geçirilmiş kod yorumlanacaktır! kod

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

Tüm gerisi aynı

bırakıldı