# -*- 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()
PyQt给自定义的控件添加右键菜单
最新推荐文章于 2024-05-14 11:02:03 发布