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. Need to store and retrieve Qt::UserRole data in a QAbstractTableModel
Forum Updated to NodeBB v4.3 + New Features

Need to store and retrieve Qt::UserRole data in a QAbstractTableModel

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 6.1k 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.
  • M Offline
    M Offline
    marceloarguello700
    wrote on last edited by marceloarguello700
    #1

    Hi, i have a tableview with a column that use combobox
    I correct subclase QAbstractTableModel and QStyledItemDelegate based on this wiki
    https://wiki.qt.io/Combo_Boxes_in_Item_Views

    My combobox have many items, so in the ComboBoxItemDelegate::setEditorData i does not want to findText on combobox

    QString currentText = index.data(Qt::EditRole).toString();
    int cbIndex = cb->findText(currentText);
    

    Just i want to retrieve the index from the model using Qt::UserRole

    int cbIndex = index.data(Qt::UserRole).toInt();
    cb->setCurrentIndex(cbIndex);
    

    Then in the TableModel try to set Qt::UserRole

    QVariant TmLayers::data(const QModelIndex &index, int role) const
    {
    	if (role == Qt::DisplayRole  && index.column()==1)
    	  { //This work
    	   return QVariant((*mSource)[index.row()].getLayer()->ZScaleName());
    	  }
    
       if (role == Qt::UserRole  && index.column()==1)
    	  { //This don´t work
    	   qDebug()<<"UserRole and column 1";
    	   return QVariant((*mSource)[index.row()].getLayer()->ZScaleIndex());
    	  }	  
       return QVariant();
    }
    

    But the model never call the data with role == Qt::UserRole

    What can be wrong?

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

      Hi,

      If you are seeing data properly then it means that you are not using your TmLayers model because currently it would return nothing unless role is Qt::DisplayRole and the column is 1.

      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
      • M Offline
        M Offline
        marceloarguello700
        wrote on last edited by marceloarguello700
        #3

        Thank for the reply but i don´t understand your answer.
        Now the model work perfect.
        In the QVariant TmLayers::data(...) have many if() for the other columns.
        The column with combobox is the number 1

        if ((role == Qt::DisplayRole ||role == Qt::EditRole) && index.column()==1)
        {
        return QVariant((*mSource)[index.row()].getLayer()->ZScaleName());
        }
        

        It work ok, but inside the itemdelegate I must to search by string:

        void idZScale::setEditorData(QWidget *editor,const QModelIndex &index) const
        {
        QString modelvalue = index.model()->data(index, Qt::EditRole).toString();
        QComboBox comboBox = static_cast<QComboBox>(editor);
        //Recive string then must to find it
        int pos = comboBox->findText(modelvalue,( Qt::MatchExactly | Qt::MatchCaseSensitive ));
        if (pos==-1){qDebug()<<"Don found";comboBox->setCurrentIndex(0);}
        comboBox->setCurrentIndex(pos);
        }
        

        And i want to do that:

        int cbIndex = index.data(Qt::UserRole).toInt();
        cb->setCurrentIndex(cbIndex);
        

        But i does not know how to store the index value in the model.
        I hope to be clear
        Greetings

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

          You would need to add that to your object but what's the problem with the findText method ? It allows you to reorder the items in the combo box at will without having to worry about that index.

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