Important: Please read the Qt Code of Conduct -

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 {
    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


    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.

    Ah nice, but where is "d" defined in qfilesystemmodel.cpp?

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


  • 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:
    ! QFilesystemModel)!

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

    ! 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