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 add custom row in QFileSystemModel?
Forum Updated to NodeBB v4.3 + New Features

How to add custom row in QFileSystemModel?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 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.
  • V Offline
    V Offline
    v-odin
    wrote on 16 Jan 2012, 20:34 last edited by
    #1

    I am using QFileSystemModel to represent file structure through the QTreView. Everything works fine, but I need to add an additional row at some level of the tree. For example for now is:

    -root

    --row1

    --row2

    --row3

    All these rows mapping folders/files from file system. I need:

    -root

    --row1

    --row2

    --row3

    --custom row

    So custom row is not representing any data from file system. I just need to add here my own data. I have read a lot of stuff from the internet and people advice to use proxy model and reimplement rowCount(), data() and flags() functions. I tried to do that(used class derived from QSortFilterProxyModel), but I never get my row in data() and flags() functions. Seems like it takes count from source model.

    @QVariant AddonFilterModel::data (const QModelIndex & index, int role) const
    {
    if(role == Qt::DisplayRole && index.row() == FilterModel::rowCount(index))
    {
    return QString("Add-Ons");
    }

    return FilterModel::data(index, role);
    

    }

    Qt::ItemFlags AddonFilterModel::flags(const QModelIndex & index) const
    {
    if (!index.isValid())
    return 0;

    if (index.row() == FilterModel::rowCount(index))
    {
        return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    }
    
    return FilterModel::flags(index);
    

    }

    int AddonFilterModel::rowCount(const QModelIndex &parent) const
    {
    int count = FilterModel::rowCount(parent);

    if(parent == this->getRootIndex())
    {
        return count+1;
    }
    return count;
    

    }@

    Using class derived from QAbstractProxyModel is not acceptable because I need filtering functions of QSortFilterProxyModel().

    Also I have tried to reimplement rowCount() of QFileSystemModel to make changes directly in model but I am getting "array out of range" error from QT code.

    I have tried insertRow() method but it is not working. I think because QFileSystemModel is read only.

    Did anyone face this problem? Any ideas?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      ludde
      wrote on 17 Jan 2012, 15:08 last edited by
      #2

      I'm not sure you can add rows using a QSortFilterProxyModel subclass. They are intended for removing rows and reordering rows, I think. If you can make it work using a QAbstractProxyModel subclass, you can always use both classes (QSortFilterProxyModel subclass and QAbstractProxyModel subclass), making one of them use the other as its model.

      1 Reply Last reply
      0
      • V Offline
        V Offline
        v-odin
        wrote on 18 Jan 2012, 13:59 last edited by
        #3

        The problem is(as I said before) that I never get my row in data() and flags() functions. It means that when I reimplement rowCount() function actual row count is not changing. UI reserve space for the extra row but data() and flags() are not processing extra row. So the main question is how to add extra row using QSortFilterProxyModel()?

        1 Reply Last reply
        0
        • L Offline
          L Offline
          ludde
          wrote on 18 Jan 2012, 14:13 last edited by
          #4

          Well, as I said, I don't think you can use QSortFilterProxyModel to add an extra row. It is used to sort and filter rows.

          To add an extra row, I believe you have to subclass QAbstractProxyModel.

          1 Reply Last reply
          0

          1/4

          16 Jan 2012, 20:34

          • Login

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