Results 1 to 5 of 5

Thread: QFileSystemModel and custom column / data

  1. #1
    Join Date
    Feb 2015
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default QFileSystemModel and custom column / data

    Hi everybody

    I am trying to create a little treeview based on a QFileSystem. The folders will be selected to perform several analysis with the files inside.
    I would like to provide more information in the treeview columns such as the number of files in the subfolder, some values extracted from path name etc.

    I spent several hours on google, this forum and others Qt resources and I still have difficulties to understand how it works.
    I found different solutions. The one I investigated is to create a subclass of the QFileSystemModel class and overwriting flags, data, setdata methods.
    I succeed to did it with this very basic code:


    Qt Code:
    1. TreeModel::TreeModel(const QString &path, const QStringList &headers, const MainAnalysis &data, QObject *parent)
    2. : QFileSystemModel(parent)
    3. {
    4. this->setReadOnly(true);
    5. this->setFilter(QDir::Dirs| QDir::NoDotAndDotDot);
    6. this->setRootPath(path);
    7. headerList = headers;
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. int TreeModel::columnCount(const QModelIndex& parent = QModelIndex()) const
    2. {
    3. int sup;
    4. sup = headerList.size()-QFileSystemModel::columnCount();
    5.  
    6. return QFileSystemModel::columnCount();//+sup;
    7. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const
    2. {
    3. for(uint i = 0; i<headerList.size(); i++){
    4. if (section == i && orientation == Qt::Horizontal && role == Qt::DisplayRole)
    5. return headerList.at(i);
    6. }
    7. return QFileSystemModel::headerData(section, orientation, role);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QVariant TreeModel::data(const QModelIndex& index,int role) const
    2. {
    3.  
    4. QString test;
    5. qDebug() << "index: " << index << "role: " << role;
    6. if(!index.isValid()){
    7. return QFileSystemModel::data(index,role);}
    8. if(index.column()==0){
    9. if (role == Qt::CheckStateRole) return checklist.contains(index) ? Qt::Checked : Qt::Unchecked;
    10. }
    11. if(index.column()== 2)
    12. {
    13. switch(role)
    14. {
    15. case(Qt::DisplayRole):
    16. {return QString("Your text");}
    17. case(Qt::TextAlignmentRole):
    18. {return Qt::AlignHCenter;}
    19. default:{}
    20. }
    21. }
    22.  
    23.  
    24. return QFileSystemModel::data(index,role);
    25. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. Qt::ItemFlags TreeModel::flags(const QModelIndex& index) const {
    2. return QFileSystemModel::flags(index) | Qt::ItemIsUserCheckable;
    3.  
    4. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. bool TreeModel::setData(const QModelIndex& index, const QVariant& value, int role) {
    2. if (role == Qt::CheckStateRole) {
    3. if (value == Qt::Checked) checklist.insert(index);
    4. else checklist.remove(index);
    5. emit dataChanged(index, index);
    6. return true;
    7. }
    8. return QFileSystemModel::setData(index, value, role);
    9. }
    To copy to clipboard, switch view to plain text mode 

    Parts of these codes are extracted from different wokrs found on forums... may be here.
    I understood how to create a column (done in headerData and columnCount...
    I understood -- partially-- how to add a checkbox (done in data) and handle if it is checked or not in setData

    But I don't understand how to modify item and index values... Is there a clever person to explain how to do that with in a newbie language ?

    With these functions, How I can add in the column 2 (for example) the number of subdirectories of the item ?
    How I can add the results of the operation on the item path name (a double) ?

    Thanks a lot

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QFileSystemModel and custom column / data

    But I don't understand how to modify item and index values.
    I don't know what you mean by this. Explain what you are trying to accomplish where you think you must "modify item and index values".

    There are two ways to do what you want. First is to derive from your base model (as you have done). The second is to create a proxy model that adds to what the source model provides. I personally prefer to use a proxy rather than derive from the source model. In either case though, you have to pay careful attention to parent-child relationships with model indexes. For selection to work properly, you also have to make sure that mapToSource() and mapFromSource() are correct.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFileSystemModel and custom column / data

    The easiest way is most likely to have your columns after the ones from the file system model.
    So you can always delegate all requests for column < baseColumnCount to the base class and only handle those above yourself.

    Cheers,
    _

  4. #4
    Join Date
    Feb 2015
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QFileSystemModel and custom column / data

    Quote Originally Posted by d_stranz View Post
    I don't know what you mean by this. Explain what you are trying to accomplish where you think you must "modify item and index values".

    There are two ways to do what you want. First is to derive from your base model (as you have done). The second is to create a proxy model that adds to what the source model provides. I personally prefer to use a proxy rather than derive from the source model. In either case though, you have to pay careful attention to parent-child relationships with model indexes. For selection to work properly, you also have to make sure that mapToSource() and mapFromSource() are correct.


    My apologies, I probably use wrong terms.

    I understood that a Treeview is composed by item, each items is characterized by a row, column.
    When I use the word index, I just want to say a folder or a file in the QFileSystemModel. It could be an "item" in any other environment but it is not the case in Qt so I used "index" to explain my problem. I will do some research on proxy model.

    The easiest way is most likely to have your columns after the ones from the file system model.
    So you can always delegate all requests for column < baseColumnCount to the base class and only handle those above yourself.
    Would you have a short example. I am not sure to understand clearly how to use delegate here.


    Thanks for your helps guys

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFileSystemModel and custom column / data

    Quote Originally Posted by ArnSpin View Post
    Would you have a short example. I am not sure to understand clearly how to use delegate here.
    Not a delegate in the model/view concept's sense, but delegating the call.

    Something like:
    Qt Code:
    1. int MyModel::columnCount(const QModelIndex &index) const
    2. {
    3. return QFileSystemModel::columnCount(index) + 2; // two additional columns.
    4. }
    5.  
    6. QVariant data(const QModelIndex &index, int role) const
    7. {
    8. if (index.column() >= QFileSystemModel::columnCount(index)) {
    9. // handle custom columns
    10. } else {
    11. return QFileSystemModel::data(index, role);
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Replies: 4
    Last Post: 27th February 2014, 09:42
  2. Adding custom row to QFileSystemModel
    By hiead in forum Qt Programming
    Replies: 2
    Last Post: 12th August 2013, 12:12
  3. QFileSystemModel: creating custom rows of data
    By masterlaws in forum Qt Programming
    Replies: 3
    Last Post: 12th August 2013, 11:58
  4. Replies: 5
    Last Post: 12th December 2012, 18:45
  5. Problem in getting data using QFileSystemModel
    By chinmayapadhi in forum Qt Programming
    Replies: 1
    Last Post: 15th August 2010, 19:34

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.