I was reading MVC tutorial and wanted to try out the code, but for some reason (which I'm not able to figure out) it is not working.
This code is suppose to show contents of current directory in QListWidget.
#include <QApplication>
#include <QFileSystemModel>
#include <QModelIndex>
#include <QListWidget>
#include <QListView>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFileSystemModel *model = new QFileSystemModel;
QString dir = QDir::currentPath();
model->setRootPath(dir);
QModelIndex parentIndex = model->index(dir);
int numRows = model->rowCount(parentIndex);
QListWidget *list = new QListWidget;
QListWidgetItem *newItem = new QListWidgetItem;
for(int row = 0; row < numRows; ++row) {
QModelIndex index = model->index(row, 0, parentIndex);
QString text = model->data(index, Qt::DisplayRole).toString();
newItem->setText(text);
list->insertItem(row, newItem);
}
list->show();
return a.exec();
}
There are 2 problems.
The first described by Frank Osterfeld's answer. Move:
into your loop.
The second has to do with
QFileSystemModel
's threading model. from the docs forQFileSystemModel
:and
I don't think
QFileSystemModel()
will work properly until after the Qt event loop is running (which is started bya.exec()
in your example).In your case,
model->rowCount(parentIndex)
returns 0, even though there are items in the directory (at least that's what it's doing on my test).Replacing
QFileSystemModel
withQDirModel
(and removing themodel->setRootPath(dir)
call, which QDirModel` doesn't support) populates the list.You must create a new item for each row. Move
into the for loop.