How do I select a row in a QTreeview programatically? - qt

Update: Let me try to simplify the issue and steps. Does anyone see where I've gone wrong? Also, this is on OSX 10.7.5/Qt 5.1 – perhaps it is an OSX issue?
I pass an absolute path which points to a directory of images and set the root path of my QFileSystemModel instance and use the index returned by that to set my QTreeView instance root index
QModelIndex idx = dirmodel->setRootPath(dPath);
myTreeView->setRootIndex(idx);
I then set the current index of the treeview to that index. Which I guess is pointing to the directory – perhaps this is wrong?
myTreeView->setCurrentIndex(idx);
I have tried using the selectionModel of the treeView to make the selection using that index:
myTreeView->selectionModel()->select(idx,
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
I have also tried accessing a child of the directory and using that to set the current index and selection:
QModelIndex next_index =myTreeView->currentIndex().child(1,0);
But I still am not getting the selection on my treeview.
Context: For a Qt learning project I am building an image viewer. I have a 'QTreeView' with a backing 'QFileSystemModel.' The user has the option to select a directory of images and only the contents of that directory will be displayed in the QTreeView (i.e. it is not displaying the entire file system, just the subdirectory the user selects).
I would like to have the first item in the QTreeView selected when the user changes directories. I have seen this question (Selecting a row in QTreeView programatically) but it didn't work for me. I've been poking at this for a few hours now. What am I doing wrong? I am able to select directories and display the images but I am missing how to set the QTreeView selection.
My code is below, with various attempts commented out. The console output from the qDebug statement is always:
next_index -1 -1
void DirBrowser::on_actionSet_Image_Directory_triggered()
{
QString dPath = QFileDialog::getExistingDirectory(this,
tr("Set Image Drectory"), QDir::currentPath());
if (!dPath.isEmpty()) {
QModelIndex idx = dirmodel->setRootPath(dPath);
myTreeView->setRootIndex(idx);
myTreeView->setCurrentIndex(idx);
QModelIndex next_index =myTreeView->currentIndex().sibling(1,0);
//QModelIndex next_index = myTreeView->model()->index(1, 0);
//QModelIndex next_index =idx.child(0,0);
qDebug() << "next_index" << next_index.row() << next_index.column();
//myTreeView->setCurrentIndex(next_index);
myTreeView->selectionModel()->select(next_index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
QString path = dirmodel->fileInfo(next_index).absoluteFilePath();
qDebug() << "set Dir" << path;
}
}

Figured it out. The problem was that I was trying to make a selection on the QTreeView before the specified directory had finished being loaded. In other words, tryng to make the selection within the method where I was changing directories.
The solution is to listen for the directoryLoaded(QString) SIGNAL that QFileSystemModel emits.
void QFileSystemModel::directoryLoaded ( const QString & path ) [signal]
This signal is emitted when the gatherer thread has finished to load the path.
This function was introduced in Qt 4.7.
connect(dirmodel,
SIGNAL(directoryLoaded(QString)),
this,
SLOT(model_directoryLoaded(QString)));

Related

Application crashing when searching for QLineEdit in QFormLayout

I've experienced a weird crash when trying to find a QLineEdit in a QFormLayout.
Firstly, I created a QFormLayout and set a name for it:
QFormLayout *charColLayout = new QFormLayout; charColLayout->setObjectName("charColLayout");
Then, I created a QLineEdit, modified it a bit and add it in to the layout, and I also give it a name:
QLineEdit *delim = new QLineEdit;
delim->setMaxLength(1);
delim->setMaximumWidth(100);
delim->setText("/");
delim->setObjectName("delEdit");
charColLayout->addRow("Delimiter", delim);
Afterward, in a completely different function, I re-searched that layout with findChild():
QFormLayout *charcoal = secondHoriField->findChild<QFormLayout *>("charColLayout", Qt::FindChildrenRecursively);
It should be noted that secondHoriField is just a normal QLayout which my QFormLayout is located in.
Finally, I attempted to find that QLineEdit:
QLineEdit *delimEdit = charcoal->findChild<QLineEdit*>("delEdit", Qt::FindChildrenRecursively);
if (delimEdit == nullptr) {cerr << "error\n";} //debug
string curDelim = qStrToStr(delimEdit->text());
And it surprisingly came down with a crash, and as the output shown, it's because the delimEdit is null.
18:06:10: Starting D:\...\build-cryptog-Desktop_Qt_5_15_2_MinGW_64_bit-Debug\debug\cryptog.exe ...
error
18:06:17: The program has unexpectedly finished.
18:06:17: The process was ended forcefully.
18:06:17: D:\...\build-cryptog-Desktop_Qt_5_15_2_MinGW_64_bit-Debug\debug\cryptog.exe crashed.
But when I switched the findChild() function for this rather bruteforce-y line:
QLineEdit *delimEdit = dynamic_cast<QLineEdit*>(charcoal->itemAt(1)->widget());
cerr << qStrToStr(delimEdit->objectName()) << endl; //debug line
The program ran fine, and it showed the same name I set for the QLineEdit:
18:12:02: Starting D:\...\build-cryptog-Desktop_Qt_5_15_2_MinGW_64_bit-Debug\debug\cryptog.exe ...
delEdit
18:12:11: D:\...\build-cryptog-Desktop_Qt_5_15_2_MinGW_64_bit-Debug\debug\cryptog.exe exited with code 0
Why did this happened?
Another note: qStrToStr() is a function I implement to convert QString to std::string, and I have hand-checked it.
While findChild is a QObject method the itemAt is a QFormLayout method.
QObject::findChild
QFormLayout::itemAt
With addRow you add an item to the QFormLayout. This does not make it a child in the context of the QObject.
QFormLayout::addRow
The purpose of the QFormLayout is to organize the positioning of QWidgets, it is not meant to serve as a container. Maybe you could check whether the top level QWidget (e.g. QMainWindow) holding the QFormLayout would be a better choice as a parent for the QLineEdit.
Assuming you have some kind of QMainWindow:
QMainWindow myMainWindow;
// ...
QLineEdit *delim = new QLineEdit(&myMainWindow);
delim->setObjectName("delEdit");
//...
In another location:
auto delimEdit = myMainWindow.findChild<QLineEdit*>("delEdit");

How to update a TableView with progress data for multiple ProgressBars?

I've started to extend the qGet DownloadManager to emit the progress of a TransferItem, so that i can connect to it. I'm inserting the progress data into a cell of a TableView model for display with an Delegate, finally the delegate paints the progress bar. That works in theory, but i'm running into the following
Problem: when there are multiple downloads in parallel, then i get progress updates from both signals into both cells!
Both progress bars show progress data, but the signal is kind of mixed and not unique to the current index (QModelIndex index / index.row()).
(Please ignore the small transitioning problem between UserRoles (after clicking the download button "ActionCell" is displayed and then "Install", before the "ProgressBar" shows up.). That is not the main problem here. My question is about the index problem.) The text "112" and "113" is the int index.row.
Questions:
How to update a TableView with progress data for multiple ProgressBars?
What must i change to render a progress bar for each download?
Source
Emit progress of a download
I've added the following things to re-emit the signal through the classes, until it bubbles up to the top, where it becomes connectable from the GUI.
a connection from QNetworkReply - downloadProgress(qint64,qint64) to TransferItem - updateDownloadProgress(qint64,qint64)
void TransferItem::startRequest()
{
reply = nam.get(request);
connect(reply, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(reply, SIGNAL(downloadProgress(qint64,qint64)),
this, SLOT(updateDownloadProgress(qint64,qint64)));
connect(reply, SIGNAL(finished()), this, SLOT(finished()));
timer.start();
}
the SLOT function TransferItem - updateDownloadProgress(qint64,qint64) as receiver calculates the progress and stores it in progress (QMap<QString, QVariant>).
After the calculation the downloadProgress(this) signal is emitted.
// SLOT
void TransferItem::updateDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
progress["bytesReceived"] = QString::number(bytesReceived);
progress["bytesTotal"] = QString::number(bytesTotal);
progress["size"] = getSizeHumanReadable(outputFile->size());
progress["speed"] = QString::number((double)outputFile->size()/timer.elapsed(),'f',0).append(" KB/s");
progress["time"] = QString::number((double)timer.elapsed()/1000,'f',2).append("s");
progress["percentage"] = (bytesTotal > 0) ? QString::number(bytesReceived*100/bytesTotal).append("%") : "0 %";
emit downloadProgress(this);
}
QString TransferItem::getSizeHumanReadable(qint64 bytes)
{
float num = bytes; QStringList list;
list << "KB" << "MB" << "GB" << "TB";
QStringListIterator i(list); QString unit("bytes");
while(num >= 1024.0 && i.hasNext()) {
unit = i.next(); num /= 1024.0;
}
return QString::fromLatin1("%1 %2").arg(num, 3, 'f', 1).arg(unit);
}
When a new download is enqueued, i'm connecting the emitted downloadProgress(this) to the Slot DownloadManager - downloadProgress(TransferItem*). (dl is DownloadItem which extends TransferItem).
void DownloadManager::get(const QNetworkRequest &request)
{
DownloadItem *dl = new DownloadItem(request, nam);
transfers.append(dl);
FilesToDownloadCounter = transfers.count();
connect(dl, SIGNAL(downloadProgress(TransferItem*)),
SLOT(downloadProgress(TransferItem*)));
connect(dl, SIGNAL(downloadFinished(TransferItem*)),
SLOT(downloadFinished(TransferItem*)));
}
Finally, i'm re-emitting the download progress one more time:
void DownloadManager::downloadProgress(TransferItem *item)
{
emit signalProgress(item->progress);
}
Now the TableView with Delegate, doDownload(index) and ProgressBarUpdater
QTableView
with added QSortFilterProxyModel (for case-insensitivity)
with added ColumnDelegate, which renders DownloadButton and ProgressBar based on custom UserRoles. The delegate handles the button click: the SIGNAL downloadButtonClicked(index) is emited from the editorEvent(event, model, option, index) method.
actionDelegate = new Updater::ActionColumnItemDelegate;
ui->tableView->setItemDelegateForColumn(Columns::Action, actionDelegate);
connect(actionDelegate, SIGNAL(downloadButtonClicked(QModelIndex)), this, SLOT(doDownload(QModelIndex)));
The doDownload method receives the index and fetches the download URL from the model. Then the URL is added to the DownloadManager
and i'm setting up a ProgressBarUpdater object to set the progress data to the model at the given index. Finally i'm, connecting downloadManager::signalProgress to progressBar::updateProgress and invoke the downloadManager::checkForAllDone to start the download processing.
void UpdaterDialog::doDownload(const QModelIndex &index)
{
QUrl downloadURL = getDownloadUrl(index);
if (!validateURL(downloadURL)) return;
QNetworkRequest request(downloadURL);
downloadManager.get(request); // QueueMode is Parallel by default
ProgressBarUpdater *progressBar = new ProgressBarUpdater(this, index.row());
progressBar->setObjectName("ProgressBar_in_Row_" + QString::number(index.row()) );
connect(&downloadManager, SIGNAL(signalProgress(QMap<QString, QVariant>)),
progressBar, SLOT(updateProgress(QMap<QString, QVariant>)));
QMetaObject::invokeMethod(&downloadManager, "checkForAllDone", Qt::QueuedConnection);
}
The model update part: the ProgressBarUpdater takes the index and the progress and should update the model at the given index.
ProgressBarUpdater::ProgressBarUpdater(UpdaterDialog *parent, int currentIndexRow) :
QObject(parent), currentIndexRow(currentIndexRow)
{
model = parent->ui->tableView_1->model();
}
void ProgressBarUpdater::updateProgress(QMap<QString, QVariant> progress)
{
QModelIndex actionIndex = model->index(currentIndexRow, UpdaterDialog::Columns::Action);
// set progress to model
model->setData(actionIndex, progress, ActionColumnItemDelegate::DownloadProgressBarRole);
model->dataChanged(actionIndex, actionIndex);
}
The rendering part: i'm rendering the fake ProgressBar from the delegate; fetching the progress data with index.model()->data(index, DownloadProgressBarRole).
void ActionColumnItemDelegate::drawDownloadProgressBar(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionProgressBarV2 opt;
opt.initFrom(bar);
opt.rect = option.rect;
opt.rect.adjust(3,3,-3,-3);
opt.textVisible = true;
opt.textAlignment = Qt::AlignCenter;
opt.state = QStyle::State_Enabled | QStyle::State_Active;
// get progress from model
QMap<QString, QVariant> progress =
index.model()->data(index, DownloadProgressBarRole).toMap();
QString text = QString::fromLatin1(" %1 %2 %3 %4 %5 ")
.arg(QString::number(index.row()))
.arg(progress["percentage"].toString())
.arg(progress["size"].toString())
.arg(progress["speed"].toString())
.arg(progress["time"].toString());
opt.minimum = 0;
opt.maximum = progress["bytesTotal"].toFloat();
opt.progress = progress["bytesReceived"].toFloat();
opt.text = text;
bar->style()->drawControl(QStyle::CE_ProgressBar,&opt,painter,bar);
}
I've added QString::number(index.row() to the progress bar text, so that each ProgressBar gets its row number rendered. In other words: the rendering is unique to the row, but the incoming progress data is somehow mixed.
I'm stuck on the index problem for a while now. Thank you in advance for your help.
Update: The issue is resolved!
Thank you very much ddriver!! I followed your suggestions and fixed it:
The DownloadManager tracks the progress for all transfers, and you keep each transfer item's data in the respective TransferItem.
The logical thing IMO would be to have a connection from each TransferItem to the corresponding ProgressBarUpdater, and emit from the transfer item.
However, in your case, you are reporting progress not from each individual transfer item, but from the download manager. So each time you are emitting a progress, you are emitting the progress for a particular transfer item to all progress bars.
connect(&downloadManager, SIGNAL(signalProgress(QMap<QString, QVariant>)),
progressBar, SLOT(updateProgress(QMap<QString, QVariant>)));
So instead of a
TransferItem --progress--> CorrespondingUI
you have a:
TransferItem --transferItem--> DownloadManager --progress--> AllUIs
This leads to having one single and varying progress for all progress bars, which corresponds to the last download that happen to report progress before the UI updated. Which is why you get no more variation after the first download is completed, as the manager only updates the progress for the second.
Finally, i'm re-emitting the download progress one more time:
void DownloadManager::downloadProgress(TransferItem *item)
{
emit signalProgress(item->progress);
}
And who exactly needs an anonymous progress, containing no information whatsoever to which transfer it applies? Aside from the bug of course.
Would you be so nice to explain, how to simplify it?
I was at the end of my mental rope yesterday when I commented, on a clear head it doesn't look all that overdone, but still I'd probably go for something more streamlined, involving 3 key components only:
DownloadsManager -> DownloadController -> UI
-> DownloadController -> UI
It just seems redundant to have a DownloadItem and then also a TransferItem, considering that a download is a transfer.
The model and view are totally unnecessary as well, as is storing the progress in the model rather than just having it as a member of the progress bar. You could go with just a regular widget for each download, and place them in a vertical layout.
Update:
The excessive, unnecessary compartmentalization has led to a level of fragmentation which makes it hard to get to the data, needed to make it work once you put everything together. The main issue is you have no way to tie a transfer item to the right progress bar updater, and since you still haven't posted all of the relevant code, the simplest possible solution I can offer involves the following minor changes:
// in DownloadManager
void signalProgress(QMap<QString, QVariant>); // this signal is unnecessary, remove
void DownloadManager::downloadProgress(TransferItem *item) // change this
{
registry[item->request.url()]->updateProgress(item->progress);
}
QMap<QUrl, ProgressBarUpdater *> registry; // add this
// in UpdaterDialog
void UpdaterDialog::doDownload(const QModelIndex &index)
{
QUrl downloadURL = getDownloadUrl(index);
if (!validateURL(downloadURL)) return;
QNetworkRequest request(downloadURL);
downloadManager.get(request); // QueueMode is Parallel by default
ProgressBarUpdater *progressBar = new ProgressBarUpdater(this, index.row());
progressBar->setObjectName("ProgressBar_in_Row_" + QString::number(index.row()) );
// remove the connection - source of the bug, instead register the updater
downloadManager.registry[downloadURL] = progressBar;
QMetaObject::invokeMethod(&downloadManager, "checkForAllDone", Qt::QueuedConnection);
}
That's pretty much it, the progress updater is associated with the URL, and in DownloadManager::downloadProgress instead of emitting the progress to all progress updaters, you simply lookup the one which actually corresponds to the particular download, and only update its progress. It is somewhat clumsy, but as I said, if your design is proper, it would not be needed and you wouldn't have the problem in the first place.
There are other solutions as well:
change the DownloadManager's signal to void signalProgress(TransferItem *), and the body of downloadProgress toemit signalProgress(item); , change to void ProgressBarUpdater::updateProgress(TransferItem *), and in the body compare the transfer item's request's url to the one in the model at the currentIndexRow, and only model-setData() if it is the same. This solution is not very efficient, since it will emit to all progress updaters just to modify one.
cut out the middleman, what I've been suggesting from the start, make DownloadManager ::get() return a pointer to the DownloadItem/TransferItem created in its body, then in UpdaterDialog::doDownload() you can connect the transfer item directly to the appropriate progress updater, so you will no longer need DownloadManager::downloadProgress() and the signalProgress signal, you only need to change the signature of the signal in TransferItem to void downloadProgress(QMap<QString, QVariant>); and emit the progress rather than the item. This is actually the most efficient solution, as it involves nothing extra, jsut the removal of unnecessary stuff.

QSortFilterProxyModel QTableView 5.3 not sorting/updating?

Qt 5.3.0 & 5.3.1 Linux
I've subclassed QSortFilterProxyModel and implemented lessThan().
When someone clicks the tableview header (which Ive 'connect'ed to)
and I call invalidate(), I see it call 'lessThan' but the tableview never
updates. Can someone tell me what Im missing? lessThan() is definitely
sorting things properly as I put some print statements to see what was
happening inside there when it was called. I've also tried adding
table->repaint() which doesn't do anything.
Here's my code:
QTableView *table = m_ui->tableView;
table->resize(930, 200);
table->setAlternatingRowColors(true);
table->setSelectionMode(QAbstractItemView::SingleSelection);
table->setSelectionBehavior(QAbstractItemView::SelectRows);
table->verticalHeader()->hide();
QStringList header;
header << "ID";
header << "Prefix";
header << "First";
header << "M";
header << "Last";
int cols = header.size();
BookcaseModel *bookcaseModel = new BookcaseModel(this, cols, header);
m_proxy_bookcase = new SortFilterProxyModelBookcase(this);
m_proxy_bookcase->setSourceModel(bookcaseModel);
m_proxy_bookcase->sort(0, Qt::AscendingOrder);
m_proxy_bookcase->setDynamicSortFilter(true);
m_proxy_bookcase->setSortRole(Qt::DisplayRole);
table->setModel(bookcaseModel);
table->setSortingEnabled(true);
table->horizontalHeader()->setSortIndicator(0, Qt::AscendingOrder);
table->horizontalHeader()->setSectionsClickable(true);
connect(table->horizontalHeader(), SIGNAL(sectionClicked(int)),
this, SLOT(selectedColumnSlot(int)));
Then the slot:
void selectedColumnSlot(int col)
{
m_proxy_bookcase->sort(col, Qt::DescendingOrder);
m_proxy_bookcase->invalidate();
}
Considering this line:
table->setModel(bookcaseModel);
It seems your table is showing the underlying model, not the proxy. It should be:
table->setModel(m_proxy_bookcase);
When you sort a model using a proxy, it doesn't modify the source model; only the proxy knows the order of elements after the call to sort(). That's why your view must show the proxy rather than the source model.
And (I might be wrong) I don't think the call to invalidate() after sort() is useful.

QTableView: how to edit non-editable cells in the program?

How should this be done by using the model->setData() method call?
I have derived a class called "MyStandardItemModel" from QStandardItemModel. I have made my third and fourth columns non-editable by overriding the protected virtual flags method. This is how it goes:
#define TX_PACKET_COLUMN (4u)
#define RX_PACKET_COLUMN (5u)
Qt::ItemFlags MyStandardItemModel::flags(const QModelIndex& index) const
{
if (index.column() == TX_PACKET_COLUMN || index.column() == RX_PACKET_COLUMN)
{
return (QStandardItemModel::flags(index) & ~Qt::ItemIsEditable);
}
else
{
return QStandardItemModel::flags(index);
}
}
...
//Set model
ui->testCaseTableView->setModel(model);
Having this done, I am not able to edit the cells in the third and fourth column.
Now, I want that when I double click on these cells, a pop-up dialog comes up. I will modify some data in the editable field of that dialog, and then copy it back to the non editable cells inside the code.
I tried to just write a doubleclick() handler for the QTreeView and just copy some data to the cells just to see if it is possible to copy data to the non-editable cells.
This operation is failing, and the data is not written into the non-editable cells.
Here you can find the double click handler:
void MainWindow::on_testCaseTableView_doubleClicked(const QModelIndex &index)
{
QVariant variant;
variant.toString() = "AA";
if((index.column() == TX_PACKET_COLUMN)||(index.column() == RX_PACKET_COLUMN))
{
model->setData(index, variant); // set new value
}
}
The setData(..) operation is clearing the already written data in the cells, but string "AA" is not getting written. Please suggest how to copy some data to non-editable cells inside the code.
QVariant set is empty. Nothing needs to be wrong in your model. Error is on this line:
variant.toString() = "AA";
change to:
QVariant variant("AA"); // just for testing anyway
As I indicated in my comment, you have to fix this first issue:
instead of:
QVariant variant;
variant.toString() = "AA";
you should write
QVariant variant = QLatin1String("AA");
In general, you would look into the setData(...) implementation for such cases whether you emit the data changed signal properly and so forth, but here you are entering a prior issue which can lead to problems, so let us fix that.
Note, you should use QLatin1String to avoid the unnecessary explicit conversion from raw char* to QString. This is a good practice in general, and this is available with Qt 4 as well as Qt 5.
Although, you could also use the QStringLiteral macro for creating a QString very efficiently with template magic out of your raw literal, but that requires C++11.

Display Audio & Video Files In TreeView on Clicking Any Drive (QFileSystemModel)

I want to traverse the drives present in my system and search for audio/video files inside it. Basically traverse the sub directories and display the file inside the treeview. I have 2 tree views, One to display System Directories and other to display audio/video files.
// Displays System Drives inside TreeView(Drive View) When Application Starts
void PanasonicViewer::onCamStartup()
{
m_SystemModel = new QFileSystemModel(this);
m_SystemListViewModel = new QFileSystemModel(this);
m_SystemModel->setRootPath(QDir::currentPath());
ui->DriveView->setModel(m_SystemModel); //Left side TreeView
ui->DriveListView->setModel(m_SystemListViewModel); //Right Side TreeView
// regard less how many columns you can do this using for:
for(int nCount = 1; nCount < m_SystemModel->columnCount(); nCount++)
ui->DriveView->hideColumn(nCount);
}
//On Clicking The TreeView, it should display Audio and Video files in DriveListView
void PanasonicViewer::on_DriveView_clicked(const QModelIndex &index)
{
QStringList sDriveFilters;
QString sPath = m_SystemModel->fileInfo(index).absoluteFilePath();
ui->DriveListView->setRootIndex(m_SystemListViewModel->setRootPath(sPath));
m_SystemModel->setRootPath(QDir::currentPath());
m_SystemModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs );
m_SystemListViewModel->setFilter( QDir::Files | QDir::NoDotAndDotDot );
sDriveFilters << "*.aac" << "*.wmv" << "*.avi" << "*.mpeg" << "*.mov" << "*.3gp" << "*.flv" << "*.mp3" ;
m_SystemListViewModel->setNameFilters(sDriveFilters);
m_SystemListViewModel->setNameFilterDisables(false);
}
You can notice in the above click event that I have set Filter to selected extensions. This seems to work and displays audio and video files when I click a drive i.e. E:\ but doesnt display the files present inside subfolders. Where am i going wrong?
The problem is due to what you're setting for QFileSystemModel::filter.
The default setting for is QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs, where QDir::AllEntries translates to QDir::Dirs | QDir::Files | QDir::Drives. If we expand it out, the default setting is:
QDir::Dirs | QDir::Files | QDir::Drives | QDir::NoDotAndDotDot | QDir::AllDirs
By calling m_SystemModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs ), you're telling the model that you only want to see standard directories, and want to exclude all files. Just remove your call to setFilter altogether and you should be fine.
Try changing
ui->DriveListView->setRootIndex(m_SystemListViewModel->setRootPath(sPath));
to
m_SystemListViewModel->setRootPath(sPath)
ui->DriveListView->setRootIndex(m_SystemListViewModel->index(sPath);
I'm skeptical whether the QModelIndex returned by setRootPath is the one you think it is. Troubleshoot this by removing the filter from m_SystemListViewModel to make sure that you're actually viewing the folder that you think you are.
I know that this should work if sPath is a directory path. I'm not sure if it'll work as expected if it's a file path.
What you expect is not what the standard QFileSystemModel is supposed to do. What you need is to manually extract the multimedia files recursively in QStringListModel and set it on your QListView, or directly use QListWidget to add items directly while enumerating. When the selection over the QFileSystemModel changes you can get the path of the selected folder and enumerate the files and the subfolder files recursively. Note that if you click accedentally some drive you will end-up waiting for all the multimedia files in your drive. If that is what you want then maybe you may consider enumerating in worker thread which is not so easy.

Resources