Results 1 to 8 of 8

Thread: ListView problem with c++ model

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

    Default ListView problem with c++ model

    Hi all,
    I have a problem with ListView with a custom c++ model created following qt guide (i.e. extending QAbstractListModel).
    In my application I have a ListView that can show only 10 rows, while in my model I load 17 items.

    The problem is that when I start toying around with the list (flicking, clicking on elements, moving highlight with my personal focus management), at a certain point, the ListView stops to work!
    Flicking is blocked or, when it is enabled, the elements that should appear are not visible.. or again currentItem still blocked in wrong item like this (http://tinypic.com/view.php?pic=5d3p05&s=5).

    I found a sort of similar problem at this link: http://stackoverflow.com/questions/1...ws-are-visible but the answer does not help me.

    Notes:
    1) ListView is a my custom ListView that has some extension to manage focus changing; I tried with a standard ListView but the problem still remains.
    2) With static ListModel added in qml file everything works fine, so the problem seems to be my QAbstractListModel implementation.
    3) In my focus management I print some debug strings and I noticed that, when the problem occurs, ListView.view.count becomes always 0.


    Can anyone help me?
    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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

    Can you show us your model implementation?
    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.


  3. #3
    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 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


  5. #5
    Join Date
    Jun 2011
    Posts
    25
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: ListView problem with c++ model

    The model is not modified so I don't think is necessary to reimplement it


    Added after 40 minutes:


    Update:
    I don't know what .. but something must be wrong.

    If I run program (compiled in debug) without debugger: changing of a property value and working with ListView works with a small number of operation before "crash".
    If I run program (compiled in debug) with debugger: changing of a property value and ListView do not work immediately!

    Over all the really strange thing is that, with debugger, i break on emit of the signal "fieldValueChanged" but the relative slot is not called!
    I checked also that is connected the right QObject!

    This stinks of dirty memory.. but I also tried to use valgrind without find any invalid read related to the data model.

    How is it possibile?
    Last edited by nick85; 11th September 2013 at 15:37.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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

    It seems your model is modifyable since you are emitting dataChanged() signals at some point.
    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.


  7. #7
    Join Date
    Jun 2011
    Posts
    25
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: ListView problem with c++ model

    edit: mmm ..maybe setData is needed
    Last edited by nick85; 11th September 2013 at 17:07.

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

    A couple of things that might not be related to the observed problem.

    in data() the check for row's range is wrong. row == index.row() > _fieldContainer->count() passes through but is not a valid index in the list
    Should be
    Qt Code:
    1. if (index.row() < 0 || index.row() >= _fieldContainer->count())
    To copy to clipboard, switch view to plain text mode 

    in fieldCurrentValueChanged(int), the given listIndex is passed for row and column. column should be 0 in a list model.

    The method should also call index() since that is one of the methods implemented by QAbstractListModel. It likely does just call createIndex() but this is not guaranteed.

    Cheers,
    _

  9. The following user says thank you to anda_skoa for this useful post:

    nick85 (26th September 2013)

Similar Threads

  1. Replies: 3
    Last Post: 15th June 2013, 13:39
  2. listview problem (i need help)
    By rimie23 in forum Qt Programming
    Replies: 1
    Last Post: 10th May 2012, 20:03
  3. Custom ListView. Using the model / view framework.
    By plopes21 in forum Qt Programming
    Replies: 19
    Last Post: 8th May 2012, 09:43
  4. QML Listview Highlight image problem
    By vikaspachdha in forum Qt Quick
    Replies: 0
    Last Post: 2nd November 2011, 10:14
  5. ListView model binding to QAbstractListModel
    By michalk in forum Qt Quick
    Replies: 1
    Last Post: 16th July 2011, 10: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.