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 change the colume info in QDirModel?
Qt 6.11 is out! See what's new in the release blog

How to change the colume info in QDirModel?

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

    Now I use QDirModel for the model of a QTreeView. There are 4 columns natively produced by QDirModel: 'name', 'size', 'type', 'Date modified' and I want hide the Date field and change size info column to author info column.
    @public class FileTree extends QTreeView{
    public FileTree(QWidget parent) {
    super(parent);

        //setHeaderLabels(labels);
        setSortingEnabled(true);
    
        // Create our jambi file model
        QDirModel model = new QDirModel();
        //model.removeColumn(2);
        setModel(model);
        header().setResizeMode(QHeaderView.ResizeMode.Interactive);
    
        header().hideSection(3);
    
        this.setColumnWidth(0, 200);
    
        this.setAnimated(true); 
    
    }
    

    }@

    1 Reply Last reply
    0
    • T Offline
      T Offline
      Thomas Penin
      wrote on last edited by
      #2

      I suppose that you can subclass QDirModel and use this subclass as the new model for your view. Try reimplementing columnCount(), index() and data() to remove the column you do not want (or alternatively just hide this column in your view) and headerData() to change the name of the "Size info" column.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        "QDirModel": http://doc.qt.nokia.com/stable/qdirmodel.html is obsolete, better use "QFileSystemModel":http://doc.qt.nokia.com/stable/qfilesystemmodel.html

        In your case I would write a small subclass of "QSortFilterProxyModel":http://doc.qt.nokia.com/stable/qsortfilterproxymodel.html and reimplement "filterAcceptsColumn() ":http://doc.qt.nokia.com/stable/qsortfilterproxymodel.html#filterAcceptsColumn

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • R Offline
          R Offline
          remember2015
          wrote on last edited by
          #4

          [quote author="Volker" date="1294750923"]"QDirModel": http://doc.qt.nokia.com/stable/qdirmodel.html is obsolete, better use "QFileSystemModel":http://doc.qt.nokia.com/stable/qfilesystemmodel.html

          In your case I would write a small subclass of "QSortFilterProxyModel":http://doc.qt.nokia.com/stable/qsortfilterproxymodel.html and reimplement "filterAcceptsColumn() ":http://doc.qt.nokia.com/stable/qsortfilterproxymodel.html#filterAcceptsColumn[/quote]

          Thanks for your reply. I'll try it. The Area 51 Engineer...

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            This does the trick, in case you never played with the item models:

            @
            class FSMProxy : public QSortFilterProxyModel
            {
            Q_OBJECT

            public:
            FSMProxy(QObject *parent);

            protected:
            bool filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const;
            };

            FSMProxy::FSMProxy(QObject *parent)
            : QSortFilterProxyModel(parent)
            {
            }

            bool FSMProxy::filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const
            {
            Q_UNUSED(source_parent)

            // adjust the columns you filter out here
            // return false for those that will be hidden
            if(source_column == 2 || source_column == 3)
                return false;
            return true;
            

            }

            // [EDIT:] and add in your UI class:
            QFileSystemModel *fsm = new QFileSystemModel(this);
            FSMProxy *proxy = new FSMProxy(this);
            proxy->setSourceModel(fsm);
            treeView->setModel(proxy);
            @

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • R Offline
              R Offline
              remember2015
              wrote on last edited by
              #6

              [quote author="Volker" date="1294828371"]This does the trick, in case you never played with the item models:

              @
              class FSMProxy : public QSortFilterProxyModel
              {
              Q_OBJECT

              public:
              FSMProxy(QObject *parent);

              protected:
              bool filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const;
              };

              FSMProxy::FSMProxy(QObject *parent)
              : QSortFilterProxyModel(parent)
              {
              }

              bool FSMProxy::filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const
              {
              Q_UNUSED(source_parent)

              // adjust the columns you filter out here
              // return false for those that will be hidden
              if(source_column == 2 || source_column == 3)
                  return false;
              return true;
              

              }

              // [EDIT:] and add in your UI class:
              QFileSystemModel *fsm = new QFileSystemModel(this);
              FSMProxy *proxy = new FSMProxy(this);
              proxy->setSourceModel(fsm);
              treeView->setModel(proxy);
              @
              [/quote]

              It works.It's a elegant way to use the proxy model.
              Thank you a lot...

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                It's in the "wiki":http://developer.qt.nokia.com/wiki/Filter_Columns_in_a_QFileSystemModel now.

                http://www.catb.org/~esr/faqs/smart-questions.html

                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