とこのコードにカスタムコンテキストメニューを追加するための改訂コードを追加しました!改訂されたコードはコメントになります!コードの
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()
残りはすべて同じ
を残しました