Issue
After connecting to my DB i try to get all the items value from QListView but it doesn't have text() method or any thing else i tried to use the model.data() but it returns the following error:
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)
Solution
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 tree models).
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)
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.