Results 1 to 5 of 5

Thread: QAbstractItemModel derivative showing a checkbox next to each index

  1. #1
    Join Date
    Aug 2013
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QAbstractItemModel derivative showing a checkbox next to each index

    Hello.

    I am creating an FtpFileSystemModel, and, for that, I am using QAbstractItemModel as a base, which I have never subclassed before.

    After quite a few problems I have reached the point where my ftp files are displayed in a QTreeView, just like a QFileSystemModel would do with a local fs.

    There are a few quirks here and there, though. The thing that worries me is that all the indexes (or "cells"), show a ticker next to them. This is better illustrated with a screenshot:

    20140517.122325.jpg

    See the ftp file listing to the middle-right portion of the screenshot.

    Before you ask, the flags() method is this:

    Qt Code:
    1. Qt::ItemFlags FtpFileSystemModel::flags(const QModelIndex &index) const
    2. {
    3. if(!index.isValid())
    4. {
    5. return 0;
    6. }
    7.  
    8. Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    9. qDebug() << Q_FUNC_INFO << QString("flags: %1").arg(flags);
    10. return flags;
    11. }
    To copy to clipboard, switch view to plain text mode 

    As you see, neither of Qt::ItemIsUserCheckable or Qt::ItemIsTristate is set.

    I have also a problem with alignment, but I haven't dug much into it. I am setting these in data(), but they don't seem to do anything:

    Qt Code:
    1. if(role == Qt::TextAlignmentRole)
    2. {
    3. Qt::Alignment flags = 0;
    4.  
    5. if(index.column() == 0 || index.column() == 1)
    6. {
    7. flags = Qt::AlignVCenter | Qt::AlignLeft;
    8. }
    9. else
    10. {
    11. flags = Qt::AlignVCenter | Qt::AlignRight;
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for any idea you can share

  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: QAbstractItemModel derivative showing a checkbox next to each index

    This is most likely unrelated to flags() or alignment, the most likely cause is that your data() method returns something other than a null QVariant when it is asked for the CheckStateRole.

    A common mistake is to forget to check the "role" argument that is passed to data().

    Cheers,
    _

  3. #3
    Join Date
    Aug 2013
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QAbstractItemModel derivative showing a checkbox next to each index

    It worked like a charm. Thanks.

    But.... out of curiosity, how are you supposed to know that? I can't see that info in the subclassing document for QAbstractItemModel not even in the internet...

    Again, thank you

  4. #4
    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: QAbstractItemModel derivative showing a checkbox next to each index

    The API contract of the data() method is that it returns a null variant for all indexes or roles that it does not have actual data for.

    From QAbstractItemModel::data()

    Note: If you do not have a value to return, return an invalid QVariant instead of returning 0.
    Cheers,
    _

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

    i92guboj (28th May 2014)

  6. #5
    Join Date
    Aug 2013
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QAbstractItemModel derivative showing a checkbox next to each index

    Ok. Thanks. Now I think I fully got it. Some more checks are due now in my code :lol:

    Thank you.

Similar Threads

  1. Showing Icon and tooltip into QSqlQueryModel index
    By cia.michele in forum Qt Programming
    Replies: 0
    Last Post: 19th February 2012, 13:58
  2. Replies: 0
    Last Post: 12th January 2012, 06:41
  3. implementing sizeHint in a QItemDelegate derivative
    By momesana in forum Qt Programming
    Replies: 2
    Last Post: 5th May 2010, 21:59
  4. QAbstractItemModel reset Persistent Index
    By larcho in forum Qt Programming
    Replies: 8
    Last Post: 5th April 2010, 05:01
  5. Replies: 0
    Last Post: 10th September 2009, 09:43

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.