https://bugs.kde.org/show_bug.cgi?id=404995

            Bug ID: 404995
           Summary: QApplication::quitOnLastWindowClosed() doesn't work if
                    dialog opened before QApplication::exec() started
           Product: plasma-integration
           Version: unspecified
          Platform: Other
                OS: Linux
            Status: REPORTED
          Severity: normal
          Priority: NOR
         Component: general
          Assignee: plasma-b...@kde.org
          Reporter: martin.sandsm...@kde.org
  Target Milestone: ---

Some applications call e. g. QFileDialog::getOpenFileName() before starting
QApplication.

As an example (which was my test case), here I call getOpenFileName() in the
constructor of my custom widget for the file:
https://github.com/sandsmark/epubreader/blob/master/widget.cpp#L26

... and then after creating the widget I actually start the event loop:
https://github.com/sandsmark/epubreader/blob/master/main.cpp#L8-L11


This works fine either without a QPA or with a QPA without custom dialogs
(tested with qt5ct), but for some reason I couldn't track down
KDEPlatformFileDialog breaks quitOnLastWindowClosed; i. e. the application
opens a file dialog, starts the QApplication, and then shows its main widget,
but QApplication doesn't exit when the main widget is closed.


Debugging done so far:

I looked briefly in the code that handles quitOnLastWindowClosed, and copied
the logic into my test case:
https://github.com/sandsmark/epubreader/blob/master/widget.cpp#L92-L111

But that prints «last closed? true» both when it works (e.g. with qt5ct) and
when it doesn't.

I also tried various combinations of setting Qt::WA_DeleteOnClose on the
KDEPlatformFileDialog, and my own application, and replacing hide() for
close(), etc., but nothing worked. I also verified that the
KDEPlatformFileDialog and the KDEPlatformFileDialogHelper got deleted.

The only thing I've thought about that I haven't tested is changing
KDEPlatformFileDialog to not inherit from QDialog, but making it just a plain
QWidget.


Partial solution:

After I tracked down that it only happened iff the application tried to open a
dialog before starting the QApplication, I added a hack (to my own private
"fork" of plasma-integration with ugly hacks that I can't put in
plasma-integration proper), that just checks if the QApplication is running,
otherwise it just lets Qt use its own QFileDialog:

https://github.com/sandsmark/sandsmark-integration/commit/476c4e16c21128d6f390b9c80c8dad9290c222f7


In conclusion calling getOpenFile() etc. before launching the QApplication is
probably a bit of an anti-pattern, but it's not that uncommon, so it should
probably be fixed properly. It might be a bug in Qt, but emitLastWindowClosed()
checks if in_exec is true (in_exec is set when the QCoreApplication event loop
is started) every time it is called, so the logic there looks sound.

-- 
You are receiving this mail because:
You are watching all bug changes.

Reply via email to