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. Problem mapping QSpinBox to current item row in QAbstractItemModel-based model.
Forum Updated to NodeBB v4.3 + New Features

Problem mapping QSpinBox to current item row in QAbstractItemModel-based model.

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.4k 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.
  • R Offline
    R Offline
    roni219
    wrote on last edited by
    #1

    Hello everybody,

    I would like to map QSpinBox value to the row number of the current item in a model tracked by QItemSelectionModel as follows:

    • When QItemSelectionModel changes current row, the QSpinBox value must change.

    • When QSpinBox value changes, QItemSelectionModel must change the current item.

    • The QSpinBox range must be [0, model.rowCount() - 1], and change when number of rows in the model changes.

    This is what I tried (based on Simple Widget Mapper example):

    MyModel model;
    QItemSelectionModel selection(&model);
    QSpinBox * spinBox = new QSpinBox();
    QDataWidgetMapper * mapper = new QDataWidgetMapper();
    
    mapper->setModel(&model);
    mapper->addMapping(spinBox, model.ID_COLUMN);
    
    connect(&selection, SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex &)),
    mapper, SLOT(setCurrentModelIndex(const QModelIndex &)));
    
    connect(mapper, SIGNAL(currentIndexChanged(const QModelIndex &)),
    &selection, SLOT(setCurrentIndex(const QModelIndex &)));
    

    The model reimplements data() and reports item's row for ID_COLUMN when the role is Qt::DisplayRole:

    class MyModel : public QAbstractItemModel
        {
        public:
    
            enum {ID_COLUMN = 0, ..., COLUMN_COUNT };
    
    	QVariant data(const QModelIndex & index, int role) const override;
    
           // ...  other methods
    };
    
    QVariant MyModel::data(const QModelIndex & index, int role) const
    {
        if((!indexIsValid(index)) || (index.column() != ID_COLUMN))
        {
            return QVariant();
        }
    
        switch(role)
        {
            case Qt::DisplayRole:
                return QVariant(QString::number(index.row()));
           // ...  other cases
        }
    
        return QVariant();
    }
    

    The above code does not work: the spin box range is incorrect, the spin box value changes have no effect on current item. When selection changes the current row, the spin box value is set to 0, not to the current row.

    Please suggest what might be missing. Perhaps additional roles in data() must be implemented? And how to tie the model's row index range to the spin box range?

    Thank you in advance!

    Roni.

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

      Hi,

      QDataWidgetMapper is for interacting with database a model content which is not what you are currently doing.

      I'd rather connect the QSpinBox valueChanged signal to a slot that will update the current selected index using the value provided by the spin box and then do the same for the currentIndexChanged and update the spin box from the slot.

      Hope it helps

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

        Thank you, @SGaist. I originally wrote a solution like you suggested, and it worked. It also required changing the range of the spin box whenever the rows where inserted or deleted from the model.

        Than I saw the Simple Widget Mapper Example, and it seemed like a simpler solution. The text said:

        "The QDataWidgetMapper class allows information obtained from a model to be viewed and edited in a collection of widgets instead of in an item view."

        The example used QStandardItemModel, so it was not clear how a custom model should support this interface...

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

          Quote from the documentation: The QDataWidgetMapper class provides mapping between a section of a data model to widgets.

          The important part here is section of a data model. What you are manipulating here is not a section of a data model

          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
          • R Offline
            R Offline
            roni219
            wrote on last edited by
            #5

            Thank you for the explanation, @SGaist, I will go back to the original approach.

            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