PyQt给自定义的控件添加右键菜单

# -*- coding: utf-8 -*-
# !/usr/bin/env python
import os, sys
from Qt import QtCore, QtGui, QtWidgets, QtCompat
#定义自定义的控件跟右键菜单
class myWid(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(myWid, self).__init__(parent)
        
        self.create_UI()
        #QWidget 自带的默认信号与右键菜单的customContextMenuRequested
        
        self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)#使用customContextMenuRequested 必须要设置这行代码
        
        self.customContextMenuRequested.connect(self.showMenu)
        
    def create_UI(self):

        self.verticalLayout = QtWidgets.QVBoxLayout(self)
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label_fill_light_RotateY = QtWidgets.QLabel(self)
        self.label_fill_light_RotateY.setMinimumSize(QtCore.QSize(200, 30))
        self.label_fill_light_RotateY.setObjectName("label_fill_light_RotateY")
        self.horizontalLayout.addWidget(self.label_fill_light_RotateY)
        self.lineEdit_fill_light_RotateY = QtWidgets.QLineEdit(self)
        self.lineEdit_fill_light_RotateY.setMinimumSize(QtCore.QSize(70, 30))
        self.lineEdit_fill_light_RotateY.setObjectName("lineEdit_fill_light_RotateY")
        self.horizontalLayout.addWidget(self.lineEdit_fill_light_RotateY)
        self.horizontalSlider_fill_light_RotateY = QtWidgets.QSlider(self)
        self.horizontalSlider_fill_light_RotateY.setMinimumSize(QtCore.QSize(300, 0))
        self.horizontalSlider_fill_light_RotateY.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider_fill_light_RotateY.setObjectName("horizontalSlider_fill_light_RotateY")
        self.horizontalLayout.addWidget(self.horizontalSlider_fill_light_RotateY)
        self.verticalLayout.addLayout(self.horizontalLayout)
        
        self.label_fill_light_RotateY.setText("fill_light_RotateY")
        self.lineEdit_fill_light_RotateY.setProperty("objAttr", "fill_light_RotateY")

	#自定义菜单函数
    def showMenu(self, pos):
       
        self.menu = QtWidgets.QMenu(self)

        action_add = QtWidgets.QAction(self.menu)
        action_add.setText("Add")
        action_del = QtWidgets.QAction(self.menu)
        action_del.setText("Delete")
        self.menu.addAction(action_add)
        self.menu.addAction(action_del)
    
        self.menu.move(QtGui.QCursor.pos())
        self.menu.show()

#主窗口

class test_01(QtWidgets.QMainWindow):
    
    def __init__(self, parent=None):
        super(test_01, self).__init__(parent)

        self.create_UI()
        self.setWindowTitle("Tool Name")  # 窗口名字

    def create_UI(self):
        self.ui = self
        self.ui.centralwidget = QtWidgets.QWidget(self)
        self.ui.main_layout = QtWidgets.QVBoxLayout(self.ui.centralwidget)
        wid1 = myWid(self)
        wid2 = myWid(self)

        self.ui.main_layout.addWidget(wid1)
        self.ui.main_layout.addWidget(wid2)
        self.ui.setCentralWidget(self.ui.centralwidget)
def main():
    app = QtWidgets.QApplication(sys.argv)
    try:
        handle.close()
        handle.deleteLater()
    except Exception:
        pass
    handle = test_01()
    handle.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值