January 11, 2011

remember2015 remember2015
Lab Rat
2 posts

How to change the colume info in QDirModel?

 

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.

  1. public class FileTree extends QTreeView{
  2.     public FileTree(QWidget parent) {
  3.         super(parent);
  4.  
  5.         //setHeaderLabels(labels);
  6.         setSortingEnabled(true);
  7.  
  8.         // Create our jambi file model
  9.         QDirModel model = new QDirModel();
  10.         //model.removeColumn(2);
  11.         setModel(model);
  12.         header().setResizeMode(QHeaderView.ResizeMode.Interactive);
  13.  
  14.         header().hideSection(3);
  15.  
  16.         this.setColumnWidth(0, 200);
  17.  
  18.         this.setAnimated(true);
  19.  
  20.     }
  21. }

6 replies

January 11, 2011

Thomas Penin Thomas Penin
Lab Rat
4 posts

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.

January 11, 2011

Volker Volker
Ant Farmer
5331 posts

“QDirModel”: http://doc.qt.nokia.com/stable/qdirmodel.html is obsolete, better use QFileSystemModel [doc.qt.nokia.com]

In your case I would write a small subclass of QSortFilterProxyModel [doc.qt.nokia.com] and reimplement filterAcceptsColumn() [doc.qt.nokia.com]

January 12, 2011

remember2015 remember2015
Lab Rat
2 posts

Volker wrote:
“QDirModel”: http://doc.qt.nokia.com/stable/qdirmodel.html is obsolete, better use QFileSystemModel [doc.qt.nokia.com]

In your case I would write a small subclass of QSortFilterProxyModel [doc.qt.nokia.com] and reimplement filterAcceptsColumn() [doc.qt.nokia.com]

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

January 12, 2011

Volker Volker
Ant Farmer
5331 posts

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

  1. class FSMProxy : public QSortFilterProxyModel
  2. {
  3.     Q_OBJECT
  4.  
  5. public:
  6.     FSMProxy(QObject *parent);
  7.  
  8. protected:
  9.     bool filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const;
  10. };
  11.  
  12.  
  13.  
  14. FSMProxy::FSMProxy(QObject *parent)
  15.     : QSortFilterProxyModel(parent)
  16. {
  17. }
  18.  
  19.  
  20. bool FSMProxy::filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const
  21. {
  22.     Q_UNUSED(source_parent)
  23.  
  24.     // adjust the columns you filter out here
  25.     // return false for those that will be hidden
  26.     if(source_column == 2 || source_column == 3)
  27.         return false;
  28.     return true;
  29. }
  30.  
  31. // [EDIT:] and add in your UI class:
  32.     QFileSystemModel *fsm = new QFileSystemModel(this);
  33.     FSMProxy *proxy = new FSMProxy(this);
  34.     proxy->setSourceModel(fsm);
  35.     treeView->setModel(proxy);

January 12, 2011

remember2015 remember2015
Lab Rat
2 posts

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

  1. class FSMProxy : public QSortFilterProxyModel
  2. {
  3.     Q_OBJECT
  4.  
  5. public:
  6.     FSMProxy(QObject *parent);
  7.  
  8. protected:
  9.     bool filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const;
  10. };
  11.  
  12.  
  13.  
  14. FSMProxy::FSMProxy(QObject *parent)
  15.     : QSortFilterProxyModel(parent)
  16. {
  17. }
  18.  
  19.  
  20. bool FSMProxy::filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const
  21. {
  22.     Q_UNUSED(source_parent)
  23.  
  24.     // adjust the columns you filter out here
  25.     // return false for those that will be hidden
  26.     if(source_column == 2 || source_column == 3)
  27.         return false;
  28.     return true;
  29. }
  30.  
  31. // [EDIT:] and add in your UI class:
  32.     QFileSystemModel *fsm = new QFileSystemModel(this);
  33.     FSMProxy *proxy = new FSMProxy(this);
  34.     proxy->setSourceModel(fsm);
  35.     treeView->setModel(proxy);

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

January 12, 2011

Volker Volker
Ant Farmer
5331 posts

It’s in the wiki [developer.qt.nokia.com] now.

 
  ‹‹ QImage - Retrieve data from an 8 bit bitmap image      Work with video files (movies), audio, images ››

You must log in to post a reply. Not a member yet? Register here!