[英]how to get item values from QListView in pyqt5?
連接到我的數據庫后,我嘗試從 QListView 獲取所有項目值,但它沒有 text() 方法或任何其他我嘗試使用 model.data() 但它返回以下錯誤的東西:
文件“c:\Users\inter\Desktop\neosun\main copy 9.py”,第 88 行,在 addToPlaylist 項 = model.data(index, 1, Qt.DisplayRole) TypeError: data(self, QModelIndex, role: int = Qt.DisplayRole):參數 1 具有意外類型“int”
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setGeometry(900,180,800,600)
self.setWindowTitle("Media Display")
self.setWindowIcon(QIcon('favicon.png'))
self.model = QSqlQueryModel()
self.model.setQuery("SELECT path FROM files")
self.listview = QListView()
self.listview.setModel(self.model)
self.listview.setModelColumn(1)
self.getData()
def getData(self):
model = self.listview.model()
for index in range(model.rowCount()):
# item = model.data(index)
item = model.data(index, 1, Qt.DisplayRole)
print(item)
Qt uses the QModelIndex class to locate data within any model, so you cannot just use integers to access them, but model.index(row, column, parent=None)
must be used instead (the optional parent argument is for multidimensional models, like樹模型)。
def getData(self):
model = self.listview.model()
for row in range(model.rowCount()):
index = model.index(row, 1)
item = model.data(index, Qt.DisplayRole)
print(item)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.