OO-SKY
OO-SKY

Reputation: 91

QT/C++, OS X: Alternative for hide() when quiting app so that app keeps running and shows up again after clicking icon in dock

I'm using QT with c++ on Mac OS X. When closing my application I use hide() to keep my app running and to hide the window.

But afterwards when I click on the icon of my app in the dock, it does not show up anymore.

I read here that using the following code instead of hide() should fix this behaviour:

ProcessSerialNumber pn;
GetFrontProcess (&pn);
ShowHideProcess(&pn,false);

But I don't know how to use that code :s Can somebody explain how to use this code, or how to solve my problem?

thanks!

Upvotes: 1

Views: 1362

Answers (2)

ehopperdietzel
ehopperdietzel

Reputation: 324

You can compile Objective-C inside your Qt app, so do the next:

Add this to your .pro file:

macx {

    LIBS += -framework Foundation
    LIBS += -framework AppKit

    OBJECTIVE_SOURCES += objectivec.mm
    HEADERS +=  objectivec.h
}

Create a file called objective.h:

#ifndef __ObjectiveC_h_
#define __ObjectiveC_h_


class ObjectiveC
{
public:
    static void HideWindow();
};

#endif

Another one called objective.mm:

#include "objectivec.h"
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <CoreData/CoreData.h>

void ObjectiveC::HideWindow()
{
    [NSApp hide:nil];
}

And then just use this wherever you like:

#ifdef Q_OS_MAC
   #include "objectivec.h"
#endif

#ifdef Q_OS_MAC
   ObjectiveC *obc = new ObjectiveC();
   obc->HideWindow();
#endif

Upvotes: 2

UmNyobe
UmNyobe

Reputation: 22890

The code you are talking about is native OSX API. I am in foreign territory but I am going to try to make magic here:

for ProcessSerialNumber

//either
#include <Carbon/Carbon.h> 
#include <Cocoa/Cocoa.h>

for GetFrontProcess (&pn);

Documentation:

The GetFrontProcess function returns the process serial number of the process running in the foreground. Return "undef" if an error was detected.

Signature:

 //carbon or cocoa
 OSErr GetFrontProcess (ProcessSerialNumber *PSN);

for ShowHideProcess(&pn,false);

Signature:

 #include <Carbon/Processes.h> //carbon only?
 OSErr ShowHideProcess(const ProcessSerialNumber *psn, Boolean visible)

Upvotes: 0

Related Questions