Results 1 to 5 of 5

Thread: Checkable items for the view with QAbstractTableModel

  1. #1
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Checkable items for the view with QAbstractTableModel

    I have Qtreeview with the model derived from QAbstractTableModel.

    All the items in the first column has to be checkable.
    I implemented the flags() member function in the model that returns needed flag for the first item in each row.

    Qt::ItemFlags mymodel::flags ( const QModelIndex & index ) const
    {
    if (! index.isValid())
    return Qt::ItemFlag::NoItemFlags;

    if (index.column() == 0)
    {
    return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemFlag::ItemIsUserCheckable;
    }
    else
    return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    }


    Now, Qt::ItemFlags section of the documenation states:

    checkable items need to be given both a suitable set of flags and an initial state, indicating whether the item is checked or not. This is handled automatically for model/view components

    My question is: how to indicate that the "item" is checkable from within the model? I don't see any method in
    QAbstractTableModel or in QModelIndex to do this.
    Am I supposed to do somthing for CheckStateRole in the reimplemented "data" member functionЪ

    QVariant mymodel::data(const QModelIndex& index, int role) const
    {
    if (!index.isValid())
    return QVariant();

    if (role == Qt::TextAlignmentRole)
    return int(Qt::AlignRight | Qt::AlignVCenter);
    else if (role == Qt::CheckStateRole)
    {
    ??
    }
    ....
    }
    }

    Any help will be greatly appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Checkable items for the view with QAbstractTableModel

    It's Qt::CheckStateRole you are looking for.

  3. The following user says thank you to Lykurg for this useful post:

    TorAn (31st March 2010)

  4. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Checkable items for the view with QAbstractTableModel

    ??
    There you can check the item flags and return value accordingly from the data method of the model,,,,cant you ?

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

    TorAn (31st March 2010)

  6. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Checkable items for the view with QAbstractTableModel

    Model is an interface to actual data, which are stored in some underlaying data structure. So if I would like to have a list of checkable rows in my QListView then my QAbstractItemModel subclass will contain some private member like:
    Qt Code:
    1. QList<QPair<bool, QString> > m_modelData; // pair (checked?, name)
    To copy to clipboard, switch view to plain text mode 

    And in data() method it will look like this:
    Qt Code:
    1. // . . . some checks for other roles and index validity
    2. if (role == Qt::CheckStateRole) {
    3. return m_modelData.at(index.row()).first ? Qt::Checked : Qt::Unchecked;
    4. }
    5. // . . .
    To copy to clipboard, switch view to plain text mode 
    I think that you assumed that this check state would be stored somewhere by the model itself, but it is you, who is supposed to write the actual data structure where this check state would be stored.
    And don't forget about setData() where you have to update data in your structure while it is called with Qt::CheckStateRole.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  7. The following user says thank you to faldzip for this useful post:

    TorAn (31st March 2010)

  8. #5
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Checkable items for the view with QAbstractTableModel

    Thank you. After I posted my question I continued working on this issue and soon realized that I was _that close_ to the answer. I thank everyone who replied, but your answer stands out. Would you please submit it to Nokia for inclusion in Qt documentation? This will surely puts this question to the rest - I googled similar questions from 2004, so apparently I am not the one that missed the idea.

Similar Threads

  1. Making QTreeView Items Checkable
    By JimDaniel in forum Qt Programming
    Replies: 4
    Last Post: 30th January 2020, 16:43
  2. Combobox with checkable items
    By qtneuling in forum Qt Programming
    Replies: 1
    Last Post: 5th July 2008, 14:42
  3. QAbstractTableModel::flags -> The view has a checkbox?
    By kroenecker in forum Qt Programming
    Replies: 3
    Last Post: 24th June 2008, 21:10
  4. checkable items in QTableView
    By cgorac in forum Qt Programming
    Replies: 6
    Last Post: 11th March 2008, 22:45
  5. QDirModel+QTreeView and checkable items
    By L.Marvell in forum Qt Programming
    Replies: 7
    Last Post: 11th May 2007, 18:54

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.