Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

How to use Share API in macOS and Qt Quick

From Qt Wiki
Revision as of 15:54, 14 January 2015 by Maintenance script (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Hi everyone!

In this article I’ve tried to explain how to use Mac OS X Share API in your Qt Quick app. Share API was implemented in Mac OS 10.10 (Yosemite).

As you know all frameworks in OS X which you could use in your application created with Objective-C/C++. That’s why you can’t use framework in your C++ classes. You need to rename your .cpp file to .mm and then you can call Objective-C code. But sometimes you need to call methods with arguments with Objective-C. As you know in .h file you can’t call Objective-C code. You must create Objective-C class which will call all needed methods to work with target framework.

Classes with Share API support.

First you need to create class with all methods in Objective-C/C++ to use Share API. To do this you need follow a few steps:

  1. Create a new class and rename .cpp file to .mm.
  2. Add .h file to .pro of your project in section “OBJECTIVE_HEADERS” and .mm to “OBJECTIVE_SOURCES”.
  3. Change .h file content to this code:
  4. Change .mm file content to this code:
    p. More information you can find in official Apple documentation [developer.apple.com]. And you need to see information about class NSSharingServicePickerDelegate [developer.apple.com].

Create QtQuick Item to use Share API from App.

Second class which you need to create is a class to call from C++. This class will call Objective-C/C++. To do this you need follow a few steps:

  1. Create a new C++ class.
  2. Rename .cpp file to .mm and added it to “OBJECTIVE_SOURCES”.
  3. Change .h file content to code:
  4. Change .mm file content to code:
  5. Register new type to use in QML.

How to use in QML.

Import the new type to your QML file where you need to use share logic and add this code:

Summary

This Share item can share only text and link. If you need to share Image you can implement special logic to convert QImage to NSImage. And added a new parameter to method: shareCurrentContent().