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
[toc align_right="yes" depth="2"]
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.
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:
# Create a new class and rename .cpp file to .mm.
# Add .h file to .pro of your project in section "OBJECTIVE_HEADERS" and .mm to "OBJECTIVE_SOURCES".
# Change .h file content to this code:
#ifdef __cplusplus<br />extern "C&quot; {<br />#endif
#import <Foundation/Foundation.h&gt;<br />#import <Social/Social.h&gt;
/**
typedef QfSharePickerItemClicked
*
param sharingService Selected type of sharing.<br /> *
author Andrew Shapovelov*/
typedef void(
/**
class QfSharePicker<br /> *
*
author Andrew Shapovalov*/<br />
/**
brief Create a new object.<br /> *
*
param frame Rect to show share window.<br /> *
*
param block Block of code to select item.<br /> *
- (instancetype)initWithView:(NSView*)view frame:(NSRect)frame datasArray:(NSArray*)datas onItemClicked:(QfSharePickerItemClicked)block;
end
<br />#ifdef __cplusplus<br />}<br />#endif<br />
# Change .mm file content to this code:
<br />#import "qfsharepicker.h&quot;<br />#include <QtCore&gt;
<br />/**
*
author Andrew Shapovalov*/<br />
/** Sharing service picker./
property (nonatomic, retain) NSSharingServicePicker''' picker;
<br />/** Block of code to select item.'''/<br />
end
<br />
#pragma mark - Init methods
- (instancetype)initWithView:(NSView)view frame:(NSRect)frame datasArray:(NSArray*)datas onItemClicked:(QfSharePickerItemClicked)block
{
self = [super init];
if(self)
{
self.onItemClicked = block;
self.picker = [[NSSharingServicePicker alloc] initWithItems: datas];
self.picker.delegate = self;
[self.picker showRelativeToRect:frame ofView:view preferredEdge:NSMinXEdge];
}
return self;
}
#pragma mark - NSSharingServicePickerDelegate methods
- (void)sharingServicePicker:(NSSharingServicePicker )sharingServicePicker didChooseSharingService:(NSSharingService)service
{
if(self.picker == sharingServicePicker)
{
if(self.onItemClicked)
{
self.onItemClicked(service);
}
}
}
- (id <NSSharingServiceDelegate>)sharingServicePicker:(NSSharingServicePicker )sharingServicePicker delegateForSharingService:(NSSharingService)sharingService
{
Q_UNUSED(sharingService);
if(self.picker == sharingServicePicker)
{
}
return self;
}
#pragma mark - NSSharingServiceDelegate methods
- (void)sharingService:(NSSharingService )sharingService willShareItems:(NSArray)items
{
Q_UNUSED(sharingService);
Q_UNUSED(items);
//Some code here
}
- (void)sharingService:(NSSharingService )sharingService didFailToShareItems:(NSArray)items error:(NSError )error
{
Q_UNUSED(sharingService);
Q_UNUSED(items);
Q_UNUSED(error);
//Some code here
}
- (void)sharingService:(NSSharingService)sharingService didShareItems:(NSArray )items
{
Q_UNUSED(sharingService);
Q_UNUSED(items);
//Some code here
}
#pragma mark - Clear memory
- (void)dealloc
{
[super dealloc];
[self.picker autorelease];
[self.onItemClicked release];
self.onItemClicked = nil;
}
end<br />
p. More information you can find in official Apple "documentation":https://developer.apple.com/library/mac/DOCUMENTATION/AppKit/Reference/NSSharingServicePicker_Class/index.html. And you need to see information about class "NSSharingServicePickerDelegate":https://developer.apple.com/library/mac/DOCUMENTATION/AppKit/Reference/NSSharingServicePickerDelegate_Protocol/index.html#//apple_ref/occ/intf/NSSharingServicePickerDelegate.
h1. Create QtQuick Item to use Share API from App.
p. 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:
# Create a new C+ class.
# Rename .cpp file to .mm and added it to "OBJECTIVE_SOURCES".
# Change .h file content to code:
<br />#include <QtCore&gt;<br />#include <QtQuick&gt;
<br />/'''*
*
brief Manager to use share logic.<br /> *
class QfShareItem : public QQuickPaintedItem
{
Q_OBJECT
Q_PROPERTY(QString shareString READ getShareString WRITE setShareString NOTIFY shareStringChanged)
Q_PROPERTY(QUrl shareUrl READ getShareUrl WRITE setShareUrl NOTIFY shareUrlChanged)
private:
/** The share string info./
QString m_shareString;
/* The share link./
QUrl m_shareUrl;
/*
brief Strip all HTML tags from string.<br /> *
*
return Plain text.<br /> *
QString stripHTMLTags(QString body);
/**
brief Works with all applications events.<br /> *
*
param event The event of call.<br /> *
bool eventFilter(QObject* obj, QEvent* event);
/**
brief Share current content.<br /> *
void shareCurrentContent();
public:
/**
brief Create a new object.<br /> *
*
author Andrew Shapovalov*/<br /> explicit QfShareItem(QQuickPaintedItem '''parent = 0);
<br /> //Others<br /> /'''*
*
param painter The object for draw data.<br /> *
void paint(QPainter painter);
//Getters
/*
brief Get share content string.<br /> *
*
author Andrew Shapovalov*/<br /> inline QString getShareString(){return m_shareString;}
<br /> /**
*
return Share url.<br /> *
inline QUrl getShareUrl(){return m_shareUrl;}
//Setters
/**
brief Set share content string.<br /> *
*
author Andrew Shapovalov*/<br /> inline void setShareString(QString value){m_shareString = value; emit shareStringChanged(m_shareString);}
<br /> /**
*
param value Share url.<br /> *
inline void setShareUrl(QUrl value){m_shareUrl = value; emit shareUrlChanged(m_shareUrl);}
/**
brief Share content and url.<br /> *
*
param url The url to share.<br /> *
Q_INVOKABLE void shareContent(QString text = QString(), QUrl url = QUrl());
signals:
/**
brief Called when share content string was changed.<br /> *
*
author Andrew Shapovalov*/<br /> void shareStringChanged(QString value);
<br /> /**
*
param value Share url.<br /> *
void shareUrlChanged(QUrl value);
/**
brief Called when user select share service.<br /> *
*
author Andrew Shapovalov*/<br /> void selectedService(QString serviceName);
<br /> public slots:<br />};<br />
# Change .mm file content to code:
<br />#include "qfshareitem.h&quot;<br />#include "qfsharepicker.h&quot;
<br />QString QfShareItem::stripHTMLTags(QString body)<br />{<br /> body.replace("<br&gt;","");<br /> body.replace("</br&gt;","");<br /> body.replace("</p&gt;","");<br /> body.replace("</td&gt;","");<br /> body.remove(QRegExp("&lt;head&amp;gt;(.''')&lt;/head&amp;gt;"));<br /> body.remove(QRegExp("&lt;form(.''')&lt;/form&gt;"));<br /> body.remove(QRegExp( "<(.)[</sup>>]'''>"));
<br /> return body.trimmed();<br />}
<br />bool QfShareItem::eventFilter(QObject''' obj, QEvent* event)<br />{<br /> if(obj == this)<br /> {<br /> if(parentItem())<br /> {<br /> return QObject::eventFilter(parentItem(), event);<br /> }<br /> }
return QObject::eventFilter(obj, event);<br />}
void QfShareItem::shareCurrentContent()<br />{<br /> QQuickItem* parentItem = this->parentItem();<br /> if(!m_shareString.isEmpty() && parentItem)<br /> {<br /> QRectF rect = parentItem->mapRectToItem(NULL, parentItem->boundingRect());<br /> NSView* view = reinterpret_cast&lt;NSView *>(parentItem->window()<s>>winId());<br /> NSRect frame = NSMakeRect(rect.x(), rect.y(), rect.width(), rect.height());
<br /> m_shareString = m_shareString.replace("&lt;style type=quot;text/cssquot;&gt;a {color:#44a51c;text-decoration:none;}&lt;/style&amp;gt;", "");<br /> QString content = stripHTMLTags(m_shareString).trimmed();<br /> NSMutableArray* datas = [NSMutableArray arrayWithObject: content.toNSString()];<br /> if(!m_shareUrl.isEmpty())<br /> {<br /> NSURL* url = [NSURL URLWithString: m_shareUrl.toString().toNSString()];<br /> if(url)<br /> {<br /> [datas addObject: url];<br /> }<br /> }<br /> QfSharePicker* sharePicker = [[QfSharePicker alloc] initWithView:view frame:frame datasArray:datas onItemClicked:nil];<br /> [sharePicker autorelease];<br /> }<br />}
<br />QfShareItem::QfShareItem(QQuickPaintedItem '''parent) :<br /> QQuickPaintedItem(parent)<br />{<br /> m_shareString.clear();<br /> m_shareUrl.clear();
<br /> connect(this, &QQuickPaintedItem::parentChanged, [this](QQuickItem''' newParent){
<br /> if(newParent)<br /> {<br /> newParent</s>>setFiltersChildMouseEvents(true);<br /> }<br /> });<br /> this->installEventFilter(this);
setFlag(QQuickPaintedItem::ItemHasContents, true);<br /> setFlag(QQuickPaintedItem::ItemClipsChildrenToShape, true);<br /> setFlag(QQuickPaintedItem::ItemAcceptsDrops, true);<br /> setRenderTarget(QQuickPaintedItem::InvertedYFramebufferObject);<br />}
void QfShareItem::paint(QPainter *painter)<br />{<br /> try<br /> {<br /> Q_UNUSED(painter);<br /> }<br /> catch(std::exception&amp; exception)<br /> {<br /> qDebug()<<"exception: "<<exception.what();<br /> }<br />}
void QfShareItem::shareContent(QString text, QUrl url)<br />{<br /> if(!text.isEmpty())<br /> {<br /> m_shareString = text;<br /> }
if(!url.isEmpty())<br /> {<br /> m_shareUrl = url;<br /> }
shareCurrentContent();<br />}<br />
# Register new type to use in QML.
<br />qmlRegisterType&lt;YourShareItem&gt;(uri, 1, 1, "YourShareItem&quot;);<br />
How to use in QML.
Import the new type to your QML file where you need to use share logic and add this code:<br />Button {<br /> id: btnLink<br /> width: 22<br /> height: 22
onClicked: {<br /> share.shareContent();<br /> }
QfShareItem {<br /> id: share<br /> anchors.fill: btnLink<br /> shareUrl: userPanelView.shareUrl<br /> }<br />}<br />