PyQT QListWidget 列表项添加、删除与编辑

PyQT开发图形界面的应用时,QListWidget控件类用于生成列表,可以方便地以可视化方式对列表项进行选择,添加,删除,编辑操作。

如果要处理的数据是2维结构,如2维数组, Pandas DataFrame, 嵌套列表等, 请参考文章:PyQt QTableWidget 表格控件的使用
如果需要可视化处理数据库数据,请参考文章:PyQT Model/View编程:数据库增删改查(CRUD)操作的可视化

1. QListWidget 代码实现步骤:

  1. 生成QListWidget对象
  2. 构建 QListWidgetItem 列表项目对象
  3. 对列表项进行添加,删除,修改操作等

2. 准备 PyQT 演示代码框架

在本例中,将建立 1个窗口,包含4个控件:

  • 1个QListWidget对象,
  • 1个输入控件;
  • 1个添加列表项按钮;
  • 1个删除列表项按钮

准备1个演示代码框架

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

import sys
 
W_WIDTH = 400
W_HEIGHT = 400
W_X = 725
W_Y = 350
 
 
class CreateButton:
    def __init__(self, window, text, width, height, x, y):
        self.button = QPushButton(window)
        self.button.setText(text)
        self.button.resize(width, height)
        self.button.move(x, y)
 
    def get_instance(self):
        return self.button
 
 
class MyWin(QMainWindow):
    def __init__(self):
        super(MyWin, self).__init__()
        self.init()
        self.show()
 
    def writetextbox(self):
        self.textbox.setPlainText("test")
 
    def textboxcreate(self):
        self.textbox = QPlainTextEdit(self)
        self.textbox.move(150,25)
        self.textbox.resize(75, 25)
 
    def init(self):
        self.resize(W_WIDTH, W_HEIGHT)
        self.move(W_X, W_Y)
        self.setWindowTitle('QlistWidget Example')
        
        self.textbox = QPlainTextEdit(self)
        self.textbox.move(145,5)
        self.textbox.resize(125, 25)
 
        self.button_add_item = CreateButton(self, "Add Item", 120, 25, 275, 5).get_instance()
        self.button_remove_item = CreateButton(self, "Remove Item", 250, 25, 145, 35).get_instance()


 
app = QApplication(sys.argv)
win = MyWin()
sys.exit(app.exec_())

##3. QListWidget 代码实现

3.1 创建 QListWidget对象

self.mylist = QListWidget(self)

3.2 添加列表项

新生成的列表是空的。 列表项数据为 QListWidgetItem类的对象
用addItem()方法向列表中添加列表项,
list_item1 = QListWidgetItem(“Pear”)
self.mylist.addItem(list_item1)

在本例中,列表项内容通过 textEdit 输入框输入,单击 button_add_item 按钮后,将输入内容添加至列表中,如果输入框为空,会弹出提示框报错。
将 button_add_item按钮的 clicked 信号绑定到 slot 方法 addItem()

self.button_add_item.clicked.connect(self.additem)

def additem(self):
        if self.textbox.toPlainText() == "" or self.textbox.toPlainText() is None:
            msgbox = QMessageBox()
            msgbox.setIcon(QMessageBox.Critical)
            msgbox.setText("Item cannot be empty")
            msgbox.setWindowTitle("Error")
            msgbox.setStandardButtons(QMessageBox.Ok)
            msgbox.exec()
        else:
            self.mylist.addItem(QListWidgetItem(self.textbox.toPlainText()))
            self.textbox.setPlainText(None)
               

3.3 删除列表项

选中1个列表项后,单击按钮 button_remove_item ,将删除该列表项
将 button_remove_item 按钮的 clicked 信号绑定到 slot 方法 delItem()

 self.button_remove_item.clicked.connect(self.delitem)
     def delitem(self):
            selected_row = self.mylist.currentRow()
            item = self.mylist.takeItem(selected_row)
            del item

3.4 修改列表项

本例可通过双击列表项对其进行修改。 该列表项的flags就先置为Qt.ItemIsEditable。
将QListWidget对象的itemDoubleClicked 信号绑定至 DoubleClicked_to_edit 方法

 self.mylist.itemDoubleClicked.connect(self.DoubleClicked_to_edit)
    def DoubleClicked_to_edit(self, item):
        item.setFlags(item.flags() | Qt.ItemIsEditable)
        if not item.isSelected():
            item.setSelected(True)

3.5 运行项目

PyQT QListWidget 列表项添加、删除与编辑_第1张图片

4. 完整代码

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

import sys
 
W_WIDTH = 400
W_HEIGHT = 400
W_X = 725
W_Y = 350
 
 
class CreateButton:
    def __init__(self, window, text, width, height, x, y):
        self.button = QPushButton(window)
        self.button.setText(text)
        self.button.resize(width, height)
        self.button.move(x, y)
 
    def get_instance(self):
        return self.button
 
 
class MyWin(QMainWindow):
    def __init__(self):
        super(MyWin, self).__init__()
        self.init()
        self.show()
 
    def writetextbox(self):
        self.textbox.setPlainText("test")
 
    def textboxcreate(self):
        self.textbox = QPlainTextEdit(self)
        self.textbox.move(150,25)
        self.textbox.resize(75, 25)
 
    def init(self):
        self.resize(W_WIDTH, W_HEIGHT)
        self.move(W_X, W_Y)
        self.setWindowTitle('QlistWidget Example')
 
        self.mylist = QListWidget(self)
        self.mylist.setGeometry(5,5,135,250)
 
 
        self.textbox = QPlainTextEdit(self)
        self.textbox.move(145,5)
        self.textbox.resize(125, 25)
 
        self.button_add_item = CreateButton(self, "Add Item", 120, 25, 275, 5).get_instance()
        self.button_add_item.clicked.connect(self.additem)
 
        self.button_remove_item = CreateButton(self, "Remove Item", 250, 25, 145, 35).get_instance()
        self.button_remove_item.clicked.connect(self.delitem)

        self.mylist.itemDoubleClicked.connect(self.DoubleClicked_to_edit)
        
    def additem(self):
        if self.textbox.toPlainText() == "" or self.textbox.toPlainText() is None:
            msgbox = QMessageBox()
            msgbox.setIcon(QMessageBox.Critical)
            msgbox.setText("Item cannot be empty")
            msgbox.setWindowTitle("Error")
            msgbox.setStandardButtons(QMessageBox.Ok)
            msgbox.exec()
        else:
            self.mylist.addItem(QListWidgetItem(self.textbox.toPlainText()))
            self.textbox.setPlainText(None)
                
    def delitem(self):
            selected_row = self.mylist.currentRow()
            item = self.mylist.takeItem(selected_row)
            del item
 
    def DoubleClicked_to_edit(self, item):
        item.setFlags(item.flags() | Qt.ItemIsEditable)
        if not item.isSelected():
            item.setSelected(True)
            
 
app = QApplication(sys.argv)
win = MyWin()
sys.exit(app.exec_())

上述代码支持PyQT5, PyQT6 版本。

你可能感兴趣的:(PyQt,pyqt,python,开发语言)