效果图:
自己画一个ui,如图,前面已经讲过怎么用Qt Designer画图了,现在就不赘述了。
ui_play.py
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'ui_play2.ui' # # Created by: PyQt5 UI code generator 5.13.0 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(422, 375) self.treeWidget = QtWidgets.QTreeWidget(Form) self.treeWidget.setGeometry(QtCore.QRect(10, 10, 401, 351)) self.treeWidget.setObjectName("treeWidget") self.treeWidget.headerItem().setTextAlignment(0, QtCore.Qt.AlignCenter) font = QtGui.QFont() font.setPointSize(14) font.setBold(True) font.setWeight(75) self.treeWidget.headerItem().setFont(0, font) self.treeWidget.headerItem().setBackground(0, QtGui.QColor(170, 255, 0)) brush = QtGui.QBrush(QtGui.QColor(170, 85, 255)) brush.setStyle(QtCore.Qt.SolidPattern) self.treeWidget.headerItem().setForeground(0, brush) self.treeWidget.headerItem().setTextAlignment(1, QtCore.Qt.AlignCenter) font = QtGui.QFont() font.setPointSize(14) font.setBold(True) font.setWeight(75) self.treeWidget.headerItem().setFont(1, font) self.treeWidget.headerItem().setBackground(1, QtGui.QColor(170, 255, 127)) brush = QtGui.QBrush(QtGui.QColor(170, 85, 255)) brush.setStyle(QtCore.Qt.SolidPattern) self.treeWidget.headerItem().setForeground(1, brush) item_0 = QtWidgets.QTreeWidgetItem(self.treeWidget) brush = QtGui.QBrush(QtGui.QColor(85, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) item_0.setBackground(0, brush) brush = QtGui.QBrush(QtGui.QColor(85, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) item_0.setBackground(1, brush) item_1 = QtWidgets.QTreeWidgetItem(item_0) item_1 = QtWidgets.QTreeWidgetItem(item_0) item_1 = QtWidgets.QTreeWidgetItem(item_0) item_0 = QtWidgets.QTreeWidgetItem(self.treeWidget) brush = QtGui.QBrush(QtGui.QColor(85, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) item_0.setBackground(0, brush) brush = QtGui.QBrush(QtGui.QColor(85, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) item_0.setBackground(1, brush) item_1 = QtWidgets.QTreeWidgetItem(item_0) item_1 = QtWidgets.QTreeWidgetItem(item_0) item_1 = QtWidgets.QTreeWidgetItem(item_0) item_0 = QtWidgets.QTreeWidgetItem(self.treeWidget) brush = QtGui.QBrush(QtGui.QColor(85, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) item_0.setBackground(0, brush) brush = QtGui.QBrush(QtGui.QColor(85, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) item_0.setBackground(1, brush) item_1 = QtWidgets.QTreeWidgetItem(item_0) item_1 = QtWidgets.QTreeWidgetItem(item_0) item_1 = QtWidgets.QTreeWidgetItem(item_0) item_0 = QtWidgets.QTreeWidgetItem(self.treeWidget) brush = QtGui.QBrush(QtGui.QColor(85, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) item_0.setBackground(0, brush) brush = QtGui.QBrush(QtGui.QColor(85, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) item_0.setBackground(1, brush) item_1 = QtWidgets.QTreeWidgetItem(item_0) item_1 = QtWidgets.QTreeWidgetItem(item_0) item_1 = QtWidgets.QTreeWidgetItem(item_0) item_0 = QtWidgets.QTreeWidgetItem(self.treeWidget) brush = QtGui.QBrush(QtGui.QColor(85, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) item_0.setBackground(0, brush) brush = QtGui.QBrush(QtGui.QColor(85, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) item_0.setBackground(1, brush) item_1 = QtWidgets.QTreeWidgetItem(item_0) item_1 = QtWidgets.QTreeWidgetItem(item_0) item_1 = QtWidgets.QTreeWidgetItem(item_0) item_0 = QtWidgets.QTreeWidgetItem(self.treeWidget) brush = QtGui.QBrush(QtGui.QColor(85, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) item_0.setBackground(0, brush) brush = QtGui.QBrush(QtGui.QColor(85, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) item_0.setBackground(1, brush) item_1 = QtWidgets.QTreeWidgetItem(item_0) item_1 = QtWidgets.QTreeWidgetItem(item_0) item_1 = QtWidgets.QTreeWidgetItem(item_0) item_0 = QtWidgets.QTreeWidgetItem(self.treeWidget) brush = QtGui.QBrush(QtGui.QColor(85, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) item_0.setBackground(0, brush) brush = QtGui.QBrush(QtGui.QColor(85, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) item_0.setBackground(1, brush) item_1 = QtWidgets.QTreeWidgetItem(item_0) item_1 = QtWidgets.QTreeWidgetItem(item_0) item_1 = QtWidgets.QTreeWidgetItem(item_0) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.treeWidget.headerItem().setText(0, _translate("Form", "姓名")) self.treeWidget.headerItem().setText(1, _translate("Form", "信息")) __sortingEnabled = self.treeWidget.isSortingEnabled() self.treeWidget.setSortingEnabled(False) self.treeWidget.topLevelItem(0).setText(0, _translate("Form", "张三")) self.treeWidget.topLevelItem(0).child(0).setText(0, _translate("Form", "性别")) self.treeWidget.topLevelItem(0).child(0).setText(1, _translate("Form", "男")) self.treeWidget.topLevelItem(0).child(1).setText(0, _translate("Form", "年龄")) self.treeWidget.topLevelItem(0).child(1).setText(1, _translate("Form", "18")) self.treeWidget.topLevelItem(0).child(2).setText(0, _translate("Form", "爱好")) self.treeWidget.topLevelItem(0).child(2).setText(1, _translate("Form", "LOL")) self.treeWidget.topLevelItem(1).setText(0, _translate("Form", "李四")) self.treeWidget.topLevelItem(1).child(0).setText(0, _translate("Form", "性别")) self.treeWidget.topLevelItem(1).child(0).setText(1, _translate("Form", "男")) self.treeWidget.topLevelItem(1).child(1).setText(0, _translate("Form", "年龄")) self.treeWidget.topLevelItem(1).child(1).setText(1, _translate("Form", "20")) self.treeWidget.topLevelItem(1).child(2).setText(0, _translate("Form", "爱好")) self.treeWidget.topLevelItem(1).child(2).setText(1, _translate("Form", "DOTA")) self.treeWidget.topLevelItem(2).setText(0, _translate("Form", "王五")) self.treeWidget.topLevelItem(2).child(0).setText(0, _translate("Form", "性别")) self.treeWidget.topLevelItem(2).child(0).setText(1, _translate("Form", "男")) self.treeWidget.topLevelItem(2).child(1).setText(0, _translate("Form", "年龄")) self.treeWidget.topLevelItem(2).child(1).setText(1, _translate("Form", "22")) self.treeWidget.topLevelItem(2).child(2).setText(0, _translate("Form", "爱好")) self.treeWidget.topLevelItem(2).child(2).setText(1, _translate("Form", "麻将")) self.treeWidget.topLevelItem(3).setText(0, _translate("Form", "芳芳")) self.treeWidget.topLevelItem(3).child(0).setText(0, _translate("Form", "性别")) self.treeWidget.topLevelItem(3).child(0).setText(1, _translate("Form", "女")) self.treeWidget.topLevelItem(3).child(1).setText(0, _translate("Form", "年龄")) self.treeWidget.topLevelItem(3).child(1).setText(1, _translate("Form", "20")) self.treeWidget.topLevelItem(3).child(2).setText(0, _translate("Form", "爱好")) self.treeWidget.topLevelItem(3).child(2).setText(1, _translate("Form", "看书")) self.treeWidget.topLevelItem(4).setText(0, _translate("Form", "小智")) self.treeWidget.topLevelItem(4).child(0).setText(0, _translate("Form", "性别")) self.treeWidget.topLevelItem(4).child(0).setText(1, _translate("Form", "男")) self.treeWidget.topLevelItem(4).child(1).setText(0, _translate("Form", "年龄")) self.treeWidget.topLevelItem(4).child(1).setText(1, _translate("Form", "25")) self.treeWidget.topLevelItem(4).child(2).setText(0, _translate("Form", "爱好")) self.treeWidget.topLevelItem(4).child(2).setText(1, _translate("Form", "解说")) self.treeWidget.topLevelItem(5).setText(0, _translate("Form", "婷婷")) self.treeWidget.topLevelItem(5).child(0).setText(0, _translate("Form", "性别")) self.treeWidget.topLevelItem(5).child(0).setText(1, _translate("Form", "女")) self.treeWidget.topLevelItem(5).child(1).setText(0, _translate("Form", "年龄")) self.treeWidget.topLevelItem(5).child(1).setText(1, _translate("Form", "27")) self.treeWidget.topLevelItem(5).child(2).setText(0, _translate("Form", "爱好")) self.treeWidget.topLevelItem(5).child(2).setText(1, _translate("Form", "健身")) self.treeWidget.topLevelItem(6).setText(0, _translate("Form", "大熊")) self.treeWidget.topLevelItem(6).child(0).setText(0, _translate("Form", "性别")) self.treeWidget.topLevelItem(6).child(0).setText(1, _translate("Form", "男")) self.treeWidget.topLevelItem(6).child(1).setText(0, _translate("Form", "年龄")) self.treeWidget.topLevelItem(6).child(1).setText(1, _translate("Form", "14")) self.treeWidget.topLevelItem(6).child(2).setText(0, _translate("Form", "爱好")) self.treeWidget.topLevelItem(6).child(2).setText(1, _translate("Form", "睡觉")) self.treeWidget.setSortingEnabled(__sortingEnabled)
play_py
#!/usr/bin/env python # _*_ coding: UTF-8 _*_ """================================================= @Project -> File : FDS -> play2.py @IDE : PyCharm @Author : zihan @Date : 2020/5/13 10:41 @Desc : =================================================""" import sys from PyQt5.QtWidgets import QApplication, QWidget, QMenu, QMessageBox from PyQt5.QtCore import Qt from ui_play2 import Ui_Form class QmyWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) # 调用父类构造函数 self.ui = Ui_Form() # 创建UI对象 self.ui.setupUi(self) # 构造UI self.ui.treeWidget.expandAll() self.ui.treeWidget.setColumnWidth(0, 200) self.ui.treeWidget.setStyleSheet("QHeaderView::section{background:rgb(85, 170, 127);}") self.ui.treeWidget.setContextMenuPolicy(Qt.CustomContextMenu) self.ui.treeWidget.customContextMenuRequested.connect(self.do_custom_context_menu_request) def do_custom_context_menu_request(self, pos): # row_num = -1 # 当前节点的行号 parent_num = -1 # 存放当前行的父节点的行号 for i in self.ui.treeWidget.selectionModel().selection().indexes(): # row_num = i.row() parent_num = i.parent().row() # print(parent_num) # print(row_num) if parent_num == -1: pass else: menu = QMenu() item1 = menu.addAction(u"选项一") item2 = menu.addAction(u"选项二") item3 = menu.addAction(u"选项三") action = menu.exec_(self.ui.treeWidget.mapToGlobal(pos)) if action == item1: QMessageBox.information(self, "消息框标题", "你选择了:选项一", QMessageBox.Yes | QMessageBox.No) print("you select 1") elif action == item2: QMessageBox.information(self, "消息框标题", "你选择了:选项二", QMessageBox.Yes | QMessageBox.No) print("you select 2") elif action == item3: QMessageBox.information(self, "消息框标题", "你选择了:选项三", QMessageBox.Yes | QMessageBox.No) print("you select 3") else: return if __name__ == '__main__': app = QApplication(sys.argv) # 创建app form = QmyWidget() form.show() sys.exit(app.exec_())
这就可以实现右击菜单了。关键是有两句话
self.ui.treeWidget.setContextMenuPolicy(Qt.CustomContextMenu) # 允许右击菜单 self.ui.treeWidget.customContextMenuRequested.connect(self.do_custom_context_menu_request) # 右击时触发函数
这是关键。