Hi. I've done all the research I can and can't solve this simple problem.

I have a model (QAbstractListModel) that loads data from a database (id, name, etc.), and puts it in a data structure QList<QHash<QString, QString> >.

Then in the model's data() method the name field is returned to the QListView.

Code for the model looks something like this (I removed some unnecessary stuff so there might be some inconsistencies):

Qt Code:
  1. class MovieListModel : public QAbstractListModel
  2. {
  3. Q_OBJECT
  4.  
  5. private:
  6. QList<QHash<QString, QString> > lists;
  7.  
  8. public:
  9. MovieListModel(QObject *parent = 0);
  10. int rowCount(const QModelIndex &parent) const;
  11. QVariant data(const QModelIndex &index, int role) const;
  12. };
  13.  
  14. MovieListModel::MovieListModel(QObject *parent) : QAbstractListModel(parent)
  15. {
  16. QSqlQuery query;
  17. query.exec("SELECT id, name FROM movie_lists ORDER BY id ASC");
  18.  
  19. while(query.next())
  20. {
  21. QHash<QString, QString> movieData;
  22.  
  23. movieData["id"] = query.value(0).toString();
  24. movieData["name"] = query.value(1).toString();
  25.  
  26. lists << movieData;
  27. }
  28. }
  29.  
  30. int MovieListModel::rowCount(const QModelIndex &parent) const
  31. {
  32. return lists.size();
  33. }
  34.  
  35. QVariant MovieListModel::data(const QModelIndex &index, int role) const
  36. {
  37. if(role == Qt::DisplayRole)
  38. {
  39. QHash<QString, QString> movieData = lists.at(index.row());
  40.  
  41. return movieData["name"];
  42. }
  43.  
  44. return QVariant();
  45. }
To copy to clipboard, switch view to plain text mode 

And then I set the model to a view:

Qt Code:
  1. MovieListModel *mdl = new MovieListModel(this);
  2. QListView *view = new QListView(this);
  3. view->setModel(mdl);
To copy to clipboard, switch view to plain text mode 

Then I have another widget that inherits from QAbstractScrollArea and what I want to do is when I click an item in the view, I want to access that item's QHash<QString, QString> data from the widget that inherits from QAbstractScrollArea, lets call it MovieItemArea that's populated by movie item widgets that belong to the list that was clicked. So in the MovieItemArea class I would have something like this:

Qt Code:
  1. MovieItemArea::loadMovieItems(const QHash<QString, QString>& movieData)
  2. {
  3. int listId = movieData["id"].toInt();
  4.  
  5. // SQL query to load movies for that list id...
  6. }
To copy to clipboard, switch view to plain text mode 

I can extract the list name like this:

Qt Code:
  1. connect(view, SIGNAL(clicked(QModelIndex)), movieItemArea, SLOT(loadMovieItems(QModelIndex)));
To copy to clipboard, switch view to plain text mode 

When the function implementation is a little different as well:

Qt Code:
  1. MovieItemArea::loadMovieItems(const QModelIndex& movieData)
  2. {
  3. QMessageBox::information(this, tr("List name"), movieData.data().toString();
  4. }
To copy to clipboard, switch view to plain text mode 

But I can't figure out a way to access all the data.

Also this is my first proper Qt app so if there's something to improve let me know.

Thanks.