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.
@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); }
}@
-
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.
-
"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 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...
-
This does the trick, in case you never played with the item models:
@
class FSMProxy : public QSortFilterProxyModel
{
Q_OBJECTpublic:
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 author="Volker" date="1294828371"]This does the trick, in case you never played with the item models:
@
class FSMProxy : public QSortFilterProxyModel
{
Q_OBJECTpublic:
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... -
It's in the "wiki":http://developer.qt.nokia.com/wiki/Filter_Columns_in_a_QFileSystemModel now.