Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Calling the data method of a QAbstractItemModel in QML always uses display role instead of the requested role
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

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qabstractitemmoroles
2 Posts 2 Posters 31 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Grzesiek11G Offline
    Grzesiek11G Offline
    Grzesiek11
    wrote last edited by Grzesiek11
    #1

    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 TextField ends 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, so edit name 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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote last edited by
      #2

      Hi and welcome to devnet,

      Might be a silly question but are you using Qt Bridges ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt
      • Unsolved