Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to show/hide checkboxes “on the fly” on a QFileSystemModel?

How to show/hide checkboxes “on the fly” on a QFileSystemModel?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    calak
    wrote on last edited by
    #1

    I work on QFileSystemModel to make it more flexible.

    I've set the directory/file checkable
    Setted empty dirs non-collapsable
    Added the support of Tri-state to _PartiallyCheck_ parents of checked elements
    Show/Hide "on the fly" PartiallyChecks
    ...
    

    But a this point, i block on a behavior.

    I want to show/hide "Checked/Unchecked" checkboxes on the fly

    So, I've edit my flag method:

    @Qt::ItemFlags FSModel::flags(const QModelIndex& index) const
    {
    Qt::ItemFlags flag = QFileSystemModel::flags(index);

    // set flag to have only first column checkable
    if ((index.column() == 0) && (m_user_checkable)) {
        flag |= Qt::ItemIsUserCheckable;
    }
    
    return flag;
    

    }@

    note: m_user_checkable is a boolean which active/desactive checkboxes for the model.

    I've put "@qDebug() << filePath(index) << index.flags();@" inside my handler, for outputs flags of the index where I click, to see his value before and after "m_user_checkable" changes. So, I'm sure that index flags change but the checkboxes stay visibles :/

    Did you know a way to hide these checkboxes once they are showed ?

    PS: ask if you want the whole code here... (~ 500 lines)

    Thanks

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      your model needs to tell the view that something has changed. Meaning tell the view that a item's checkbox isn't available anymore.
      You may try to trigger QAbstractItemModel::dataChanged() signal when a checkbox for an index isn't available anymore.
      Then the view should regather the data from the model...

      Alternatively you can call QAbstractItemModel::reset() if the checkboxes for all indexes changes.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        It sounds to me you are looking for "something like this":/wiki/QSortFilterProxyModel_subclass_to_add_a_checkbox ?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          calak
          wrote on last edited by
          #4

          Not really, my code already provides all these features.

          Now, I want to build a function which show/hide checkboxes once tree is already rendered.

          Imagine the situation: you have a button, and if you click it, it call the fonction that show or hide checkboxes of the list.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            calak
            wrote on last edited by
            #5

            [quote author="raven-worx" date="1375442462"]your model needs to tell the view that something has changed. Meaning tell the view that a item's checkbox isn't available anymore.
            You may try to trigger QAbstractItemModel::dataChanged() signal when a checkbox for an index isn't available anymore.
            Then the view should regather the data from the model...

            Alternatively you can call QAbstractItemModel::reset() if the checkboxes for all indexes changes.[/quote]

            Sry, i wasn't pay attention to your message before...
            I had already added "dataChanged()", but your message gave me indirectly the solution...

            dataChanged... Data changed... hum, data had changed? Oh my god, the datas have changed...

            I hadn't added the condition in the data() method

            @QVariant FSModel::data(const QModelIndex& index, int role) const
            {
            if (index.isValid()) {
            // HERE, i added "m_user_checkable && " before "role == Qt::CheckSt..."
            if (m_user_checkable && role == Qt::CheckStateRole && index.column() == 0) {
            // item checked only if we have stored its path
            if (m_checked.contains(filePath(index))) {
            return Qt::Checked;
            }
            if (m_partially_check_parents && m_partially_checked.contains(filePath(index))) {
            return Qt::PartiallyChecked;
            }
            return Qt::Unchecked;
            }
            }
            return QFileSystemModel::data(index, role);
            }@

            Just this boolean... now it works perfectly :p

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved