Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QListWidgetItem->setData() with different roles fails

QListWidgetItem->setData() with different roles fails

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 8.4k Views 1 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.
  • H Offline
    H Offline
    H.Klingel
    wrote on last edited by
    #1

    Assume the following code:
    @
    QDate currentDate = QDate::currentDate();

    QListWidgetItem *item = new QListWidgetItem;
    item->setData(Qt::EditRole, currentDate );
    item->setData(Qt::DisplayRole, date.toString("dd.MM.yyyy"));
    @
    Now, If I try to get the edit role with:
    @QDate date = item->data(Qt::EditRole).value<QDate>()@
    the result is empty (jd=0).

    That is because item->data(Qt::EditRole) returns the display role, in this case a QString.

    Is that a known bug or is there an error in my understandig of roles?

    I'm using Qt 4.8.1

    Regards,
    Harald

    Edit: please use @ tags around code sections; Andre

    1 Reply Last reply
    0
    • H Offline
      H Offline
      H.Klingel
      wrote on last edited by
      #2

      Ok, I found it by looking at QListWidget.cpp::setData():
      Qt::EditRole is casted to Qt::Display role, so the second setData()-call overrides the first one, since it has the same key.

      I will use Qt::UserRole instead of Qt::EditRole

      1 Reply Last reply
      0
      • JeroentjehomeJ Offline
        JeroentjehomeJ Offline
        Jeroentjehome
        wrote on last edited by
        #3

        Hi there, you mixed it up a bit. The Display role is used by the model to give the current text to be displayed. The EditRole is used by the view to give the model new text to be stored as current text.
        Example (in the model of course):
        @
        bool YourModel::setData(const QModelIndex &index,
        const QVariant &value, int role)
        {
        if (index.isValid() && role == Qt::EditRole) {

             yourList.replace(index.row(), value.toString());
             emit dataChanged(index, index);
             return true;
         }
         return false;
        

        }

        QVariant yourModel::data(const QModelIndex &index, int role) const
        {
        if (!index.isValid())
        return QVariant();

         if (index.row() >= yourList.size())
             return QVariant();
        
         if (role == Qt::DisplayRole)
             return yourList.at(index.row());
         else
             return QVariant();
        

        }
        @
        So in basic when using the Widget version it all happens on the background and the editrole and display role gets handled the same way.

        Greetz, Jeroen

        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 Extensions
        • Unsolved