Search code examples
c++cocoaqtobjective-c++

Handle Cocoa setAction message in Objective-C++


How do you handle the setAction message in Objective-C++? (Not Objective-C.)

For example, suppose I have:

my_class.mm

NSSegmentedControl *segmented = [[NSSegmentedControl alloc] init];
[segmented setSegmentCount:5];
// etc.
[segmented setAction:???];

Application: I am programming in Qt (C++) and need a wrapper around some Cocoa widgets I want to use directly. I am inheriting from QMacCocoaViewContainer but can't figure out how to handle the "clicks" of the NSSegmentedControl I am wrapping. Eventually this will emit a standard Qt signal.


Solution

  • action is just a selector - it is used in tandem with target. so write an objc method for target+action which calls through or does what you really want. actions' arguments are the sender, but you can omit that if you don't need it. the sender will be whatever is sending the message (e.g. the control). it's no different in ObjC++ - this has to be wrapped in an objc method because the target must be an objc object.

    so it would look like this:

    obj.action = @selector(pressDoStuff:);
    

    and the method is:

    - (void)pressDoStuff:(id)sender