Results 1 to 6 of 6

Thread: Checkboxes in Treeview do not get refreshed

  1. #1
    Join Date
    Jan 2008
    Posts
    3
    Thanks
    1

    Default Checkboxes in Treeview do not get refreshed

    Hi!

    I have a QTreeview which displays a FilesystemModel with Checkboxes before the files. If a directory gets checked and has childs, all the subitems should get checked.
    The logic is working BUT the view does not get refreshed!
    If I have the directory with sub-items expanded I have to collapse/expand it again to see the (correct) rendered result.
    The code in question is this:
    Qt Code:
    1. QVariant FileModel::data(const QModelIndex &index, int role) const
    2. {
    3. if(index.isValid() && index.column() == 0 && role == Qt::CheckStateRole) {
    4. const QString path = this->filePath(index);
    5. if(m_files.isEmpty()) {
    6. return Qt::Unchecked;
    7. }
    8. if(m_files.contains(path)) {
    9. return Qt::Checked;
    10. }
    11. if(m_excludeFiles.contains(path)) {
    12. return Qt::Unchecked;
    13. }
    14. if(parentIsExcluded(path)) {
    15. return Qt::Unchecked;
    16. }
    17. if(parentIsChecked(path)){
    18. return Qt::Checked;
    19. }
    20. return Qt::Unchecked;
    21. }
    22. return QFileSystemModel::data(index, role);
    23. }
    To copy to clipboard, switch view to plain text mode 

    The flags method returns
    Qt Code:
    1. defaultFlags |= Qt::ItemIsUserCheckable;
    To copy to clipboard, switch view to plain text mode 
    setData just fills my QSets (m_files, m_excludeFiles) and returns true or calls QFileSystemModel::setData(index, value, role);

    What am I missing?
    Thank you very much

    m.

  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: Checkboxes in Treeview do not get refreshed

    This is not the way to go because of at least two reasons. One is that you don't allow the user to uncheck any of the boxes (unless that's what you want) because the checkbox state is unrelated to any of the user's actions. The other is that you didn't inform the views that data of the model has changed. You need to emit dataChanged() signal for the range of indexes that had their data (like the check state) changed and the signal should be emitted each time you change m_files, m_excludeFiles or anything that might make parentIsExcluded() or parentIsChecked() change return values for the same indexes.

  3. #3
    Join Date
    Jan 2008
    Posts
    3
    Thanks
    1

    Default Re: Checkboxes in Treeview do not get refreshed

    Thank you for your response -

    The user is able to change the state of the checkboxes:
    Qt Code:
    1. bool FileModel::setData(const QModelIndex& index, const QVariant& value, int role)
    2. {
    3. if(index.isValid() && index.column() == 0 && role == Qt::CheckStateRole) {
    4. QString path = filePath(index);
    5. if(value.toInt() == Qt::Checked) {
    6. m_files.insert(path);
    7. m_excludeFiles.remove(path);
    8. } else {
    9. if(parentIsChecked(path)) {
    10. m_excludeFiles.insert(path);
    11. } else {
    12. m_files.remove(path);
    13. }
    14. }
    15. emit dataChanged(index, index); // here I have to check if how many items are visible to inform the view???
    16. return true;
    17. }
    18. return QFileSystemModel::setData(index, value, role);
    19. }
    To copy to clipboard, switch view to plain text mode 

    My first try to accomplish this "check-the-files-in-subfolders-automatically" was quite as you suggested:
    I checked every Item recursively and therefore knew all the indexes that changed and could emit the dataChanged-signal - but performance was of course very low - especially if you checked a very top-level directory.
    So I thought about it and came to the following conclusion:
    All files are treated recursively (all of their content is included or excluded) and depend solely on their parent item (or itself when clicked)

    And so I thought that if I return the correct state in the ::data method it is enough.
    The function gets called and if I add debug output I can see it returns the correct values - but I have to move the mouse over the items or collapse/expand to see the end result.

    Is there a way to refresh all the visible items or do I have to think of another way to achive the desired behaviour described above?

    Thanks!
    Last edited by mesch.t; 18th February 2009 at 08:17.

  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: Checkboxes in Treeview do not get refreshed

    You can try emitting layoutChanged() for the most parent item instead of dataChanged() for subitems.

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

    drsm (18th March 2009), mesch.t (18th February 2009)

  6. #5
    Join Date
    Jan 2008
    Posts
    3
    Thanks
    1

    Default Re: Checkboxes in Treeview do not get refreshed

    Yes!
    That did it and it's fast!
    Thank you!

  7. #6
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Wink Re: Checkboxes in Treeview do not get refreshed

    mesch.t, I am trying to resolve the same issue you have encounter in this thread. Can you provide some code snippet for your solution? Thanks!
    Last edited by phamtv; 13th April 2011 at 23:26.

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.