Reputation: 83
This appears to be the same (unanswered) issue as here: QML Combobox reports ReferenceError: modelData is not defined . The closest (closed) bug in the QT database I can find is: https://bugreports.qt.io/browse/QTBUG-31135 so I'm not sure that this is the same issue. I'm running PyQt5 v5.5 and python 3.4.3.
I'm implementing a QAbstractListModel in PyQt5, and have distilled the code down to the issue at hand:
# ExampleModel.py
class ExampleModel(QAbstractListModel):
def __init__(self, parent=None):
super().__init__(parent)
self.items = []
for t in range(0,10):
self.items.append({'text': t, 'myother': 'EXAMPLE'})
def data(self, index, role):
key = self.roleNames()[role]
return self.items[index.row()][key.decode('utf-8')]
def rowCount(self, parent=None):
return len(self.items)
def roleNames(self):
return {Qt.UserRole + 1: b'text',
Qt.UserRole + 2: b'myother'}
And the related QML:
# example.qml
...
ComboBox { // Displays blank entires + throws ReferenceError
id: comboExample
model: ExampleModel{}
textRole: 'text' # This was the missing line to make this work.
}
ListView { // Works Correctly
id: listExample
model: ExampleModel{}
delegate: Text {
text: myname + " " + myother
}
}
...
When I run this, the combo box has 10 blank entries, and the console error log shows:
[path]/ComboBox.qml:562: ReferenceError: modelData is not defined
(x 10)
Now, if I modify the roleNames() code in ExampleModel.py above to the following:
def roleNames(self):
return {Qt.UserRole + 1: b'myname'}
the ComboBox works correctly.
Am I missing a key concept here? I don't want to implement my model twice (once for this ComboBox workaround.)
By adding Mitch's suggestion to example.qml above, this problem is fixed. The code has been updated accordingly.
Upvotes: 2
Views: 3027
Reputation: 24416
You need to set textRole
to the name of the role you want to display ("text"
, in your case).
The documentation could be a lot clearer about this, so I've created a bug report.
Upvotes: 3