2

I would like to know if there is a way to use the Cocoa API in a Qt application.

I've already used the Windows API to get the selected text from the active application. I'd like to do the same with mac os. I tried to make a simple "hello world" application C++ with xCode, including the <Cocoa/Cocoa.h> but it didn't work as I excepted. Is there a way to get this "hello word" application to build with Cocoa?

And, also If that is possible, can I get the selected text from the active windows with Cocoa API?

EDIT :

All right, so I successfully build something using Cocoa.h, thanks to this thread : How to mix Qt, C++ and Obj-C/Cocoa.

For the selection problem you could check out the answers I posted which tell you how to do it.

Community
  • 1
  • 1
Aleks
  • 410
  • 4
  • 22
  • I'm using Qt with Cocoa without any problem. Just create Qt application and add obj-c files to your project – Kamil Klimek Mar 18 '12 at 12:07
  • I'm not sure I understand what you mean. I tried #include and mac: LIBS += -framework Cocoa in the .pro file. But I get 9689 errors to be exact... And I'm puzzled, when I run an empthy Qt app, it open an x11 frame, is that normal ? I thought Mac OS didn't use x11 anymore. – Aleks Mar 18 '12 at 12:22
  • You must use Objective-C++ if you want to use C++ with Cocoa. Your implementation files must use the `.mm` file extension. I know very little about Qt so I can't help with that. – Rob Keniger Mar 18 '12 at 12:56
  • Qt doesn't use X11 under OS X, unless one configures it at compile time to do so. Which I've only seen in the combination of macports and KDE 3, years ago. – Frank Osterfeld Mar 18 '12 at 13:01
  • possible duplicate of [How to mix Qt, C++ and Obj-C/Cocoa](http://stackoverflow.com/questions/2355056/how-to-mix-qt-c-and-obj-c-cocoa) – mmmmmm Mar 18 '12 at 16:19
  • You must keep your objc code in separate .mm file – Kamil Klimek Mar 18 '12 at 16:36
  • 1
    Take a look how does Qt do it http://qt.gitorious.org/qt/qt/trees/4.8/src/gui/util – Kamil Klimek Mar 18 '12 at 16:40

1 Answers1

2

For those who could be interested : I found a way to get the current selected text.

Just by simulating cmd + c :

So thanks to this thread, I changed the code to obtain the "c" key which is represented by the integer 8 (Found in NSEvent.h), so here's the code :

CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef saveCommandDown = CGEventCreateKeyboardEvent(source, (CGKeyCode)8, YES);
CGEventSetFlags(saveCommandDown, kCGEventFlagMaskCommand);
CGEventRef saveCommandUp = CGEventCreateKeyboardEvent(source, (CGKeyCode)8, NO);

CGEventPost(kCGAnnotatedSessionEventTap, saveCommandDown);
CGEventPost(kCGAnnotatedSessionEventTap, saveCommandUp);

CFRelease(saveCommandUp);
CFRelease(saveCommandDown);
CFRelease(source);

Now you just have to access the clipboard from Qt to get the selection. (If ask, I can put the code to do so)

Anyway, thanks to the stackoverflow community ;)

Community
  • 1
  • 1
Aleks
  • 410
  • 4
  • 22