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. Adding additional String to Qtreeview's item
Forum Updated to NodeBB v4.3 + New Features

Adding additional String to Qtreeview's item

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 1.8k 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.
  • Y Offline
    Y Offline
    yafes
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • Y Offline
          Y Offline
          yafes
          wrote on last edited by
          #4

          @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!

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • Y Offline
              Y Offline
              yafes
              wrote on last edited by
              #6

              @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?

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                I'd probably use a proxy model for that.

                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