Python QListView教程的實(shí)現(xiàn)
QListView是PyQt(包括PyQt5和PyQt6)中的一個強(qiáng)大控件,用于展示列表數(shù)據(jù)。它基于模型/視圖/委托(Model/View/Delegate)架構(gòu),提供了靈活的數(shù)據(jù)展示和處理能力。以下是一個關(guān)于QListView的全面教程:
一、安裝PyQt
首先,確保已經(jīng)安裝了PyQt5或PyQt6。如果沒有安裝,可以使用pip進(jìn)行安裝:
pip install PyQt5 # 或者 pip install PyQt6
二、導(dǎo)入必要的模塊
在使用QListView之前,需要導(dǎo)入必要的PyQt模塊。例如:
import sys from PyQt5.QtWidgets import QApplication, QListView, QWidget, QVBoxLayout # PyQt5用戶 # 或者 # from PyQt6.QtWidgets import QApplication, QListView, QWidget, QVBoxLayout # PyQt6用戶 from PyQt5.QtGui import QStandardItemModel, QStandardItem # PyQt5用戶,用于創(chuàng)建和管理模型中的數(shù)據(jù) # 或者 # from PyQt6.QtCore import QStringListModel # PyQt6用戶,另一個常用的模型類
三、創(chuàng)建QListView和模型
QListView本身不存儲數(shù)據(jù),而是通過與數(shù)據(jù)模型關(guān)聯(lián)來展示數(shù)據(jù)。常用的數(shù)據(jù)模型有QStandardItemModel和QStringListModel。
使用QStandardItemModel
class ListViewExample(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.listView = QListView(self)
self.model = QStandardItemModel()
# 添加一些項(xiàng)目到模型中
items = ["Item 1", "Item 2", "Item 3", "Item 4"]
for item in items:
standardItem = QStandardItem(item)
self.model.appendRow(standardItem)
self.listView.setModel(self.model)
layout = QVBoxLayout()
layout.addWidget(self.listView)
self.setLayout(layout)
self.setWindowTitle('QListView Example')
# 主程序
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = ListViewExample()
ex.show()
sys.exit(app.exec_())
使用QStringListModel
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QListView # PyQt5用戶
# 或者
# from PyQt6.QtWidgets import QApplication, QMainWindow, QListView # PyQt6用戶
from PyQt5.QtGui import QStringListModel # PyQt5用戶
# 或者
# from PyQt6.QtCore import QStringListModel # PyQt6用戶
app = QApplication(sys.argv)
main_window = QMainWindow()
main_window.setWindowTitle("QListView 示例")
main_window.setGeometry(100, 100, 400, 300)
list_view = QListView(main_window)
list_view.setGeometry(50, 50, 300, 200)
model = QStringListModel()
data = ["蘋果", "香蕉", "橙子", "葡萄", "草莓"]
model.setStringList(data)
list_view.setModel(model)
main_window.show()
sys.exit(app.exec_())
四、QListView的常用方法和信號
常用方法
setModel(Model): 用來設(shè)置View所關(guān)聯(lián)的Model。selectedItem(n): 選中Model中的條目n(注意:這個方法不是QListView的標(biāo)準(zhǔn)方法,可能是某些特定上下文或自定義擴(kuò)展中的方法,標(biāo)準(zhǔn)方法是通過模型來獲取選中項(xiàng))。isSelected(): 判斷Model中的某條目是否被選中(同樣,這個方法可能不是QListView的直接方法,而是通過模型來判斷)。
常用信號
clicked: 當(dāng)單擊某項(xiàng)時發(fā)送。doubleClicked: 當(dāng)雙擊某項(xiàng)時發(fā)送。
五、自定義QListView
QListView支持自定義項(xiàng)的顯示方式,這通常通過實(shí)現(xiàn)自定義的委托(QStyledItemDelegate或QItemDelegate)來完成。自定義委托可以重寫paint()和sizeHint()方法來控制項(xiàng)的繪制和大小。
六、QListView的常用設(shè)置
- 設(shè)置項(xiàng)間距:
setSpacing(int spacing) - 設(shè)置顯示模式:
setViewMode(QListView.ViewMode mode),可以是列表模式(ListMode)或圖標(biāo)模式(IconMode)。 - 設(shè)置是否允許拖動:
setDragEnabled(bool enable) - 設(shè)置選擇模式:
setSelectionMode(QAbstractItemView.SelectionMode mode),可以是單選、多選等。
七、示例:添加、刪除和排序功能
以下是一個包含添加、刪除和排序功能的QListView示例:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QListView, QPushButton, QMessageBox
from PyQt5.QtGui import QStandardItemModel, QStandardItem
class QListviewDemo(QWidget):
def __init__(self, *args, **kwargs):
super(QListviewDemo, self).__init__(*args, **kwargs)
self.setWindowTitle("QListviewDemo")
self.resize(400, 400)
self.layout = QVBoxLayout(self)
self.qlistview = QListView()
self.layout.addWidget(self.qlistview)
self.mode = QStandardItemModel(5, 1)
for i in range(self.mode.rowCount()):
item = QStandardItem("item%d" % i)
self.mode.setItem(i, 0, item)
self.mode.insertRow(5, QStandardItem("item0"))
self.qlistview.setModel(self.mode)
hlayout = QVBoxLayout()
self.add_btn = QPushButton("增", clicked=self.on_add_btn_clicked)
self.del_btn = QPushButton("刪", clicked=self.on_del_btn_clicked)
self.sort_btn = QPushButton("排序", clicked=self.on_sort_btn_clicked)
hlayout.addWidget(self.add_btn)
hlayout.addWidget(self.del_btn)
hlayout.addWidget(self.sort_btn)
self.layout.addLayout(hlayout)
def on_add_btn_clicked(self):
num = self.mode.rowCount()
self.mode.appendRow(QStandardItem("item%d" % (num + 1)))
def on_del_btn_clicked(self):
num = self.mode.rowCount()
if num > 0:
self.mode.removeRow(num - 1)
def on_sort_btn_clicked(self):
self.mode.sort(0)
if __name__ == '__main__':
app = QApplication(sys.argv)
test = QListviewDemo()
test.show()
sys.exit(app.exec_())

在這個示例中,我們創(chuàng)建了一個QListView,并為其添加了一個QStandardItemModel作為數(shù)據(jù)模型。然后,我們添加了三個按鈕來實(shí)現(xiàn)添加、刪除和排序功能。
總的來說,QListView是一個功能強(qiáng)大的控件,用于展示和操作列表數(shù)據(jù)。通過了解其基本原理和常用方法,可以輕松地將其集成到PyQt應(yīng)用程序中。
到此這篇關(guān)于Python QListView教程的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python QListView內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python使用Selenium+Requests+OpenCV實(shí)現(xiàn)智能化的Web自動化系統(tǒng)
隨著互聯(lián)網(wǎng)技術(shù)的快速發(fā)展,Web 自動化測試和任務(wù)自動化成為了現(xiàn)代軟件開發(fā)與運(yùn)營的重要組成部分,今天,我們將深入探討如何使用 Python 中的 Selenium、Requests 和 OpenCV 庫,來實(shí)現(xiàn)一個智能化的 Web 自動化系統(tǒng),需要的朋友可以參考下2025-07-07
淺談function(函數(shù))中的動態(tài)參數(shù)
下面小編就為大家?guī)硪黄獪\談function(函數(shù))中的動態(tài)參數(shù)。小編覺得聽不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
Python返回數(shù)組/List長度的實(shí)例
今天小編就為大家分享一篇Python返回數(shù)組/List長度的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
Python-numpy實(shí)現(xiàn)灰度圖像的分塊和合并方式
今天小編就為大家分享一篇Python-numpy實(shí)現(xiàn)灰度圖像的分塊和合并方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python實(shí)現(xiàn)把類當(dāng)做字典來訪問
今天小編就為大家分享一篇Python實(shí)現(xiàn)把類當(dāng)做字典來訪問,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12

