Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6730197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:22:45+00:00 2026-05-26T10:22:45+00:00

Short version: I have a Qt/C++ to which I am having to add a

  • 0

Short version: I have a Qt/C++ to which I am having to add a limited amount of Cocoa/Objective-C code. I have changed the .cpp file to a .mm file and added the objective-c code/objects to said file, and it compiles and works. I now need a delegate for one of the objects I created- a NSPopUpButton (or, rather, the menu thereof) to be exact – and I’m stuck. How can I add a delegate for this object?

Details:
Files in question:

reportwindow.h, reportwindow.cpp RENAMED TO reportwindow.mm
-These are the files containing my original C++ implementation plus some objective-c code (open a NSSavePanel containing a NSPopUpButton). reportwindow.h is additionally included in a .cpp file, if that makes a difference.

menuHandler.h, menuHandler.mm
-these files contain a (currently empty) objective-c class that I was intending to use as a delegate

My first thought was that I could simply make the C++ class the delegate, but this obviously doesn’t work as straight C++ doesn’t understand delegation. I then thought I’d make a separate objective-c class as a NSMenuDelegate and add an instance of it as a member object to my C++ class. As I have been able to add other objective-c objects as members, I figured this should work. However, as soon as I included the header for my new objective-c class in the C++ class header file, I got several hundred errors about “expected unqualified-id before ‘@’ token” -from the apple header files (NSValue.h, NSObject.h, etc) So apparently that didn’t work, at least not as-is. I get the same result when including ANY cocoa header in my class header file.

I then thought I’d try a forward-declaration of the objective-c class (that is how I got the other objective-c objects working). however, this didn’t work either- if I declare it as “class myClassName” I get an error about re-defining the class as a different type of symbol (presumably c++ class vs objective-c protocol). If I try to forward declare it as @protocol myClassName, I get an error about “expected unqualified-id before ‘@’ token”. So how can I make this work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:22:45+00:00Added an answer on May 26, 2026 at 10:22 am

    Ok to answer your question:

    reportwindow.h is additionally included in a .cpp file, if that makes
    a difference.

    It does make a difference. Any compilation unit (cpp file in this case) that is touching Objective-C code has to be renamed to .mm or .m. Including the header that in turn is including Objective-C stuff in a C++ file will lead to the problem that the C++ compiler sees Objective-C code which it cannot handle.

    Renaming the cpp file to mm will select the Objective-C option during compilation (which isn’t when the file is named cpp or c) and hence allow to compile stuff with the Objective-C tokens (mainly “@” in your case).

    An alternative would be not to include the Objective-C delegate class to your C++ class but rather include a pointer to your C++ class within the Objective-C delegate (i.e. implement it the other way around). This way you could arrange things such that the Objective-C code isn’t touching the C++ code.

    Edit: Actually, I’d prefer the second suggestion. Here is an example:

    DelegateClass.h:

    class MyCPPClassHandlingStuff;
    
    @interface MyDelegateObject : NSObject <SomeDelegateProtocol> {
      MyCPPClassHandlingStuff *m_handlerInstance;
    }
    
    - (id) initWithCPPInstance:(MyCPPClassHandlingStuff*)cppInstance;
    
    - (void) theDelegateProtocolMethod;
    
    @end
    

    DelegateClass.mm

    #include "MyCPPClassHandlingStuff.h"
    
    @implementation MyDelegateObject
    
    - (id) initWithCPPInstance:(MyCPPClassHandlingStuff*)cppInstance
    {
      self = [super init];
      if (self) {
        m_handlerInstance = cppInstance;
      }
      return self;
    }
    
    - (void) theDelegateProtocolMethod
    {
      if (m_handlerInstance)
        m_handlerInstance->handleDelegateMethod();
    }
    
    @end
    

    And well the MyCPPClassHandlingStuff.h:

    #ifndef __MyCPPClassHandlingStuff_H__
    #define __MyCPPClassHandlingStuff_H__
    
    class MyCPPClassHandlingStuff
    {
    public:
      MyCPPClassHandlingStuff();
      void handleDelegateMethod();
    };
    
    #endif /* __MyCPPClassHandlingStuff_H__ */
    

    MyCPPClassHandlingStuff can be initialized from Objective-C but you cannot initialise any Objective-C class from C++ code there. If you need to use Objective-C in your C++ code, you would have to compile it as Objective-C (i.e. use an .mm file). I leave the .cpp details as an exercise for the reader 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Short version: Say I have a string str and a file functions.py from which
Short version: I have a similar setup to StackOverflow. Users get Achievements. I have
Short version: I have a Django project under development & testing (not yet into
I have a short version of the question: I start a thread like that:
I have the following short version of a tsql if else if.. IF @var
The short version of my question may be: Do Magento and Wordpress have 301
Short Version: When I've created a Channel using ChannelFactory on a client which uses
Short version of what I want to accomplish : I have a foot pedal
Short version : How can I write an SQL procedure to list which of
Short version: How do I create the <> column manually? Long version: In Access

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.