Results 1 to 6 of 6

Thread: QAbstractListModel problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QAbstractListModel problem

    The documentation specifically mentions returning an empty QVariant rather than returning 0 so I'd hope it wouldn't be that.

    Running the same code but using a QTableView instead calls with role values of 6, 7, 9, 10, 1, 0 and 8. I reckon it might actually be something weird going on with the QListView. I wonder if maybe the Jambi implementation requires me to trigger something that the QTableView doesn't?

    Thanks for your help with this. I think it might be worth seeing whether I can contact the Qt developers and logging a bug report or something. I've managed to find myself a work-around for now, it's not ideal but it's not too clunky and just avoids the QListView control.

  2. #2
    Join Date
    Sep 2009
    Posts
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractListModel problem

    I had a very similar issue with QAbstractListModel in C++ (the code I tried was the same (but in C++)) I found a solution by looking at the source of QStringListModel.
    my working C++ test class looks like:
    Qt Code:
    1. class MyModel : public QAbstractListModel
    2. {
    3. public:
    4. MyModel( QObject * parent ) : QAbstractListModel(parent) {}
    5. int rowCount( const QModelIndex & parent ) const
    6. {
    7. // we are a list, so if there is a parent return 0 (see the Qt source of QStringListModel)
    8. if (parent.isValid())
    9. return 0;
    10. return 4; // we have 4 rows
    11. }
    12.  
    13. QVariant data( const QModelIndex & index, int role /* = Qt::DisplayRole*/ ) const
    14. {
    15. // if an invalid row, return empty QVariant
    16. if (index.row() < 0 || index.row() >= 4)
    17. return QVariant();
    18.  
    19. switch( role )
    20. {
    21. case Qt::DisplayRole:
    22. case Qt::EditRole:
    23. {
    24. s = QString("row [%1]").arg( index.row() );
    25. return QVariant(s);
    26. }
    27. default:
    28. return QVariant();
    29. }
    30. }
    31. };
    To copy to clipboard, switch view to plain text mode 

    I believe that it is the 0 returned if the parent is valid from `rowCount` is what you need to do, if I just returned 4 in all cases I get no list displayed.
    Hope this solves your issue.
    Last edited by l8night; 27th September 2009 at 11:58.

Similar Threads

  1. Problem in using QHttp with QTimer
    By Ferdous in forum Newbie
    Replies: 2
    Last Post: 6th September 2008, 13:48
  2. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 10:12
  3. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 09:47
  4. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 11:35
  5. Replies: 16
    Last Post: 7th March 2006, 16:57

Tags for this Thread

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.