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. Right way to sort by checkble column in treeview using QSortFilterProxyModel subclass
Forum Updated to NodeBB v4.3 + New Features

Right way to sort by checkble column in treeview using QSortFilterProxyModel subclass

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 3.2k 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.
  • U Offline
    U Offline
    umen242
    wrote on last edited by
    #1

    i have treeview subclass that using QSortFilterProxyModel subclass to sort by column items , when the item is string it is working fine
    but when the item is checkble type , and i like to sort by checked items . in the QSortFilterProxyModel it just dosnt work.
    here is my code for lessThan methods and where i capture the checkbox state.

    @void SettingsDialog::tree_itemChanged(QStandardItem *item)
    {

     Qt::CheckState checkState = item->checkState();
     if(checkState == Qt::Checked)
     {
    

    item->setCheckState(Qt::Checked);
    item->setData(true,Qt::UserRole);

     }
     else if(checkState == Qt::Unchecked)
     {
    

    item->setCheckState(Qt::Unchecked);
    item->setData(false,Qt::UserRole);

     }
    

    }


    bool SettingsSortFilterProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
    {

    int columnLeftIndx = left.column();
    int columnRightIndx = right.column();

    if(columnLeftIndx == SETTINGS_CHECKBOX_CELL_IDX)
    {

    bool leftUint = sourceModel()->data( left,Qt::UserRole ).toBool();
    bool rightUint = sourceModel()->data( right,Qt::UserRole ).toBool();
    return leftUint < rightUint;
    }
    @

    what im doing wrong here ?

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

      Is this code complete? It looks like part of the lessThan method is missing?

      1 Reply Last reply
      0
      • U Offline
        U Offline
        umen242
        wrote on last edited by
        #3

        it just check if the sort coming from the second column and then try to do the sort .
        it is complete , as i dont really know what else i need to put

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

          The code seems incomplete, because the last brace is not there. So I was wondering if something else might have been dropped.

          Perhaps you should be more clear on what kind of result you are expecting. I guess you want your checked items to be sorted above the unchecked ones, but you still want to keep sorting on the text itself as well, right?

          Edit:
          So, is this what you are after?

          Derrek

          Zach

          Andy

          Bernard

          Freddy

          William

          1 Reply Last reply
          0
          • U Offline
            U Offline
            umen242
            wrote on last edited by
            #5

            Hi
            the last brace just missing when i copied the text , i just want the checked item to be first.
            with not text related to the sort .
            the check-boxs are in separated item.
            Thanks again

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

              Try this:

              @
              bool SettingsSortFilterProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
              {
              int columnLeftIndx = left.column();
              int columnRightIndx = right.column();

              ASSERT(columnLeftIndx == columnRightIndx);

              if(columnLeftIndx == SETTINGS_CHECKBOX_CELL_IDX)
              {

              bool l = sourceModel()->data( left,Qt::UserRole ).toBool();
              bool r = sourceModel()->data( right,Qt::UserRole ).toBool();
              return (l && !r); //only return true if the left item is checked and the right one is not--
              } else {
              return QSortFilterProxyModel::qLess(left, right);
              }
              }
              @

              1 Reply Last reply
              0
              • U Offline
                U Offline
                umen242
                wrote on last edited by
                #7

                Hi thanks for the replay , but it dosn't work . i must doing something wrong .
                @bool l = sourceModel()->data( left,Qt::UserRole ).toBool();
                bool r = sourceModel()->data( right,Qt::UserRole ).toBool();@
                always gives me true. no matter what .
                also :
                @QSortFilterProxyModel::qLess(left, right);
                gives me : error C2039: 'qLess' : is not a member of 'QSortFilterProxyModel'@

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

                  So why not use the checkStateRole directly then?

                  Note that the function name should be lessThan(), not qLess()

                  1 Reply Last reply
                  0
                  • U Offline
                    U Offline
                    umen242
                    wrote on last edited by
                    #9

                    Finally ! Thanks Andre

                    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