QListWidget是一个用于显示数据的列表,每个条目中都是一个QListWidgetItem对象;QListWidget可以设置多选;
QListWidget的功能类似于:
C#里面的listbox控件
易语言里面的列表框组件
MFC里面List Box控件
常用方法如下:
常用信号:
添加一行:
self.listWidget.addItem("11111")
self.listWidget.addItem("22222")
self.listWidget.addItem("33333")
self.listWidget.addItem("44444")
指定行插入数据
self.listWidget.insertItem(1, 'aaaaa')
清空所有行
self.listWidget.clear()
删除当前行
item = self.listWidget.takeItem(self.listWidget.currentRow())
del item
删除指定行
item = self.listWidget.takeItem(1)
del item
修改某一行:
self.listWidget.item(1).setText('cccc')
添加右键菜单
self.listWidget.setContextMenuPolicy(3)
self.listWidget.customContextMenuRequested[QPoint].connect(self.listWidgetContext)
def listWidgetContext(self, point):
popMenu = QMenu()
add_action = QAction("添加", popMenu)
add_action.triggered.connect(self.add)
popMenu.addAction(add_action)
popMenu.addAction("修改")
popMenu.addAction("删除")
popMenu.exec_(QCursor.pos())
def add(self):
pass
统计有多少行:
count = self.listWidget.count()
print(count)
触发事件请看下面完整测试代码:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.7
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QStringListModel, Qt, QPoint
from PyQt5.QtGui import QCursor
from PyQt5.QtWidgets import QMenu, QAction, QListView, QAbstractItemView
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(170, 50, 391, 401))
self.listWidget.setObjectName("listWidget")
self.listWidget.setSelectionMode(QAbstractItemView.ExtendedSelection) # 设置选择模式:多选
self.listWidget.setSelectionBehavior(QAbstractItemView.SelectRows) # 选中行为:选中整行
self.listWidget.setViewMode(QListView.ListMode) # 列表形式展示数据
# 添加一行
self.listWidget.addItem("11111")
self.listWidget.addItem("22222")
self.listWidget.addItem("33333")
self.listWidget.addItem("44444")
# 指定行插入数据
self.listWidget.insertItem(1, 'aaaaa')
# #清空所有行
# self.listWidget.clear()
# # 删除当前项
# item = self.listWidget.takeItem(self.listWidget.currentRow())
# del item
# 删除指定行,比如第2行
item = self.listWidget.takeItem(1)
del item
#修改某一行
self.listWidget.item(1).setText('cccc')
#统计有多少行
count = self.listWidget.count()
print(count)
# 项目被单击事件
self.listWidget.itemClicked.connect(self.clicked) # 项目被单击
self.listWidget.clicked.connect(self.listWidget_clicked) # 控件被单击
# self.listWidget.doubleClicked.connect(self.listWidget_clicked)#控件被双击
# 添加右键菜单
self.listWidget.setContextMenuPolicy(3)
self.listWidget.customContextMenuRequested[QPoint].connect(self.listWidgetContext)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 30))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def listWidget_clicked(self, index):
print('控件被单击')
print('选择项索引', index.row())
print('选择项目文本为', self.listWidget.currentItem().text())
def clicked(self, index):
print('项目被单击')
index = self.listWidget.currentIndex().row()
print('选择项索引', index)
print('选择项目文本为', self.listWidget.currentItem().text())
def listWidgetContext(self, point):
popMenu = QMenu()
add_action = QAction("添加", popMenu)
add_action.triggered.connect(self.add)
popMenu.addAction(add_action)
popMenu.addAction("修改")
popMenu.addAction("删除")
popMenu.exec_(QCursor.pos())
def add(self):
pass
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())