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. Facing difficulties to use share QAbstractItemModel between QTreeView & QListView
Qt 6.11 is out! See what's new in the release blog

Facing difficulties to use share QAbstractItemModel between QTreeView & QListView

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.3k 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.
  • P Offline
    P Offline
    pmoglaikar
    wrote on last edited by
    #1

    Hello,

    I have QAbstractItemModel implementation which share in two view [ QTreeView & QListView]

    List view contains some filters like... All Type, Type 1, Type 2, Type 3. those types represented as parent item in Tree View.
    So when i click on Type 1 from Tree View it will generate index click and set root index of list view by them. so it will display all child's items from Type 1 will be listed in List View. Its works fine for me but,

    When i want to use of combination of filters e.g. like Type 1 & Type 2, so in my list view it should display the list of all child's of Type 1, & Type 2 but it could not be possible because i can set only one root index at a time to list view. So how can i achieve this ?

    And same scenario above is applicable to All Type filter as well, how could i display all child's of Tree view item to be listed in list view.

    Any help will be really appreciated.

    Thanks
    Prashant Moglaikar

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      Hi,

      you have to craete a proxy model that does all that. The proxy must contain the mappijng between teh list views model index and the original one.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pmoglaikar
        wrote on last edited by
        #3

        Hi Gerolf,

        Thanks for reply i have implementation of proxy model but could figure it out how to map list view model index to get my expected out put could you please give me some hint or any small example so i can get understand how its be implement ?

        @
        bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow,const QModelIndex &sourceParent) const
        {
        QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
        QString checkStr = sourceModel()->data(index, Qt::DisplayRole).toString();
        return ShowThis(index);
        }

        bool MySortFilterProxyModel::ShowThis(const QModelIndex index) const
        {
        bool retVal = false;
        //Gives you the info for number of childs with a parent
        if ( sourceModel()->rowCount(index) > 0 )
        {
        for( int nChild = 0; nChild < sourceModel()->rowCount(index); nChild++)
        {
        QModelIndex childIndex = sourceModel()->index(nChild,0,index);
        if ( ! childIndex.isValid() )
        break;
        retVal = ShowThis(childIndex);
        if (retVal)
        {
        break;
        }
        }
        }
        else
        {
        QModelIndex useIndex = sourceModel()->index(index.row(), 0, index.parent());
        QString type = sourceModel()->data(useIndex, Qt::DisplayRole).toString();
        //qDebug()<<"type is "<<type;
        if ( ! type.contains(filterRegExp()))
        {
        retVal = false;
        }
        else
        {
        retVal = true;
        }
        }
        return retVal;
        }
        @

        Thanks
        Prashant

        1 Reply Last reply
        0
        • P Offline
          P Offline
          pmoglaikar
          wrote on last edited by
          #4

          I use this code currently for the filtering purpose emit signal from line edit as text changed and hide and show items in list view .

          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