I have a working model derived from QAbstractTableModel. It's currently happily populating lists and tables with the appropriate values.

I want to display a selected item in a widget view with some labels, text fields, etc.

Currently, I'm just trying to statically display the first item in the list to see it work. Code references are below.

First, I did not use mapper->setCurrentModelIndex(), but the mapper->toFirst() method always returned a QModelIndex of (-1,-1).

I'm having a lot of issues debugging because this seems to do virtually nothing.

It appears like it cycles through the items and calls the data() method with the Qt::EditRole, but that's it.

Currently, editing of values is not implemented. I just need to display them in the widget fields. Am I not using QDataWidgetMapper correctly? Is it only for editable widgets? Why is it only calling the EditRole and never the DisplayRole?

Any tips are greatly appreciated.

My Model:

Qt Code:
  1. #ifndef COMPONENTMODEL_H
  2. #define COMPONENTMODEL_H
  3. #include <QAbstractTableModel>
  4. #include <QResource>
  5. #include <QStringList>
  6. #include <QIcon>
  7.  
  8. #include "Common/Global.h"
  9. #include "Components/ComponentList.h"
  10. #include "Components/ComponentInterface.h"
  11.  
  12. namespace kex
  13. {
  14. class ComponentModel : public QAbstractTableModel
  15. {
  16. Q_OBJECT
  17.  
  18. public:
  19. ComponentModel(int types = ComponentInterface::AllComponents,
  20. QObject *parent = 0);
  21.  
  22. /** \brief Default destructor.
  23.   *
  24.   * Copyright 2010 KSpace MRI. All Rights Reserved.
  25.   *
  26.   * \author James Kyle
  27.   * \author $LastChangedBy$
  28.   * \date 2010-4-12
  29.   * \date $LastChangedDate$
  30.   * \version $Rev$
  31.   **/
  32. ~ComponentModel() {}
  33.  
  34. int rowCount(const QModelIndex &parent = QModelIndex()) const;
  35. int columnCount(const QModelIndex &parent = QModelIndex()) const;
  36.  
  37. QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
  38. QVariant headerData(int section, Qt::Orientation orientation,
  39. int role) const;
  40.  
  41. private:
  42. const ComponentList::ComponentQList *_componentList;
  43. public slots:
  44. void updateComponentList();
  45. };
  46.  
  47.  
  48. }
  49. #endif // COMPONENTMODEL_H
To copy to clipboard, switch view to plain text mode 

My Model's data() method:

Qt Code:
  1. QVariant ComponentModel::data(const QModelIndex &index, int role) const
  2. {
  3. QVariant result;
  4. if (role == Qt::DecorationRole)
  5. {
  6. QIcon icon(":/images/other/Science-64.png");
  7. result = icon;
  8. } else if (role == Qt::DisplayRole)
  9. {
  10. qDebug() << "called model data() display role";
  11.  
  12. if (index.row() < _componentList->count() &&
  13. index.row() >= 0 &&
  14. index.column() >= 0 &&
  15. index.column() < columnCount())
  16. {
  17. qDebug() << "valid index";
  18.  
  19. AbstractComponent::Pointer comp = (*_componentList)[index.row()];
  20.  
  21. if (index.column() == 0)
  22. {
  23. qDebug() << "column 0";
  24.  
  25. result.setValue(comp->name());
  26. }
  27. else {
  28. result.setValue(QString("a column value"));
  29. }
  30. }
  31. }
  32. return result;
  33. }
To copy to clipboard, switch view to plain text mode 

The method that configures the QDataWidgetMapper

Qt Code:
  1. void MainWindow::setUpWidgetMapper()
  2. {
  3. // set up the data mapper for displaying
  4. ComponentModel *model = new ComponentModel(ComponentInterface::AllComponents,
  5. this);
  6. mapper->setModel(model);
  7. mapper->addMapping(componentNameLabel, 0);
  8. mapper->addMapping(typeNameLabel, 1);
  9. mapper->addMapping(componentDurationLabel, 4);
  10. mapper->addMapping(componentDescriptionTextEdit, 3);
  11. mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
  12. mapper->setCurrentModelIndex(model->index(0,0));
  13. mapper->toFirst();
  14. }
To copy to clipboard, switch view to plain text mode