I’m trying to show a list of media files with Qt. For that I’m using a QTableView
with a column for the preview and a column for the file name. With the following code, the file name is shown but the preview column stays empty.
#include <filesystem>
#include <QHeaderView>
#include <QImage>
#include <QImageReader>
#include <QPixmap>
#include <QStandardItemModel>
#include <QVariant>
#include <easyqt/logging.hxx>
#include "medialist.hxx"
namespace pelican {
MediaList::MediaList() {
verticalHeader()->hide();
_model = new QStandardItemModel;
setModel(_model);
_model->setHorizontalHeaderLabels({"Preview", "Filename"});
std::filesystem::path mediaPath("/home/username/Pictures/Q");
long long int row = 0;
for (const auto& entry : std::filesystem::directory_iterator(mediaPath)) {
QImage preview(entry.path().c_str());
QStandardItem* previewItem = new QStandardItem();
previewItem->setData(QVariant(QPixmap::fromImage(preview)), Qt::DecorationRole);
_model->setItem(row, 0, previewItem);
_model->setData(_model->index(row, 1), entry.path().c_str());
}
}
}