第十六篇 -- QListWidget与QToolButton(功能)
阅读原文时间:2023年07月12日阅读:1

效果图:

添加的部分,就是对几个action绑定了槽函数,完成相应的功能。

listWidget操作的都是item,添加一个item,删除一个item,插入一个item等等。那么只需要知道item的几个操作方法。

  • Item = QListWidgetItem()
  • Item.setText("文本内容")
  • Item.setCheckState(Qt.Checked) 勾选
  • Item.setFlags(Qt.ItemIsSelectedable|Qt.ItemIsUserCheckable|Qt.ItemIsEnabled|Qt.ItemIsEditable)可以编辑
  • listWidget.addItem(Item)添加
  • listWidget.insertItem(行号,Item)插入
  • listWidget.takeItem(行号)删除
  • listWidget.clear()清空

ui_listWidget.py

# -*- coding: utf-8 -*-

Form implementation generated from reading ui file 'ui_listwidget.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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 483)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.splitter = QtWidgets.QSplitter(self.centralwidget)
self.splitter.setGeometry(QtCore.QRect(10, 10, 781, 381))
self.splitter.setOrientation(QtCore.Qt.Horizontal)
self.splitter.setObjectName("splitter")
self.toolBox = QtWidgets.QToolBox(self.splitter)
self.toolBox.setMaximumSize(QtCore.QSize(16777215, 16777215))
self.toolBox.setObjectName("toolBox")
self.page = QtWidgets.QWidget()
self.page.setGeometry(QtCore.QRect(0, 0, 152, 291))
self.page.setObjectName("page")
self.gridLayout = QtWidgets.QGridLayout(self.page)
self.gridLayout.setObjectName("gridLayout")
self.btnList_Ini = QtWidgets.QToolButton(self.page)
self.btnList_Ini.setMinimumSize(QtCore.QSize(130, 30))
self.btnList_Ini.setObjectName("btnList_Ini")
self.gridLayout.addWidget(self.btnList_Ini, 0, 0, 1, 1)
self.btnList_Clear = QtWidgets.QToolButton(self.page)
self.btnList_Clear.setMinimumSize(QtCore.QSize(130, 30))
self.btnList_Clear.setObjectName("btnList_Clear")
self.gridLayout.addWidget(self.btnList_Clear, 1, 0, 1, 1)
self.btnList_Insert = QtWidgets.QToolButton(self.page)
self.btnList_Insert.setMinimumSize(QtCore.QSize(130, 30))
self.btnList_Insert.setObjectName("btnList_Insert")
self.gridLayout.addWidget(self.btnList_Insert, 2, 0, 1, 1)
self.btnList_Append = QtWidgets.QToolButton(self.page)
self.btnList_Append.setMinimumSize(QtCore.QSize(130, 30))
self.btnList_Append.setObjectName("btnList_Append")
self.gridLayout.addWidget(self.btnList_Append, 3, 0, 1, 1)
self.btnList_Delete = QtWidgets.QToolButton(self.page)
self.btnList_Delete.setMinimumSize(QtCore.QSize(130, 30))
self.btnList_Delete.setObjectName("btnList_Delete")
self.gridLayout.addWidget(self.btnList_Delete, 4, 0, 1, 1)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("Image/icons/list.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.toolBox.addItem(self.page, icon, "")
self.page_2 = QtWidgets.QWidget()
self.page_2.setGeometry(QtCore.QRect(0, 0, 152, 291))
self.page_2.setObjectName("page_2")
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("Image/icons/tree.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.toolBox.addItem(self.page_2, icon1, "")
self.page_3 = QtWidgets.QWidget()
self.page_3.setObjectName("page_3")
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("Image/icons/tabel.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.toolBox.addItem(self.page_3, icon2, "")
self.tabWidget = QtWidgets.QTabWidget(self.splitter)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.gridLayout_2 = QtWidgets.QGridLayout(self.tab)
self.gridLayout_2.setObjectName("gridLayout_2")
self.btnSel_None = QtWidgets.QToolButton(self.tab)
self.btnSel_None.setObjectName("btnSel_None")
self.gridLayout_2.addWidget(self.btnSel_None, 1, 3, 1, 1)
self.checkBox = QtWidgets.QCheckBox(self.tab)
font = QtGui.QFont()
font.setPointSize(10)
self.checkBox.setFont(font)
self.checkBox.setObjectName("checkBox")
self.gridLayout_2.addWidget(self.checkBox, 0, 6, 1, 1)
self.listWidget = QtWidgets.QListWidget(self.tab)
self.listWidget.setObjectName("listWidget")
item = QtWidgets.QListWidgetItem()
item.setFlags(QtCore.Qt.ItemIsSelectable|QtCore.Qt.ItemIsEditable|QtCore.Qt.ItemIsDragEnabled|QtCore.Qt.ItemIsUserCheckable|QtCore.Qt.ItemIsEnabled)
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
item.setFlags(QtCore.Qt.ItemIsSelectable|QtCore.Qt.ItemIsEditable|QtCore.Qt.ItemIsDragEnabled|QtCore.Qt.ItemIsUserCheckable|QtCore.Qt.ItemIsEnabled)
self.listWidget.addItem(item)
self.gridLayout_2.addWidget(self.listWidget, 2, 0, 1, 7)
self.label = QtWidgets.QLabel(self.tab)
font = QtGui.QFont()
font.setPointSize(10)
self.label.setFont(font)
self.label.setObjectName("label")
self.gridLayout_2.addWidget(self.label, 0, 0, 1, 1)
self.lineEdit = QtWidgets.QLineEdit(self.tab)
self.lineEdit.setObjectName("lineEdit")
self.gridLayout_2.addWidget(self.lineEdit, 0, 1, 1, 5)
self.btnSel_ALL = QtWidgets.QToolButton(self.tab)
self.btnSel_ALL.setObjectName("btnSel_ALL")
self.gridLayout_2.addWidget(self.btnSel_ALL, 1, 2, 1, 1)
self.btnSel_Invs = QtWidgets.QToolButton(self.tab)
self.btnSel_Invs.setObjectName("btnSel_Invs")
self.gridLayout_2.addWidget(self.btnSel_Invs, 1, 4, 1, 1)
self.btnSelectItem = QtWidgets.QToolButton(self.tab)
self.btnSelectItem.setObjectName("btnSelectItem")
self.gridLayout_2.addWidget(self.btnSelectItem, 1, 0, 1, 2)
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.tabWidget.addTab(self.tab_2, "")
self.tab_3 = QtWidgets.QWidget()
self.tab_3.setObjectName("tab_3")
self.tabWidget.addTab(self.tab_3, "")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.toolBar = QtWidgets.QToolBar(MainWindow)
self.toolBar.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
self.toolBar.setObjectName("toolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
self.actList_Ini = QtWidgets.QAction(MainWindow)
icon3 = QtGui.QIcon()
icon3.addPixmap(QtGui.QPixmap("Image/icons/initial.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actList_Ini.setIcon(icon3)
self.actList_Ini.setObjectName("actList_Ini")
self.actList_Clear = QtWidgets.QAction(MainWindow)
icon4 = QtGui.QIcon()
icon4.addPixmap(QtGui.QPixmap("Image/icons/clear.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actList_Clear.setIcon(icon4)
self.actList_Clear.setObjectName("actList_Clear")
self.actList_Insert = QtWidgets.QAction(MainWindow)
icon5 = QtGui.QIcon()
icon5.addPixmap(QtGui.QPixmap("Image/icons/insert.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actList_Insert.setIcon(icon5)
self.actList_Insert.setObjectName("actList_Insert")
self.actList_Append = QtWidgets.QAction(MainWindow)
icon6 = QtGui.QIcon()
icon6.addPixmap(QtGui.QPixmap("Image/icons/append.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actList_Append.setIcon(icon6)
self.actList_Append.setObjectName("actList_Append")
self.actList_Delete = QtWidgets.QAction(MainWindow)
icon7 = QtGui.QIcon()
icon7.addPixmap(QtGui.QPixmap("Image/icons/delete.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actList_Delete.setIcon(icon7)
self.actList_Delete.setObjectName("actList_Delete")
self.actSel_ALL = QtWidgets.QAction(MainWindow)
self.actSel_ALL.setObjectName("actSel_ALL")
self.actSel_None = QtWidgets.QAction(MainWindow)
self.actSel_None.setObjectName("actSel_None")
self.actSel_Invs = QtWidgets.QAction(MainWindow)
self.actSel_Invs.setObjectName("actSel_Invs")
self.actQuit = QtWidgets.QAction(MainWindow)
icon8 = QtGui.QIcon()
icon8.addPixmap(QtGui.QPixmap("Image/icons/quit.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actQuit.setIcon(icon8)
self.actQuit.setObjectName("actQuit")
self.actSelPopMenu = QtWidgets.QAction(MainWindow)
icon9 = QtGui.QIcon()
icon9.addPixmap(QtGui.QPixmap("Image/icons/popmenu.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actSelPopMenu.setIcon(icon9)
self.actSelPopMenu.setObjectName("actSelPopMenu")
self.toolBar.addAction(self.actList_Ini)
self.toolBar.addAction(self.actList_Clear)
self.toolBar.addAction(self.actList_Insert)
self.toolBar.addAction(self.actList_Append)
self.toolBar.addAction(self.actList_Delete)

    self.retranslateUi(MainWindow)  
    self.toolBox.setCurrentIndex(0)  
    self.tabWidget.setCurrentIndex(0)  
    self.actSelPopMenu.triggered.connect(self.actSel\_Invs.trigger)  
    self.actQuit.triggered.connect(MainWindow.close)  
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):  
    \_translate = QtCore.QCoreApplication.translate  
    MainWindow.setWindowTitle(\_translate("MainWindow", "MainWindow"))  
    self.btnList\_Ini.setText(\_translate("MainWindow", "btnList\_Ini"))  
    self.btnList\_Clear.setText(\_translate("MainWindow", "btnList\_Clear"))  
    self.btnList\_Insert.setText(\_translate("MainWindow", "btnList\_Insert"))  
    self.btnList\_Append.setText(\_translate("MainWindow", "btnList\_Append"))  
    self.btnList\_Delete.setText(\_translate("MainWindow", "btnList\_Delete"))  
    self.toolBox.setItemText(self.toolBox.indexOf(self.page), \_translate("MainWindow", "QListWidget操作"))  
    self.toolBox.setItemText(self.toolBox.indexOf(self.page\_2), \_translate("MainWindow", "QTreeWidget"))  
    self.toolBox.setItemText(self.toolBox.indexOf(self.page\_3), \_translate("MainWindow", "QTabelWidget"))  
    self.btnSel\_None.setText(\_translate("MainWindow", "btnSel\_None"))  
    self.checkBox.setText(\_translate("MainWindow", "可编辑"))  
    \_\_sortingEnabled = self.listWidget.isSortingEnabled()  
    self.listWidget.setSortingEnabled(False)  
    item = self.listWidget.item(0)  
    item.setText(\_translate("MainWindow", "New Item"))  
    item = self.listWidget.item(1)  
    item.setText(\_translate("MainWindow", "New Item1"))  
    self.listWidget.setSortingEnabled(\_\_sortingEnabled)  
    self.label.setText(\_translate("MainWindow", "当前项变化"))  
    self.btnSel\_ALL.setText(\_translate("MainWindow", "btnSel\_ALL"))  
    self.btnSel\_Invs.setText(\_translate("MainWindow", "btnSel\_Invs"))  
    self.btnSelectItem.setText(\_translate("MainWindow", "btnSelectItem"))  
    self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), \_translate("MainWindow", "QListWidget"))  
    self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab\_2), \_translate("MainWindow", "QTreeWidget"))  
    self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab\_3), \_translate("MainWindow", "QTableWidget"))  
    self.toolBar.setWindowTitle(\_translate("MainWindow", "toolBar"))  
    self.actList\_Ini.setText(\_translate("MainWindow", "初始化列表"))  
    self.actList\_Ini.setToolTip(\_translate("MainWindow", "初始化列表"))  
    self.actList\_Ini.setShortcut(\_translate("MainWindow", "Ctrl+I"))  
    self.actList\_Clear.setText(\_translate("MainWindow", "清除列表"))  
    self.actList\_Clear.setToolTip(\_translate("MainWindow", "清除列表"))  
    self.actList\_Insert.setText(\_translate("MainWindow", "插入项"))  
    self.actList\_Insert.setToolTip(\_translate("MainWindow", "插入项"))  
    self.actList\_Insert.setShortcut(\_translate("MainWindow", "Ctrl+S"))  
    self.actList\_Append.setText(\_translate("MainWindow", "添加项"))  
    self.actList\_Append.setToolTip(\_translate("MainWindow", "添加项"))  
    self.actList\_Append.setShortcut(\_translate("MainWindow", "Ctrl+A"))  
    self.actList\_Delete.setText(\_translate("MainWindow", "删除当前项"))  
    self.actList\_Delete.setToolTip(\_translate("MainWindow", "删除当前项"))  
    self.actList\_Delete.setShortcut(\_translate("MainWindow", "Del"))  
    self.actSel\_ALL.setText(\_translate("MainWindow", "全选"))  
    self.actSel\_ALL.setToolTip(\_translate("MainWindow", "全选"))  
    self.actSel\_None.setText(\_translate("MainWindow", "全不选"))  
    self.actSel\_None.setToolTip(\_translate("MainWindow", "全不选"))  
    self.actSel\_Invs.setText(\_translate("MainWindow", "反选"))  
    self.actSel\_Invs.setToolTip(\_translate("MainWindow", "反选"))  
    self.actQuit.setText(\_translate("MainWindow", "退出"))  
    self.actQuit.setToolTip(\_translate("MainWindow", "退出程序"))  
    self.actSelPopMenu.setText(\_translate("MainWindow", "项选择"))  
    self.actSelPopMenu.setToolTip(\_translate("MainWindow", "项选择"))

myMainWindow_listWidget.py

#!/usr/bin/env python

_*_ coding: UTF-8 _*_

"""=================================================
@Project -> File : Operate-system -> myMainWindow_listwidget.py
@IDE : PyCharm
@Author : zihan
@Date : 2020/4/14 9:24
@Desc :
================================================="""

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QListWidgetItem, QMenu, QToolButton
from PyQt5.QtGui import QIcon, QCursor
from PyQt5.QtCore import pyqtSlot, Qt
from ui_listwidget import Ui_MainWindow

class QmyMainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)

    self.setCentralWidget(self.ui.splitter)  # 使splitter充满整个工作区  
    self.\_\_set\_actions\_for\_button()  # ToolButton关联Action  
    self.\_\_create\_selection\_pop\_menu()  
    self.\_\_FlagEditable = Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsEditable  
    self.\_\_FlagNotEditable = Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled

    self.ui.listWidget.setContextMenuPolicy(Qt.CustomContextMenu)

    # 初始化列表  
    self.ui.actList\_Ini.triggered.connect(self.do\_act\_list\_ini\_triggered)  
    # 插入一项  
    self.ui.actList\_Insert.triggered.connect(self.do\_act\_list\_insert\_triggered)  
    # 添加一项  
    self.ui.actList\_Append.triggered.connect(self.do\_act\_list\_append\_triggered)  
    # 删除当前项  
    self.ui.actList\_Delete.triggered.connect(self.do\_act\_list\_delete\_triggered)  
    # 清空列表  
    self.ui.actList\_Clear.triggered.connect(self.do\_act\_list\_clear\_triggered)

    # 全选  
    self.ui.actSel\_ALL.triggered.connect(self.do\_act\_sel\_all\_triggered)  
    # 全不选  
    self.ui.actSel\_None.triggered.connect(self.do\_act\_sel\_none\_triggered)  
    # 反选  
    self.ui.actSel\_Invs.triggered.connect(self.do\_act\_sel\_invs\_triggered)  
    # 当选择项发生变化时  
    self.ui.listWidget.currentItemChanged.connect(self.do\_list\_widget\_current\_item\_changed)  
    # 右键快捷菜单  
    self.ui.listWidget.customContextMenuRequested.connect(self.do\_list\_widget\_custom\_context\_menu\_requested)

# ToolButton关联Action  
def \_\_set\_actions\_for\_button(self):  
    self.ui.btnList\_Ini.setDefaultAction(self.ui.actList\_Ini)  
    self.ui.btnList\_Clear.setDefaultAction(self.ui.actList\_Clear)

    self.ui.btnList\_Insert.setDefaultAction(self.ui.actList\_Insert)  
    self.ui.btnList\_Append.setDefaultAction(self.ui.actList\_Append)  
    self.ui.btnList\_Delete.setDefaultAction(self.ui.actList\_Delete)

    self.ui.btnSel\_ALL.setDefaultAction(self.ui.actSel\_ALL)  
    self.ui.btnSel\_None.setDefaultAction(self.ui.actSel\_None)  
    self.ui.btnSel\_Invs.setDefaultAction(self.ui.actSel\_Invs)

    self.ui.btnList\_Ini.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)  
    self.ui.btnList\_Clear.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)  
    self.ui.btnList\_Insert.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)  
    self.ui.btnList\_Append.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)  
    self.ui.btnList\_Delete.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)

# 创建ToolButton按钮的下拉菜单  
def \_\_create\_selection\_pop\_menu(self):  
    menuSelection = QMenu(self)  # 下拉菜单  
    menuSelection.addAction(self.ui.actSel\_ALL)  
    menuSelection.addAction(self.ui.actSel\_None)  
    menuSelection.addAction(self.ui.actSel\_Invs)

    # listWidget上方的btnSelectItem按钮  
    self.ui.btnSelectItem.setPopupMode(QToolButton.MenuButtonPopup)  
    self.ui.btnSelectItem.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)  
    self.ui.btnSelectItem.setDefaultAction(self.ui.actSelPopMenu)  
    self.ui.btnSelectItem.setMenu(menuSelection)  # 设置下拉菜单

    # 工具栏上的下拉式菜单按钮  
    toolBtn = QToolButton(self)  
    toolBtn.setPopupMode(QToolButton.InstantPopup)  
    toolBtn.setDefaultAction(self.ui.actSelPopMenu)  
    toolBtn.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)  
    toolBtn.setMenu(menuSelection)  # 设置下拉菜单  
    self.ui.toolBar.addWidget(toolBtn)

    # 工具栏添加分隔条和"退出"按钮  
    self.ui.toolBar.addSeparator()  
    self.ui.toolBar.addAction(self.ui.actQuit)

# 初始化列表  
def do\_act\_list\_ini\_triggered(self):  
    editable = self.ui.checkBox.isChecked()  
    if editable:  
        Flag = self.\_\_FlagEditable  # 可编辑  
    else:  
        Flag = self.\_\_FlagNotEditable  # 不可编辑  
    self.ui.listWidget.clear()  # 清除列表  
    for i in range(10):  
        itemStr = "Item %d" % i  
        aItem = QListWidgetItem()  
        aItem.setText(itemStr)  
        aItem.setCheckState(Qt.Checked)  
        aItem.setFlags(Flag)  # 项的Flag  
        self.ui.listWidget.addItem(aItem)

# 插入一项  
def do\_act\_list\_insert\_triggered(self):  
    editable = self.ui.checkBox.isChecked()  
    if editable:  
        Flag = self.\_\_FlagEditable  
    else:  
        Flag = self.\_\_FlagNotEditable  
    aItem = QListWidgetItem()  
    aItem.setText("Insert Item")  
    aItem.setCheckState(Qt.Checked)  
    aItem.setFlags(Flag)  
    curRow = self.ui.listWidget.currentRow()  # 当前行  
    self.ui.listWidget.insertItem(curRow, aItem)

# 添加一项  
def do\_act\_list\_append\_triggered(self):  
    editable = self.ui.checkBox.isChecked()  
    if editable:  
        Flag = self.\_\_FlagEditable  
    else:  
        Flag = self.\_\_FlagNotEditable  
    aItem = QListWidgetItem()  
    aItem.setText("Append Item")  
    aItem.setCheckState(Qt.Checked)  
    aItem.setFlags(Flag)  
    self.ui.listWidget.addItem(aItem)

# 删除当前项  
def do\_act\_list\_delete\_triggered(self):  
    row = self.ui.listWidget.currentRow()  
    self.ui.listWidget.takeItem(row)  # 移出当前项,Python自动删除

# 清空列表  
def do\_act\_list\_clear\_triggered(self):  
    self.ui.listWidget.clear()

# 全选  
def do\_act\_sel\_all\_triggered(self):  
    for i in range(self.ui.listWidget.count()):  
        aItem = self.ui.listWidget.item(i)  
        aItem.setCheckState(Qt.Checked)

# 全不选  
def do\_act\_sel\_none\_triggered(self):  
    for i in range(self.ui.listWidget.count()):  
        aItem = self.ui.listWidget.item(i)  
        aItem.setCheckState(Qt.Unchecked)

# 反选  
def do\_act\_sel\_invs\_triggered(self):  
    for i in range(self.ui.listWidget.count()):  
        aItem = self.ui.listWidget.item(i)  
        if aItem.checkState() != Qt.Checked:  
            aItem.setCheckState(Qt.Checked)  
        else:  
            aItem.setCheckState(Qt.Unchecked)

# 当选择项发生变化时  
def do\_list\_widget\_current\_item\_changed(self, current, previous):  
    strInfo = ""  
    if current is not None:  
        if previous is None:  
            strInfo = "当前:" + current.text()  
        else:  
            strInfo = "前一项:" + previous.text() + ";当前项:" + current.text()  
    self.ui.lineEdit.setText(strInfo)

# 右键快捷菜单  
def do\_list\_widget\_custom\_context\_menu\_requested(self, pos):  
    print("aaa")  
    menuList = QMenu(self)  # 创建菜单  
    menuList.addAction(self.ui.actList\_Ini)  
    menuList.addAction(self.ui.actList\_Clear)  
    menuList.addAction(self.ui.actList\_Insert)  
    menuList.addAction(self.ui.actList\_Append)  
    menuList.addAction(self.ui.actList\_Delete)  
    menuList.addSeparator()  
    menuList.addAction(self.ui.actSel\_ALL)  
    menuList.addAction(self.ui.actSel\_None)  
    menuList.addAction(self.ui.actSel\_Invs)  
    menuList.exec(QCursor.pos())  # 显示菜单

if __name__ == "__main__":
app = QApplication(sys.argv) # 创建app,用QApplication类
form = QmyMainWindow()
form.show()
sys.exit(app.exec_())

值得注意的是,如果遇到了添加右键快捷菜单时,在组件上单击右键没有触发信号的话,考虑没有添加右键菜单策略,这时候在__init__()函数中添加一句self.ui.listWidget.setContextMenuPolicy(Qt.CustomContextMenu)。

问题参考链接:https://blog.csdn.net/Wangguang_/article/details/91550394

ok.

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章