PyQt5 自定义右键功能

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ren365880/article/details/83014722

获取实例

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

猜你喜欢

转载自blog.csdn.net/ren365880/article/details/83014722