Results 1 to 8 of 8

Thread: QDirModel+QTreeView and checkable items

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QDirModel+QTreeView and checkable items

    Qt Code:
    1. class MyDirModel : public QDirModel
    2. {
    3. ...
    4.  
    5. private:
    6. QSet<QString> checked; // wysota's suggestion
    7. }
    8.  
    9. Qt::ItemFlags MyDirModel::flags(const QModelIndex& index) const
    10. {
    11. Qt::ItemFlags f = QDirModel::flags(index);
    12. if (index.column() == 0) // make the first column checkable
    13. f |= Qt::ItemIsUserCheckable;
    14. return f;
    15. }
    16.  
    17. QVariant MyDirModel::data(const QModelIndex& index, int role = Qt::DisplayRole) const
    18. {
    19. if (index.isValid() && index.column() == 0 && role == Qt::CheckStateRole)
    20. {
    21. // the item is checked only if we have stored its path
    22. return (checked.contains(filePath(index)) ? Qt::Checked : Qt::Unchecked);
    23. }
    24. return QDirModel::data(index, role);
    25. }
    26.  
    27. bool MyDirModel::setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole)
    28. {
    29. if (index.isValid() && index.column() == 0 && role == Qt::CheckStateRole)
    30. {
    31. // store checked paths, remove unchecked paths
    32. if (value.toInt() == Qt::Checked)
    33. checked.insert(filePath(index));
    34. else
    35. checked.remove(filePath(index));
    36. return true;
    37. }
    38. return QDirModel::setData(index, value, role);
    39. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 11th May 2007 at 18:57. Reason: corrected a confusing typo
    J-P Nurmi

  2. The following 2 users say thank you to jpn for this useful post:

    dimixan (1st June 2008), L.Marvell (11th May 2007)

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.