Qt-interest Archive, January 2008
QListView, QDirModel and QSortFilterProxyModel
Message 1 in thread
I am trying to hook up a QListView that displays a list of files
within a directory and then allow the user to filter the files using a
QSortFilterProxyModel. Here is my current code:
void QImageImport::_populateFileTable()
{
QDirModel* dirModel = new QDirModel(fileListView);
dirModel->setFilter(QDir::Files|QDir::Hidden|QDir::System);
_proxyModel->setSourceModel(dirModel);
QModelIndex rootIndex =
dirModel->index(this->sourceDirectoryLineEdit->text(), 0);
fileListView->setRootIndex( rootIndex );
}
The last line throws an invalid pointer exception. I kinda understand
what is going on but I am not sure how to solve the problem. Basically,
I have a File Dialog open where the user will select a folder. The path
to the folder is stored in a QLineEdit, then control is passed off to
the _populateFileTable() method. I wand the QDirModel to have a root
index that is the path that was selected. I can see that I am setting
the root index for the ListView to a model that the listview does not
really "own", ie, the rootIndex I am getting is to the QDirModel and
NOT the proxymodel. Now, my proxy model is actually a subclass of
QSortFilterProxyModel where I have over-ridden the proper methods ( I
think). Here is the class declaration:
class FileListSortProxyModel : public QSortFilterProxyModel
{
Q_OBJECT;
public:
FileListSortProxyModel(QObject *parent = 0);
virtual ~FileListSortProxyModel();
protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex
&sourceParent) const;
bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
private:
FileListSortProxyModel(const FileListSortProxyModel&); // Copy
Constructor Not Implemented
void operator=(const FileListSortProxyModel&); // Operator '=' Not
Implemented
};
Could some one give me a hint on how to solve this?
Thanks
Mike Jackson
--
[ signature omitted ]
Message 2 in thread
You have to map the index from dirModel to Your _proxyModel....
See QAbstractProxyModel::mapFromSource or mapToSource
Regards
Karl-Heinz
Ing-büro reichel-langer
Karl-Heinz Reichel
www.techdrivers.de
> -----Ursprüngliche Nachricht-----
> Von: Mike Jackson [mailto:imikejackson@xxxxxxxxx]
> Gesendet: Montag, 28. Januar 2008 17:07
> An: qt-interest@xxxxxxxxxxxxx
> Betreff: QListView, QDirModel and QSortFilterProxyModel
>
> I am trying to hook up a QListView that displays a list of files
> within a directory and then allow the user to filter the files using a
> QSortFilterProxyModel. Here is my current code:
>
> void QImageImport::_populateFileTable()
> {
> QDirModel* dirModel = new QDirModel(fileListView);
> dirModel->setFilter(QDir::Files|QDir::Hidden|QDir::System);
> _proxyModel->setSourceModel(dirModel);
> QModelIndex rootIndex =
> dirModel->index(this->sourceDirectoryLineEdit->text(), 0);
> fileListView->setRootIndex( rootIndex );
> }
>
>
> The last line throws an invalid pointer exception. I kinda understand
> what is going on but I am not sure how to solve the problem. Basically,
> I have a File Dialog open where the user will select a folder. The path
> to the folder is stored in a QLineEdit, then control is passed off to
> the _populateFileTable() method. I wand the QDirModel to have a root
> index that is the path that was selected. I can see that I am setting
> the root index for the ListView to a model that the listview does not
> really "own", ie, the rootIndex I am getting is to the QDirModel and
> NOT the proxymodel. Now, my proxy model is actually a subclass of
> QSortFilterProxyModel where I have over-ridden the proper methods ( I
> think). Here is the class declaration:
>
> class FileListSortProxyModel : public QSortFilterProxyModel
> {
> Q_OBJECT;
>
> public:
> FileListSortProxyModel(QObject *parent = 0);
> virtual ~FileListSortProxyModel();
>
> protected:
> bool filterAcceptsRow(int sourceRow, const QModelIndex
> &sourceParent) const;
> bool lessThan(const QModelIndex &left, const QModelIndex &right)
> const;
>
> private:
> FileListSortProxyModel(const FileListSortProxyModel&); // Copy
> Constructor Not Implemented
> void operator=(const FileListSortProxyModel&); // Operator '=' Not
> Implemented
>
> };
>
>
> Could some one give me a hint on how to solve this?
>
> Thanks
> Mike Jackson
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
--
[ signature omitted ]