如何从 pyqt5 中的 QListView 获取项目值?
how to get item values from QListView in pyqt5?
连接到我的数据库后,我尝试从 QListView 获取所有项目值
但它没有 text() 方法或任何其他我尝试使用 model.data() 但它 returns 出现以下错误:
File "c:\Users\inter\Desktop\neosun\main copy 9.py", line 88, in
addToPlaylist
item = model.data(index, 1, Qt.DisplayRole) TypeError: data(self, QModelIndex, role: int = Qt.DisplayRole): argument 1 has unexpected
type '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 使用 QModelIndex class to locate data within any model, so you cannot just use integers to access them, but model.index(row, column, parent=None)
必须改用(可选的 parent 参数用于多维模型,如树模型)。
def getData(self):
model = self.listview.model()
for <b>row</b> in range(model.rowCount()):
<b>index = model.index(row, 1)</b>
item = model.data(<b>index</b>, Qt.DisplayRole)
print(item)
连接到我的数据库后,我尝试从 QListView 获取所有项目值 但它没有 text() 方法或任何其他我尝试使用 model.data() 但它 returns 出现以下错误:
File "c:\Users\inter\Desktop\neosun\main copy 9.py", line 88, in addToPlaylist item = model.data(index, 1, Qt.DisplayRole) TypeError: data(self, QModelIndex, role: int = Qt.DisplayRole): argument 1 has unexpected type '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 使用 QModelIndex class to locate data within any model, so you cannot just use integers to access them, but model.index(row, column, parent=None)
必须改用(可选的 parent 参数用于多维模型,如树模型)。
def getData(self):
model = self.listview.model()
for <b>row</b> in range(model.rowCount()):
<b>index = model.index(row, 1)</b>
item = model.data(<b>index</b>, Qt.DisplayRole)
print(item)