Adding additional String to Qtreeview's item



  • I managed to change the icon of a Qtreeview by overriding the data Function as follows:

    @class MyQFileSystemModel : public QFileSystemModel {
    public:
    QVariant data( const QModelIndex& index, int role ) const {

        if( role == Qt::DecorationRole ) {
            return QVariant(QIcon(QPixmap(":/myresources/images/folder-icon.png")));
        }
    
        return QFileSystemModel::data(index, role);
    }
    

    };@

    What function do i have to override, to add an additional information to the listed string in the QTreeview, i want to add some numbers to it.


  • Lifetime Qt Champion

    Hi,

    Do you mean to the data in cells ? If so, same function with QDisplayRole.



  • Note that your reimplementation is incorrect. You need to check for validity of the index that is requested data for. If the index is not valid for your model, return a QVariant().

    Also, you probably don't want to add this pixmap to every item in the model, but only to a certain column.



  • @Andre yes i should definitely check for the validity of index! At the moment i just have one column and it will stay one column, so i don't need to check for that.

    @Sgaist
    Ah nice, but where is "d" defined in qfilesystemmodel.cpp?
    d->name(index)

    I need this, as i would like to add the count (number of items) to the TreeView Items.

    Thanks!



  • As you are inheriting from QFileSystemModel, which as more than one column, it seemed very logical that you in fact do have more than one column.

    It seems to me you are abusing QFileSystemModel for something it is not mend to be doing.



  • @Andre

    This is how a QFileSystemModel looks like non-modified:
    !http://doc-snapshot.qt-project.org/qt5-5.4/images/stylesheet-treeview.png(Default QFilesystemModel)!

    And this is how i modified it (i added the number in the parenthesis for demonstration)

    !http://i61.tinypic.com/14ta9u.png(Modified QFilesystemModel)!
    So what would be the proper way to add the number of Files to the Folder then?



  • I'd probably use a proxy model for that.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.