Results 1 to 5 of 5

Thread: Populating sublassed QAbstractItemModel

  1. #1
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Populating sublassed QAbstractItemModel

    When trying to insert items into my QAbstractItemModel:
    Qt Code:
    1. ListModel * model = new ListModel();
    2. for (int row = 0; row < 4; ++row) {
    3. for (int column = 0; column < 4; ++column) {
    4. model->insertRow(1);
    5. qDebug() << model->setData(model->index(row),QVariant::QVariant("halooo"),Qt::EditRole);
    6. qDebug() << model->data(model->index(row),Qt::EditRole);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    Outputs:
    Qt Code:
    1. false
    2. QVariant(Invalid)
    3. false
    4. QVariant(Invalid)
    5. false
    6. QVariant(Invalid)
    7. false
    8. QVariant(Invalid)
    9. false
    10. QVariant(Invalid)
    11. false
    12. QVariant(Invalid)
    13. false
    14. QVariant(Invalid)
    15. false
    16. QVariant(Invalid)
    17. false
    18. QVariant(Invalid)
    19. false
    20. QVariant(Invalid)
    21. false
    22. QVariant(Invalid)
    23. false
    24. QVariant(Invalid)
    25. false
    26. QVariant(Invalid)
    27. false
    28. QVariant(Invalid)
    29. false
    30. QVariant(Invalid)
    31. false
    32. QVariant(Invalid)
    To copy to clipboard, switch view to plain text mode 

    listmodel.h
    Qt Code:
    1. #ifndef LISTMODEL_H
    2. #define LISTMODEL_H
    3.  
    4. #include <QAbstractListModel>
    5. #include <QStringList>
    6.  
    7. class ListModel : public QAbstractListModel
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit ListModel(QObject *parent = 0);
    12. int rowCount( const QModelIndex & parent ) const;
    13. QVariant data( const QModelIndex & index, int role /* = Qt::DisplayRole*/ ) const;
    14. Qt::ItemFlags ListModel::flags(const QModelIndex &index) const;
    15. bool setData(const QModelIndex &index,const QVariant &value, int role);
    16.  
    17. private:
    18. QStringList stringList;
    19.  
    20. signals:
    21.  
    22. public slots:
    23.  
    24. };
    25.  
    26. #endif // LISTMODEL_H
    To copy to clipboard, switch view to plain text mode 

    listmodel.cpp
    Qt Code:
    1. #include "listmodel.h"
    2. #include <QtDebug>
    3.  
    4. ListModel::ListModel(QObject *parent) :
    5. {
    6. }
    7.  
    8. QVariant ListModel::data( const QModelIndex & index, int role /* = Qt::DisplayRole*/ ) const
    9. {
    10. // if an invalid row, return empty QVariant
    11. if (index.row() < 0 || index.row() >= 4)
    12. return QVariant();
    13.  
    14. switch( role )
    15. {
    16. case Qt::DisplayRole:
    17. case Qt::EditRole:
    18. {
    19. s = QString("row [%1]").arg( index.row() );
    20. return QVariant(s);
    21. }
    22. default:
    23. return QVariant();
    24. }
    25. }
    26.  
    27. bool ListModel::setData(const QModelIndex &index,
    28. const QVariant &value, int role)
    29. {
    30. if (index.isValid() && role == Qt::EditRole) {
    31.  
    32. stringList.replace(index.row(), value.toString());
    33. emit dataChanged(index, index);
    34. return true;
    35. }
    36. return false;
    37. }
    38.  
    39. Qt::ItemFlags ListModel::flags(const QModelIndex &index) const
    40. {
    41. if (!index.isValid())
    42. return Qt::ItemIsEnabled;
    43.  
    44. return QAbstractItemModel::flags(index) /*|Qt::ItemIsEditable*/;
    45. }
    46.  
    47. int ListModel::rowCount(const QModelIndex &parent) const
    48. {
    49. return stringList.count();
    50. }
    To copy to clipboard, switch view to plain text mode 

    It seems like the index passed is never valid, "index.isValid()", so it never sets the data. What's wrong here?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Populating sublassed QAbstractItemModel

    You did not implement insertRows().
    When your example code calls insertRow(), your string list stays at size 0 and so does rowCount().

    If all you are storing is a list of strings and you are adding them from outside instead of having the model interfacing to already existing data, why not just use QStringListModel?

    Cheers,
    _

  3. #3
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Populating sublassed QAbstractItemModel

    Yes, QStringListModel is what I should use. Do I have to create a custom class to set my model as not edotable?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Populating sublassed QAbstractItemModel

    No, you don't.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Populating sublassed QAbstractItemModel

    The only way I know is to reimplement flags():
    Qt Code:
    1. Qt::ItemFlags ListModel::flags(const QModelIndex &index) const
    2. {
    3. if (!index.isValid())
    4. return Qt::ItemIsEnabled;
    5.  
    6. return QAbstractItemModel::flags(index) /*|Qt::ItemIsEditable*/;
    7. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Populating qtablewidget with QStringlist
    By decipher in forum Newbie
    Replies: 1
    Last Post: 31st August 2012, 08:25
  2. Multiple Queries populating QSqlQueryModel
    By hollyberry in forum Newbie
    Replies: 1
    Last Post: 24th April 2012, 01:36
  3. Odd behavior when populating a Table
    By Kyle_Katarn in forum Newbie
    Replies: 8
    Last Post: 8th April 2011, 22:38
  4. Populating, signals and comboboxes
    By ShamusVW in forum Newbie
    Replies: 6
    Last Post: 12th August 2010, 06:43
  5. QTableView populating question
    By onefootswill in forum Newbie
    Replies: 8
    Last Post: 7th August 2008, 22:29

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
  •  
Qt is a trademark of The Qt Company.