Question

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.

Was it helpful?

Solution

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];

OTHER TIPS

Your looking for something like this:

NSOpenPanel *panel = [NSOpenPanel openPanel]; // get an autorelease open panel
[panel setPrompt:NSLocalizedString(@"Open", @"Open")];
[panel setShowsTagField:OFF];                 // Turns off the tags
qApp->processEvent();                         // Gets Qt to play nice with the next line
NSInteger result = [panel runModal];

Works perfectly with Qt 5.2.0

The reason why the tags field is shown under Mavericks is that the following line is wrong:

if([panel respondsToSelector:@selector(setShowsTagField)])

The setShowsTagField takes an argument so the correct selector must have a colon after it:

if([panel respondsToSelector:@selector(setShowsTagField:)])
                                                       ^

and then everything works.

This question and answer made me realise that this was the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top