QFileDialog doesn't traverse mounted directories correctly



  • On Linux Ubuntu 16.04, QFileDialog does not behave as expected.

    In the Ubuntu File Navigator, I can traverse the mounted drives correctly by double clicking. I can also use the command line to traverse them correctly.

    In my Qt 5.7-based application, when I navigate to the mounted directory, double clicking actually causes the QFileDialog to navigate two directories up. If I click once on the directory and click the "Open" button in QFileDialog, it navigates correctly. Once it is inside the mounted directory, double clicks work correctly to navigate further down.

    Here's the relevant code. Would appreciate any help. Really annoying and confusing to end users.

    MainWindow::MainWindow
    {
    ...
        m_packageFileDialog.setNameFilter("gzipped files (*.gz)");
        m_packageFileDialog.setViewMode(QFileDialog::Detail);
        m_packageFileDialog.setFileMode(QFileDialog::ExistingFile);
    ...
    
        QObject::connect(&m_packageFileDialog, &QFileDialog::directoryEntered, this, &MainWindow::onPackageFileDialogDirectoryEntered);
    
    ...
    }
    
    
    void MainWindow::onPackageFileDialogDirectoryEntered(QString dir)
    {
        QDir directory(dir);
        QStringList nameFilters;
        nameFilters << "*.gz";
        QFileInfoList list = directory.entryInfoList(nameFilters, QDir::AllDirs | QDir::Files, QDir::Time | QDir::DirsLast);
        m_packageFileDialog.selectFile(list.at(0).absoluteFilePath());
    }
    
    
    void MainWindow::on_btnBrowse_clicked()
    {
        QStringList paths;
        if (!validateCdnBasePath())
            return; // error dialog popus up if not valid
        m_packageFileDialog.setDirectory(m_cdnBasePath);
        if (m_packageFileDialog.exec())
            paths = m_packageFileDialog.selectedFiles();
        else
            return;
        if (paths.isEmpty())
            return;
        QString path = paths.at(0);
    ...
    }
    `
    


  • Also, when the QFileDialog navigates two directories up, clicking the back arrow on the dialog, puts me in the directory I expected when I double clicked.


  • Lifetime Qt Champion

    Hi,

    Do you have the same behavior if you compile your application with your distribution provided Qt ?



  • @SGaist Thanks for the tip. I had high hopes for it, but after installing the distro qt 5 (5.1) with apt and setting the kit to use it in creator, the bug still exists. I'm pretty sure it worked, because I had to add a CONFIG += c++11 to the .pro file for it to compile with the 5.1 kit. Qt 5.7 kit on this platform didn't need that.


  • Lifetime Qt Champion

    Because C++11 is a requirement since 5.7.



  • I found a "workaround" for the bug. Here is a small working example that demonstrates it. There appears to be a race condition in the selectFile() function of QFileDialog. Any chance somebody from the Qt team could investigate it?

    OS: ubuntu 16.04 LTS
    Processor: Intel Core i7
    OS type: 64-bit

    fdhandler.cpp

    #include "fdhandler.h"
    
    #include <QDebug>
    #include <QDir>
    #include <QFileInfoList>
    #include <QStringList>
    
    FDHandler::FDHandler(QFileDialog *dlg, QObject *parent)
        :   QObject(parent)
        ,   m_fileDialog(dlg)
    {
    }
    
    void FDHandler::onDirectoryEntered(QString dir)
    {
        QDir directory(dir);
        QFileInfoList list = directory.entryInfoList(QStringList() << "*.gz", QDir::AllDirs | QDir::Files, QDir::Time | QDir::DirsLast);
        //uncomment the following line to work around the bug
        //qDebug() << list.at(0).absoluteFilePath();
        m_fileDialog->selectFile(list.at(0).absoluteFilePath());
    }
    

    main.cpp

    #include <QApplication>
    #include <QDebug>
    #include <QFileDialog>
    
    #include "fdhandler.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QFileDialog fd;
        fd.setNameFilter("gzipped files (*.gz)");
        fd.setViewMode(QFileDialog::Detail);
        fd.setFileMode(QFileDialog::ExistingFile);
        fd.setDirectory("/mnt");
        FDHandler handler(&fd);
        QObject::connect(&fd, &QFileDialog::directoryEntered, &handler, &FDHandler::onDirectoryEntered);
        if (fd.exec())
            qDebug() << "file selected";
        else
            qDebug() << "file not selected";
        return a.exec();
    }
    

  • Lifetime Qt Champion

    The best for that is to go to the bug report system. Check there if there's already something about the bug you found. If not please open a new report providing your sample + workaround as well as all details about your setup. If there's already something, don't hesitate to add your findings and code.



  • @SGaist bug filed.


  • Lifetime Qt Champion

    Can you share the link ? That will make it easier to find.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.