The code below works most of the time, maybe a more experienced Objective C++ programmer can help me:
Issue 1: When called from specific menu QActions, the system starts to animate the dialog into existence and then automatically closes it before it is drawn full size; the return code is -1000. For other specific actions it always works correctly.
Issue 2: I try to disable the Tags field but it still shows on Mavericks. If I remove the if statement the tags field is hidden on Mavericks but the program crashes on Lion.
// Use native dialog to save a file
QString GetCocoaSaveFile(const QString &prompt, const QString &source_dir, const QString &filename)
{
QString selected_file;
NSSavePanel *panel = [NSSavePanel savePanel];
[panel setFloatingPanel:YES];
if([panel respondsToSelector:@selector(setShowsTagField)]) // Only available on 10.9+
[panel setShowsTagField:NO];
NSString *title_ns = [[NSString alloc]initWithUTF8String:prompt.toUtf8().data()];
[panel setTitle:title_ns];
NSString *path_ns = [[NSString alloc]initWithUTF8String:source_dir.toUtf8().data()];
NSURL *directory_url = [[NSURL alloc]initFileURLWithPath:path_ns];
[panel setDirectoryURL:directory_url];
NSString *default_ns = nil;
if(!filename.isEmpty())
{
default_ns = [[NSString alloc]initWithUTF8String:filename.toUtf8().data()];
[panel setNameFieldStringValue:default_ns];
}
NSInteger result = [panel runModal];
if(result == NSFileHandlingPanelOKButton)
{
NSURL *result_url = [panel URL];
NSString *path_sel = [result_url path];
selected_file = QString::fromUtf8([path_sel UTF8String]);
}
if(default_ns) [default_ns release];
[directory_url release];
[path_ns release];
[title_ns release];
return(selected_file);
}
I'm using Qt 5.2.0 if that is relevant.
I also observed the issue #1 (Qt 4.8.0). It can be fixed by using QApplication::processEvents () before showing the dialog.
QApplication::processEvents ();
NSInteger result = [panel runModal];