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 call macOS APIs from c++/qt application?
Forum Updated to NodeBB v4.3 + New Features

How to call macOS APIs from c++/qt application?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 924 Views 3 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.
  • T Offline
    T Offline
    Taytoo
    wrote on last edited by
    #1

    I need to call a native macOS API: NSHost to get localizedName of the mac but no idea how to call either Objective-C or Swift version of the API.

    PS. Don't know anything about objective-C or Swift, new to macOS development.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      HI,

      1. create a new file with .mm extension
      2. write your c ++ function in it
      3. call it from your Qt app

      example:
      MacUtils.h

      class MacUtils
      {
      
      		MacUtils() {}
      
      	public:
      
          static QString  localizedHostName();
      };
      

      MacUtils.mm

      #import <Cocoa/Cocoa.h>
      #include "MacUtils.h"
      
      
      QString MacUtils::localizedHostName()
      {
          NSString* name=[[NSHost currentHost] localizedName];
      
          return QString::fromNSString(name);
      }
      

      Call it like this:

      #include "MacUtils.h"
      
      int main(int argc, char *argv[])
      {
          QString name=MacUtils::localizedHostName();
          qDebug()<<name;
      }
      

      For me prints: iMac de Jean

      T 1 Reply Last reply
      3
      • M mpergand

        HI,

        1. create a new file with .mm extension
        2. write your c ++ function in it
        3. call it from your Qt app

        example:
        MacUtils.h

        class MacUtils
        {
        
        		MacUtils() {}
        
        	public:
        
            static QString  localizedHostName();
        };
        

        MacUtils.mm

        #import <Cocoa/Cocoa.h>
        #include "MacUtils.h"
        
        
        QString MacUtils::localizedHostName()
        {
            NSString* name=[[NSHost currentHost] localizedName];
        
            return QString::fromNSString(name);
        }
        

        Call it like this:

        #include "MacUtils.h"
        
        int main(int argc, char *argv[])
        {
            QString name=MacUtils::localizedHostName();
            qDebug()<<name;
        }
        

        For me prints: iMac de Jean

        T Offline
        T Offline
        Taytoo
        wrote on last edited by
        #3

        @mpergand Simple, to the point and it works. Merci!

        When I was trying to find a solution, noticed some people saying that I have to add framework reference in .pro file e.g.

        macx {
            LIBS += -framework CoreServices
            LIBS += -framework Foundation
            LIBS += -framework CoreFoundation
        }
        

        Your code works without the framework references. Are macOS framework references Not needed at all OR are they needed in some cases?

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

          Hi,

          It will depend on what API you use. If they fall in frameworks already used by Qt, they will already be part of the linker options.

          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
          1
          • T Taytoo

            @mpergand Simple, to the point and it works. Merci!

            When I was trying to find a solution, noticed some people saying that I have to add framework reference in .pro file e.g.

            macx {
                LIBS += -framework CoreServices
                LIBS += -framework Foundation
                LIBS += -framework CoreFoundation
            }
            

            Your code works without the framework references. Are macOS framework references Not needed at all OR are they needed in some cases?

            M Offline
            M Offline
            mpergand
            wrote on last edited by mpergand
            #5

            @Taytoo

            I add this to my .pro :

            macx {   # l'accolade doit se trouver sur la même ligne, sinon erreur sur linux
            
             LIBS +=	-framework AppKit
            }
            
            1 Reply Last reply
            1

            • Login

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