PyQT5之QListWidget

星空28發表於2024-06-14

例項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_())

相關文章