Qt 6.12 LTS is out! See what's new in the release
blog
Calling the data method of a QAbstractItemModel in QML always uses display role instead of the requested role
-
I'm implementing an editor for my QAbstractItemModel subclass, so I started by getting it's edit role data into an edit dialog:
TextField { text: nbtTree.data(editPopup.index, "edit") }However, the
TextFieldends up with the display role data instead. I started print debugging this (I'm using Rust, hopefully this is simple enough code to understand for C++ programmers):fn data(&self, index: &QModelIndex, role: i32) -> QVariant { println!("{role}"); // ... }It prints out 0, the display role, never 2, the edit role.
I didn't override
roleNames, soeditname should be bound to the edit role, but just in case, I tried setting my own too:// role_names is equivalent to roleNames in C++ fn role_names(&self) -> QHash<QHashPair_i32_QByteArray> { let mut role_names = QHash::default(); role_names.insert(ItemDataRole::DisplayRole.repr, QByteArray::from("display".as_bytes())); role_names.insert(ItemDataRole::EditRole.repr, QByteArray::from("edit".as_bytes())); role_names }This did not change anything.
What could be the issue here?
-
Hi and welcome to devnet,
Might be a silly question but are you using Qt Bridges ?