Results 1 to 2 of 2

Thread: Accessing data from a model when an item is clicked in a view

  1. #1
    Join Date
    Sep 2011
    Posts
    20
    Qt products
    Qt5
    Platforms
    Windows

    Default Accessing data from a model when an item is clicked in a view

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Innsbruck, Austria
    Posts
    62
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Accessing data from a model when an item is clicked in a view

    You can add a method to MovieListModel that provides the QHash for the item:

    Qt Code:
    1. QHash<QString, QString> MovieListModel::movieData(const QModelIndex& index)
    2. {
    3. if(index.row() < 0 || index.row() >= list.count()) {
    4. return QHash<QString, QString>();
    5. } else {
    6. return list.at(index.row());
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Then in loadMovieItems you can do this:

    Qt Code:
    1. MovieItemArea::loadMovieItems(const QModelIndex& movieData)
    2. {
    3. QHash<QString, QString> movieData = model->movieData(movieData);
    4. QMessageBox::information(this, tr("List name"), movieData.value("name"));
    5. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 9
    Last Post: 14th February 2013, 19:39
  2. QT Model View - parent Item different from child
    By mromanuk in forum Qt Programming
    Replies: 2
    Last Post: 9th March 2011, 18:21
  3. Replies: 1
    Last Post: 24th February 2011, 05:54
  4. Accessing the first data item from a IndexList.
    By Bonafide in forum Qt Programming
    Replies: 14
    Last Post: 26th February 2010, 04:10
  5. Default Item View classes and returning data from model
    By xerionn in forum Qt Programming
    Replies: 8
    Last Post: 23rd April 2009, 19:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.