Results 1 to 8 of 8

Thread: ListView problem with c++ model

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2011
    Posts
    25
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: ListView problem with c++ model

    Qt Code:
    1. FieldListModel::FieldListModel(IndexedFieldContainer * fieldContainer)
    2. {
    3. _fieldContainer = fieldContainer;
    4.  
    5. QHash<int, QByteArray> roles;
    6. roles[NameRole] = "name";
    7. roles[ValueRole] = "value";
    8. roles[DescriptionRole] = "description";
    9. setRoleNames(roles);
    10.  
    11. IndexedFieldContainer::Iterator it = _fieldContainer->begin();
    12. for ( ; it != _fieldContainer->end(); ++it )
    13. {
    14. QObject::connect((*it), SIGNAL(fieldValueChanged(int)),
    15. this, SLOT(fieldCurrentValueChanged(int)));
    16. }
    17. }
    18.  
    19. QVariant FieldListModel::data(const QModelIndex & index, int role) const
    20. {
    21. if ( !index.isValid() )
    22. return QVariant();
    23.  
    24. if (index.row() < 0 || index.row() > _fieldContainer->count())
    25. {
    26. return QVariant();
    27. }
    28.  
    29. DataModelBasicField * field = _fieldContainer->at(index.row());
    30.  
    31. switch ( role )
    32. {
    33. case NameRole:
    34. return field->getName();
    35. break;
    36. case ValueRole:
    37. return field->toString();
    38. break;
    39. case DescriptionRole:
    40. return field->getDescription();
    41. break;
    42. default:
    43. return QVariant();
    44. }
    45.  
    46. return QVariant();
    47. }
    48.  
    49. int FieldListModel::rowCount(const QModelIndex & /* parent*/ ) const
    50. {
    51. return _fieldContainer->count();
    52. }
    53.  
    54. void FieldListModel::fieldCurrentValueChanged(int listIndex)
    55. {
    56. QModelIndex index = createIndex(listIndex, listIndex);
    57. dataChanged(index,index);
    58. }
    To copy to clipboard, switch view to plain text mode 

    Do you need other things?

    edit:
    Qt Code:
    1. typedef QHash<QString, DataModelBasicField*> FieldContainer;
    2. typedef QVector<DataModelBasicField*> IndexedFieldContainer;
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: ListView problem with c++ model

    What about setData(), insertRows(), etc.?
    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.


Similar Threads

  1. Replies: 3
    Last Post: 15th June 2013, 12:39
  2. listview problem (i need help)
    By rimie23 in forum Qt Programming
    Replies: 1
    Last Post: 10th May 2012, 19:03
  3. Custom ListView. Using the model / view framework.
    By plopes21 in forum Qt Programming
    Replies: 19
    Last Post: 8th May 2012, 08:43
  4. QML Listview Highlight image problem
    By vikaspachdha in forum Qt Quick
    Replies: 0
    Last Post: 2nd November 2011, 09:14
  5. ListView model binding to QAbstractListModel
    By michalk in forum Qt Quick
    Replies: 1
    Last Post: 16th July 2011, 09:21

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.