PyQT5之QListWidget

实例1

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QMainWindow, QMessageBox, QApplication, QWidget, QVBoxLayout, QPushButton, QListWidget, QListWidgetItem


class ListWidgetDemo(QMainWindow):

    def __init__(self, parent=None):
        super().__init__(parent)  # 调用父类的构造函数,创建QWidget窗体
        self.setWindowTitle("QListWidget 例子")
        self.resize(300, 270)
        self.listwidget = QListWidget()
        self.listwidget.resize(300, 120)
        self.listwidget.addItem("item1")
        self.listwidget.addItem("item2")
        self.listwidget.addItem("item3")
        self.listwidget.addItem("item4")
        self.listwidget.addItem("item5")

        self.setCentralWidget(self.listwidget)

        self.listwidget.itemClicked.connect(self.clicked)

    def clicked(self, index):
        QMessageBox.information(self, "QListWidget", "您选择了:" + self.listwidget.item(self.listwidget.row(index)).text())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    myMain = ListWidgetDemo()
    myMain.show()
    sys.exit(app.exec_())


实例2

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QListWidget, QListWidgetItem


class QmyWidget(QWidget):

    def __init__(self, parent=None):
        super().__init__(parent)  # 调用父类的构造函数,创建QWidget窗体
        self.setupUi()

    def setupUi(self):
        """页面初始化"""
        # 设置窗体大小及标题
        self.resize(500, 400)
        self.setWindowTitle("QListWidget组件示例")
        # 创建布局
        self.layout = QVBoxLayout()

        # 创建QListWidget组件
        self.listWidget = QListWidget()
        self.listWidget.addItem("Item 1")  # 添加item
        self.listWidget.addItem("Item 2")
        self.listWidget.addItem("Item 3")
        self.listWidget.addItem("Item 4")
        # self.listWidget.addItems(["Item 1", "Item 2", "Item 3", "Item 4"])  # 添加多个item
        # QListWidget组件绑定信号
        self.listWidget.itemClicked.connect(self.on_listWidget_itemClicked)
        self.listWidget.itemChanged.connect(self.on_listWidget_itemChanged)
        self.listWidget.itemDoubleClicked.connect(self.on_listWidget_itemDoubleClicked)

        # 创建两个按钮组件
        self.button1 = QPushButton("新增Item", self)
        self.button1.clicked.connect(self.insert_item)  # 为button绑定槽函数
        self.button2 = QPushButton("删除Item", self)
        self.button2.clicked.connect(self.delete_item)  # 为button绑定槽函数
        self.button3 = QPushButton("查询Item", self)
        self.button3.clicked.connect(self.select_item)  # 为button绑定槽函数
        self.button4 = QPushButton("修改Item", self)
        self.button4.clicked.connect(self.change_item)  # 为button绑定槽函数

        # 将组件添加到布局中
        self.layout.addWidget(self.listWidget)
        self.layout.addWidget(self.button1)
        self.layout.addWidget(self.button2)
        self.layout.addWidget(self.button3)
        self.layout.addWidget(self.button4)
        # 为窗体添加布局
        self.setLayout(self.layout)

    def insert_item(self):
        """新增item槽函数"""
        print("item新增成功!")
        # 方式一:新增文本item
        self.listWidget.insertItem(0, "Item Text")
        # 方式二:新增QListWidgetItem对象item
        item_new = QListWidgetItem()
        item_new.setText("Item Obj")  # 为item设置文本
        item_new.setCheckState(False)  # 为item设置复选状态,不勾选
        item_new.setIcon(QIcon("logo.png"))  # 为item设置图标
        item_new.setFlags(Qt.ItemIsEnabled | Qt.ItemIsEditable | Qt.ItemIsUserCheckable)  # 为item设置标志
        self.listWidget.insertItem(0, item_new)
        # 方式三:新增其他部件的item
        item_button = QListWidgetItem()
        button = QPushButton("Button")
        self.listWidget.insertItem(0, item_button)
        self.listWidget.setItemWidget(item_button, button)  # 为item设置按钮部件

    def delete_item(self):
        """删除item槽函数"""
        print("item删除成功!")
        # 方式一:按行索引删除item对象
        self.listWidget.takeItem(0)
        # 方式一:按item对象删除item的小部件
        current_item = self.listWidget.currentItem()
        self.listWidget.removeItemWidget(current_item)

    def select_item(self):
        """查询item槽函数"""
        print("item查询成功!")
        # 隐藏所有的列表项
        for i in range(4):
            self.listWidget.item(i).setHidden(True)
        # 查询Item 1并取消隐藏
        item_list = self.listWidget.findItems("Item 1", Qt.MatchContains | Qt.MatchRecursive)
        for item in item_list:
            item.setHidden(False)

    def change_item(self):
        self.listWidget.item(0).setText('修改了第一行的值')

    def on_listWidget_itemClicked(self, item):
        """槽函数"""
        print('itemClicked信号:', item.text())

    def on_listWidget_itemChanged(self, item):
        print('itemChange信号:', item.text())

    def on_listWidget_itemDoubleClicked(self, item):
        print('双击信号:', item.text())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    myMain = QmyWidget()
    myMain.show()
    sys.exit(app.exec_())

posted @ 2024-06-14 14:06  星空28  阅读(18)  评论(0编辑  收藏  举报