Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom items to QFileDialog?

Tags:

c++

windows

qt

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.

enter image description here

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.

like image 878
Alex P. Avatar asked Jun 15 '17 12:06

Alex P.


People also ask

How to create a qfiledialog?

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.

What is the use of getopenfilename () function in qfiledialog?

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.

What is the use of file selector dialog widget?

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.

How do I apply a filter to a file dialog?

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.


1 Answers

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:

enter image description here

If, however, the drives you've added don't exist / aren't accessible, then they will be shown greyed out.

like image 51
Steve Lorimer Avatar answered Oct 21 '22 06:10

Steve Lorimer