Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use Cocoa with Qt
Qt 6.11 is out! See what's new in the release blog

How to use Cocoa with Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 2 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kamkat23
    wrote on last edited by
    #1

    As the title says, i can't import cocoa because it treats it like a c++ code. Fresh Install of Qt.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      How do you do it ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kamkat23
        wrote on last edited by
        #3

        In the project file, i add to the bottom "mac: LIBS += -framework Cocoa"

        Second, i change my header into an objective header.

        Third, i import <Cocoa/Cocoa.h>

        Then 54 errors pop up.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Did you do that in a .cpp file ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kamkat23
            wrote on last edited by
            #5

            I turn the cpp file into .mm and turn it into a OBJECTIVE_SOURCES

            the errors seem to come from Foundation which cocoa depends on.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Then link the Foundation framework as well.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kamkat23
                wrote on last edited by
                #7

                Already tried that, the errors come from the headers and sources of Foundation.

                Log

                492:1: error: expected unqualified-id
                @class NSString, Protocol;
                ^
                /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:503:50: error: unknown type name 'Protocol'
                FOUNDATION_EXPORT NSString *NSStringFromProtocol(Protocol *proto) API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
                                                                 ^
                /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:504:19: error: unknown type name 'Protocol'
                FOUNDATION_EXPORT Protocol * _Nullable NSProtocolFromString(NSString *namestr) API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
                                  ^
                /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:508:53: error: format argument not an NSString
                FOUNDATION_EXPORT void NSLog(NSString *format, ...) NS_FORMAT_FUNCTION(1,2) NS_NO_TAIL_CALL;
                                             ~~~~~~~~~~~~~~~~       ^                  ~
                /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:94:49: note: expanded from macro 'NS_FORMAT_FUNCTION'
                        #define NS_FORMAT_FUNCTION(F,A) __attribute__((format(__NSString__, F, A)))
                                                                       ^                    ~
                /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:509:63: error: format argument not an NSString
                FOUNDATION_EXPORT void NSLogv(NSString *format, va_list args) NS_FORMAT_FUNCTION(1,0) NS_NO_TAIL_CALL;
                                              ~~~~~~~~~~~~~~~~                ^                  ~
                /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:94:49: note: expanded from macro 'NS_FORMAT_FUNCTION'
                        #define NS_FORMAT_FUNCTION(F,A) __attribute__((format(__NSString__, F, A)))
                                                                       ^                    ~
                In file included from ../generator/main.cpp:1:
                In file included from ../generator/generatormain.h:5:
                In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Cocoa.framework/Headers/Cocoa.h:12:
                In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:10:
                In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSArray.h:5:
                In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:8:
                /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSZone.h:9:1: error: expected unqualified-id
                @class NSString;
                ^
                In file included from ../generator/main.cpp:1:
                In file included from ../generator/generatormain.h:5:
                In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Cocoa.framework/Headers/Cocoa.h:12:
                In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:10:
                In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSArray.h:5:
                /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:10:1: error: expected unqualified-id
                @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
                ^
                /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:11:1: error: expected unqualified-id
                @class Protocol;
                ^
                /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:17:1: error: expected unqualified-id
                @protocol NSCopying
                ^
                /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:21:1: error: expected unqualified-id
                @end
                ^
                /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:27:1: error: expected unqualified-id
                @end
                ^
                /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:32:1: error: expected external declaration
                - (nullable instancetype)initWithCoder:(NSCoder *)coder; // NS_DESIGNATED_INITIALIZER
                ^
                /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:32:13: error: expected ')'
                - (nullable instancetype)initWithCoder:(NSCoder *)coder; // NS_DESIGNATED_INITIALIZER
                            ^
                /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:231:22: note: expanded from macro 'instancetype'
                #define instancetype id
                                     ^
                /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:32:3: note: to match this '('
                - (nullable instancetype)initWithCoder:(NSCoder *)coder; // NS_DESIGNATED_INITIALIZER
                  ^
                /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:32:4: error: C++ requires a type specifier for all declarations
                - (nullable instancetype)initWithCoder:(NSCoder *)coder; // NS_DESIGNATED_INITIALIZER
                   ^
                /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:32:26: error: expected ';' after top level declarator
                - (nullable instancetype)initWithCoder:(NSCoder *)coder; // NS_DESIGNATED_INITIALIZER
                                         ^
                /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:34:1: error: expected unqualified-id
                @end
                ^
                /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:44:1: error: expected unqualified-id
                @end
                ^
                /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:51:1: error: expected external declaration
                + (void)setVersion:(NSInteger)aVersion;
                ^
                /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:51:4: error: expected unqualified-id
                + (void)setVersion:(NSInteger)aVersion;
                   ^
                fatal error: too many errors emitted, stopping now [-ferror-limit=]
                20 errors generated.
                make: *** [main.o] Error 1
                22:54:28: The process "/usr/bin/make" exited with code 2.
                Error while building/deploying project generator (kit: Desktop Qt 6.0.0 clang 64bit)
                When executing step "Make"
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Can you share your .mm and .pro file ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kamkat23
                    wrote on last edited by
                    #9

                    Project File

                    QT       += core gui
                    
                    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                    
                    CONFIG += c++11
                    
                    # You can make your code fail to compile if it uses deprecated APIs.
                    # In order to do so, uncomment the following line.
                    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                    
                    SOURCES += \
                        main.cpp
                        
                    OBJECTIVE_SOURCES += \
                        mainwindow.mm
                    
                    OBJECTIVE_HEADERS += \
                        mainwindow.h
                    
                    FORMS += \
                        mainwindow.ui
                    
                    # Default rules for deployment.
                    qnx: target.path = /tmp/$${TARGET}/bin
                    else: unix:!android: target.path = /opt/$${TARGET}/bin
                    !isEmpty(target.path): INSTALLS += target
                    
                    mac: LIBS += -framework Cocoa
                    
                    mac: LIBS += -framework Foundation
                    
                    

                    .mm File

                    #include "mainwindow.h"
                    #include "ui_mainwindow.h"
                    
                    MainWindow::MainWindow(QWidget *parent)
                        : QMainWindow(parent)
                        , ui(new Ui::MainWindow)
                    {
                        ui->setupUi(this);
                    }
                    
                    MainWindow::~MainWindow()
                    {
                        delete ui;
                    }
                    
                    
                    
                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      There's no imports in your .mm file.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved