PyQt5 自定义右键功能

获取实例

textEdit = QTextEdit()

重设右键并绑定信号槽

textEdit.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
textEdit.customContextMenuRequested[QtCore.QPoint].connect(self.myListWidgetContext)

菜单及菜单事件处理

#自定义右键按钮
    def myListWidgetContext(self):
        popMenu = QMenu()
        popMenu.addAction(QAction(u'字体放大', self))
        popMenu.addAction(QAction(u'字体减小', self))
        popMenu.triggered[QAction].connect(self.processtrigger)
        popMenu.exec_(QCursor.pos())

    #右键按钮事件
    def processtrigger(self, q):
        text = self.newTextEdit.toPlainText()
        if not text.strip():
            return
        # 输出那个Qmenu对象被点击
        if q.text() == "字体放大":
            self.fontSize += 1
        elif q.text() == "字体减小":
            self.fontSize -= 1

你可能感兴趣的:(Python,QT5)