99#include < QTimer>
1010#include < QEventLoop>
1111#include < QCoreApplication>
12+ #include < QWindow>
1213#include < Cocoa/Cocoa.h>
1314
1415namespace {
@@ -52,7 +53,7 @@ QString convertQtFilterToMacOS(const QString &qtFilter)
5253
5354QString NativeFileDialog::getFileNameNative (const QString &title,
5455 const QString &initialDir, const QString &filter,
55- bool saveDialog)
56+ bool saveDialog, void *parentWindow )
5657{
5758 // Defer dialog presentation to avoid interfering with Qt QML object destruction
5859 QString result;
@@ -61,8 +62,18 @@ QString convertQtFilterToMacOS(const QString &qtFilter)
6162 // Process events once to allow QML cleanup, then show dialog with minimal delay
6263 QCoreApplication::processEvents ();
6364
65+ // Get the native NSWindow if parentWindow is provided
66+ NSWindow *nsParentWindow = nil ;
67+ if (parentWindow) {
68+ QWindow *window = static_cast <QWindow*>(parentWindow);
69+ NSView *view = reinterpret_cast <NSView *>(window->winId ());
70+ if (view) {
71+ nsParentWindow = [view window ];
72+ }
73+ }
74+
6475 // Use a very short timer to minimize delay while still avoiding Qt conflicts
65- QTimer::singleShot (1 , [&]() {
76+ QTimer::singleShot (1 , [&, nsParentWindow ]() {
6677 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc ] init ];
6778
6879 NSString *nsTitle = title.isEmpty () ? nil : title.toNSString ();
@@ -97,7 +108,15 @@ QString convertQtFilterToMacOS(const QString &qtFilter)
97108 }
98109 }
99110
100- NSInteger buttonPressed = [panel runModal ];
111+ NSInteger buttonPressed;
112+ if (nsParentWindow) {
113+ // Run as sheet modal to parent window
114+ buttonPressed = [panel runModalForWindow: nsParentWindow];
115+ } else {
116+ // Run as application modal dialog
117+ buttonPressed = [panel runModal ];
118+ }
119+
101120 if (buttonPressed == NSModalResponseOK ) {
102121 NSURL *url = [panel URL ];
103122 modalResult = [url path ];
@@ -124,7 +143,15 @@ QString convertQtFilterToMacOS(const QString &qtFilter)
124143 }
125144 }
126145
127- NSInteger buttonPressed = [panel runModal ];
146+ NSInteger buttonPressed;
147+ if (nsParentWindow) {
148+ // Run as sheet modal to parent window
149+ buttonPressed = [panel runModalForWindow: nsParentWindow];
150+ } else {
151+ // Run as application modal dialog
152+ buttonPressed = [panel runModal ];
153+ }
154+
128155 if (buttonPressed == NSModalResponseOK ) {
129156 NSArray *urls = [panel URLs ];
130157 if ([urls count ] > 0 ) {
0 commit comments