I am using non-native QFileDialog
(for choosing directory path) and I need to add some custom drives. I don't even need to display any content inside of these drives for now, I just need to display these drives on the top level (and preferably with my icon) and output some special string in the result when user selects it.
What is the easiest way to implement this?
I have read in the documentation that proxy model can be used for that, but I don't understand how to implement such model, all examples show only filtering and sorting of the already available items.
The QFileDialog class enables a user to traverse the file system in order to select one or many files or a directory. The easiest way to create a QFileDialog is to use the static functions.
The function is used similarly to QFileDialog::getOpenFileName (). In particular parent, caption, dir, filter, selectedFilter and options are used in the exact same way. The main difference with QFileDialog::getOpenFileName () comes from the ability offered to the user to select a remote file. That's why the return type and the type of dir is QUrl.
This widget is a file selector dialog. It enables the user to navigate through the file system and select a file to open or save. The dialog is invoked either through static functions or by calling exec_ () function on the dialog object.
You can apply several filters by using setNameFilters (). Use selectNameFilter () to select one of the filters you've given as the file dialog's default filter. The file dialog has two view modes: List and Detail. List presents the contents of the current directory as a list of file and directory names.
If I understand you correctly, you want to add additional drives to the side bar on the left side of the file dialog?
The function you are looking for is QFileDialog::setSidebarUrls
#include <QApplication>
#include <QMainWindow>
#include <QHBoxLayout>
#include <QPushButton>
#include <QFileDialog>
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QMainWindow window;
QWidget widget;
QHBoxLayout layout(&widget);
QPushButton open("open");
layout.addWidget(&open);
QObject::connect(&open, &QPushButton::clicked, [&]()
{
QFileDialog dialog;
dialog.setOption(QFileDialog::DontUseNativeDialog);
QList<QUrl> drives;
drives << QUrl::fromLocalFile(QDir("D:").absolutePath());
drives << QUrl::fromLocalFile(QDir("E:").absolutePath());
drives << QUrl::fromLocalFile(QDir("foobar").absolutePath());
dialog.setSidebarUrls(drives);
dialog.exec();
});
window.setCentralWidget(&widget);
window.show();
return app.exec();
}
The result of this is shown below:
If, however, the drives you've added don't exist / aren't accessible, then they will be shown greyed out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With