Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Closing one QFileDialog also closes other QFileDialog on Windows
Qt 6.11 is out! See what's new in the release blog

Closing one QFileDialog also closes other QFileDialog on Windows

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 833 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tiantianxiangshang
    wrote on last edited by
    #1

    The following code creates a pops up three QFileDialog instances. Closing any of them (cancel or accept) will also close other dialogs. "finished()" signal gets emitted for all three dialogs. This happens on Windows only. The code works as expected on Linux. Qt version: 5.12.10.

    main.cpp:
    #include <QApplication>
    #include <QFileDialog>
    #include <QFontDialog>
    #include <QDir>
    #include <QDebug>
    #include <vector>
    #include "handler.hpp"

    int main(int argc, char* argv[])
    {
    QApplication app(argc, argv);
    QApplication::setQuitOnLastWindowClosed(false);
    std::vector<QDialog*> dialogs;
    MyHandler handler;

    for(int i = 0; i < 3; i++)
    {
    	auto diag = new QFileDialog(nullptr, "Select Direcotry");
        diag->setFileMode(QFileDialog::Directory);
        diag->setOptions(QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
        diag->setWindowFlags(Qt::WindowStaysOnTopHint);
    	diag->setDirectory(QDir::root());
        dialogs.push_back(diag);
    
        QObject::connect(diag, &QFileDialog::finished, &handler, &MyHandler::finished_slot);
    
        diag->open();
        diag->activateWindow();
        diag->raise();
        
    }
    
    return app.exec();
    

    }

    handler.hpp:
    #ifndef HANDLER_HPP
    #define HANDLER_HPP

    #include <QObject>
    #include <QDebug>

    class MyHandler : public QObject
    {
    Q_OBJECT

    public slots:
    void finished_slot(int result)
    {
    qDebug() << "sender: " << (qulonglong) sender() << " finished: " << result << " parent: " << sender()->parent();
    }
    };

    #endif

    JonBJ 2 Replies Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Yes, that does seem odd.
      I could reproduce it here with Qt 5.15

      I have no good reason why all will close since the app is still running.

      1 Reply Last reply
      0
      • T tiantianxiangshang

        The following code creates a pops up three QFileDialog instances. Closing any of them (cancel or accept) will also close other dialogs. "finished()" signal gets emitted for all three dialogs. This happens on Windows only. The code works as expected on Linux. Qt version: 5.12.10.

        main.cpp:
        #include <QApplication>
        #include <QFileDialog>
        #include <QFontDialog>
        #include <QDir>
        #include <QDebug>
        #include <vector>
        #include "handler.hpp"

        int main(int argc, char* argv[])
        {
        QApplication app(argc, argv);
        QApplication::setQuitOnLastWindowClosed(false);
        std::vector<QDialog*> dialogs;
        MyHandler handler;

        for(int i = 0; i < 3; i++)
        {
        	auto diag = new QFileDialog(nullptr, "Select Direcotry");
            diag->setFileMode(QFileDialog::Directory);
            diag->setOptions(QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
            diag->setWindowFlags(Qt::WindowStaysOnTopHint);
        	diag->setDirectory(QDir::root());
            dialogs.push_back(diag);
        
            QObject::connect(diag, &QFileDialog::finished, &handler, &MyHandler::finished_slot);
        
            diag->open();
            diag->activateWindow();
            diag->raise();
            
        }
        
        return app.exec();
        

        }

        handler.hpp:
        #ifndef HANDLER_HPP
        #define HANDLER_HPP

        #include <QObject>
        #include <QDebug>

        class MyHandler : public QObject
        {
        Q_OBJECT

        public slots:
        void finished_slot(int result)
        {
        qDebug() << "sender: " << (qulonglong) sender() << " finished: " << result << " parent: " << sender()->parent();
        }
        };

        #endif

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @tiantianxiangshang , @mrjj
        I assume this behaviour would not happen if you set QFileDialog::DontUseNativeDialog? I imagine the behaviour is linked to using the native Windows directory chooser dialog, rather than the Qt Windows code itself?

        mrjjM T 2 Replies Last reply
        0
        • JonBJ JonB

          @tiantianxiangshang , @mrjj
          I assume this behaviour would not happen if you set QFileDialog::DontUseNativeDialog? I imagine the behaviour is linked to using the native Windows directory chooser dialog, rather than the Qt Windows code itself?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @JonB
          Nope, even with this option, they all close if one closes.
          I assume it's due to the use of open not exec.

          1 Reply Last reply
          0
          • T tiantianxiangshang

            The following code creates a pops up three QFileDialog instances. Closing any of them (cancel or accept) will also close other dialogs. "finished()" signal gets emitted for all three dialogs. This happens on Windows only. The code works as expected on Linux. Qt version: 5.12.10.

            main.cpp:
            #include <QApplication>
            #include <QFileDialog>
            #include <QFontDialog>
            #include <QDir>
            #include <QDebug>
            #include <vector>
            #include "handler.hpp"

            int main(int argc, char* argv[])
            {
            QApplication app(argc, argv);
            QApplication::setQuitOnLastWindowClosed(false);
            std::vector<QDialog*> dialogs;
            MyHandler handler;

            for(int i = 0; i < 3; i++)
            {
            	auto diag = new QFileDialog(nullptr, "Select Direcotry");
                diag->setFileMode(QFileDialog::Directory);
                diag->setOptions(QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
                diag->setWindowFlags(Qt::WindowStaysOnTopHint);
            	diag->setDirectory(QDir::root());
                dialogs.push_back(diag);
            
                QObject::connect(diag, &QFileDialog::finished, &handler, &MyHandler::finished_slot);
            
                diag->open();
                diag->activateWindow();
                diag->raise();
                
            }
            
            return app.exec();
            

            }

            handler.hpp:
            #ifndef HANDLER_HPP
            #define HANDLER_HPP

            #include <QObject>
            #include <QDebug>

            class MyHandler : public QObject
            {
            Q_OBJECT

            public slots:
            void finished_slot(int result)
            {
            qDebug() << "sender: " << (qulonglong) sender() << " finished: " << result << " parent: " << sender()->parent();
            }
            };

            #endif

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @mrjj said in Closing one QFileDialog also closes other QFileDialog on Windows:

            I assume it's due to the use of open not exec.

            @tiantianxiangshang said in Closing one QFileDialog also closes other QFileDialog on Windows:
            This happens on Windows only. The code works as expected on Linux.

            Strange then that would differ across platforms.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              ChrisW67
              wrote on last edited by
              #6

              @tiantianxiangshang said in Closing one QFileDialog also closes other QFileDialog on Windows:

              Does the behaviour change if you remove this unnecessary fluff?

              diag->activateWindow();
              diag->raise();
              
              1 Reply Last reply
              0
              • JonBJ JonB

                @tiantianxiangshang , @mrjj
                I assume this behaviour would not happen if you set QFileDialog::DontUseNativeDialog? I imagine the behaviour is linked to using the native Windows directory chooser dialog, rather than the Qt Windows code itself?

                T Offline
                T Offline
                tiantianxiangshang
                wrote on last edited by
                #7

                Using QFileDialog::DontUseNativeDialog solves the problem:
                diag->setOptions(QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks | QFileDialog::DontUseNativeDialog);
                Tested with Qt version 5.15.2 on Windows. Didn't test with version of Qt.

                @JonB said in Closing one QFileDialog also closes other QFileDialog on Windows:

                @tiantianxiangshang , @mrjj
                I assume this behaviour would not happen if you set QFileDialog::DontUseNativeDialog? I imagine the behaviour is linked to using the native Windows directory chooser dialog, rather than the Qt Windows code itself?

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved