How to use Share API in macOS and Qt Quick: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
[toc align_right="yes" depth="2"]
Hi everyone!
Hi everyone!


In this article I’ve tried to explain how to use Mac OS X Share <span class="caps">API</span> in your Qt Quick app. Share <span class="caps">API</span> was implemented in Mac OS 10.10 (Yosemite).
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:<br /># Create a new class and rename .cpp file to .mm.<br /># Add .h file to .pro of your project in section &quot;OBJECTIVE_HEADERS&amp;quot; and .mm to &quot;OBJECTIVE_SOURCES&amp;quot;.<br /># Change .h file content to this code:<br /><code>#ifdef __cplusplus<br />extern &quot;C&amp;quot; {<br />#endif
 
#import &lt;Foundation/Foundation.h&amp;gt;<br />#import &lt;Social/Social.h&amp;gt;
 
/** </code>typedef QfSharePickerItemClicked<br /> * <code>param sharingService Selected type of sharing.<br /> * </code>author Andrew Shapovelov*/<br />typedef void(<sup>QfSharePickerItemClicked)(NSSharingService* sharingService);
 
<br />/** <code>class QfSharePicker<br /> * </code>brief Object to show share menu.<br /> * <code>author Andrew Shapovalov*/<br /></code>interface QfSharePicker : NSObject
<br />/** <code>brief Create a new object.<br /> * </code>param view View on what will show share menu.<br /> * <code>param frame Rect to show share window.<br /> * </code>param datas Datas items to share.<br /> * <code>param block Block of code to select item.<br /> * </code>author Andrew Shapovalov*/<br />- (instancetype)initWithView:(NSView*)view frame:(NSRect)frame datasArray:(NSArray*)datas onItemClicked:(QfSharePickerItemClicked)block;
<br /><code>end
<br />#ifdef __cplusplus<br />}<br />#endif<br /></code><br /># Change .mm file content to this code:<br /><code><br />#import &quot;qfsharepicker.h&amp;quot;<br />#include &lt;QtCore&amp;gt;
<br />/** </code>brief The private methods and properties of QfSharePicker class.<br /> * <code>author Andrew Shapovalov*/<br /></code>interface QfSharePicker () &lt;NSSharingServicePickerDelegate, NSSharingServiceDelegate&amp;gt;
<br />/** Sharing service picker.'''/<br /><code>property (nonatomic, retain) NSSharingServicePicker''' picker;
<br />/** Block of code to select item.'''/<br /></code>property (nonatomic, copy) QfSharePickerItemClicked onItemClicked;
<br /><code>end
 
<br /></code>implementation QfSharePicker
<br />#pragma mark - Init methods
<br />- (instancetype)initWithView:(NSView''')view frame:(NSRect)frame datasArray:(NSArray*)datas onItemClicked:(QfSharePickerItemClicked)block<br />{<br /> self = [super init];
<br /> if(self)<br /> {<br /> self.onItemClicked = block;<br /> self.picker = [[NSSharingServicePicker alloc] initWithItems: datas];<br /> self.picker.delegate = self;<br /> [self.picker showRelativeToRect:frame ofView:view preferredEdge:NSMinXEdge];<br /> }
<br /> return self;<br />}
 
<br />#pragma mark - NSSharingServicePickerDelegate methods
<br />- (void)sharingServicePicker:(NSSharingServicePicker ''')sharingServicePicker didChooseSharingService:(NSSharingService''')service<br />{<br /> if(self.picker == sharingServicePicker)<br /> {<br /> if(self.onItemClicked)<br /> {<br /> self.onItemClicked(service);<br /> }<br /> }<br />}
<br />- (id &lt;NSSharingServiceDelegate&amp;gt;)sharingServicePicker:(NSSharingServicePicker ''')sharingServicePicker delegateForSharingService:(NSSharingService''')sharingService<br />{<br /> Q_UNUSED(sharingService);
<br /> if(self.picker == sharingServicePicker)<br /> {<br /> }
<br /> return self;<br />}
<br />#pragma mark - NSSharingServiceDelegate methods
<br />- (void)sharingService:(NSSharingService ''')sharingService willShareItems:(NSArray''')items<br />{<br /> Q_UNUSED(sharingService);<br /> Q_UNUSED(items);
<br /> //Some code here<br />}
<br />- (void)sharingService:(NSSharingService ''')sharingService didFailToShareItems:(NSArray''')items error:(NSError ''')error<br />{<br /> Q_UNUSED(sharingService);<br /> Q_UNUSED(items);<br /> Q_UNUSED(error);
<br /> //Some code here<br />}
<br />- (void)sharingService:(NSSharingService''')sharingService didShareItems:(NSArray ''')items<br />{<br /> Q_UNUSED(sharingService);<br /> Q_UNUSED(items);
<br /> //Some code here<br />}
<br />#pragma mark - Clear memory
<br />- (void)dealloc<br />{<br /> [super dealloc];
<br /> [self.picker autorelease];<br /> [self.onItemClicked release];<br /> self.onItemClicked = nil;<br />}
<br /><code>end<br /></code><br />p. More information you can find in official Apple &quot;documentation&amp;quot;:https://developer.apple.com/library/mac/DOCUMENTATION/AppKit/Reference/NSSharingServicePicker_Class/index.html. And you need to see information about class &quot;NSSharingServicePickerDelegate&amp;quot;:https://developer.apple.com/library/mac/DOCUMENTATION/AppKit/Reference/NSSharingServicePickerDelegate_Protocol/index.html#//apple_ref/occ/intf/NSSharingServicePickerDelegate.
<br />h1. Create QtQuick Item to use Share API from App.
<br />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:<br /># Create a new C''+ class.<br /># Rename .cpp file to .mm and added it to &quot;OBJECTIVE_SOURCES&amp;quot;.<br /># Change .h file content to code:<br /><code>
<br />#include &lt;QtCore&amp;gt;<br />#include &lt;QtQuick&amp;gt;
<br />/'''* </code>class QfShareItem<br /> * <code>brief Manager to use share logic.<br /> * </code>author Andrew Shapovalov*/<br />class QfShareItem : public QQuickPaintedItem<br />{<br /> Q_OBJECT<br /> Q_PROPERTY(QString shareString READ getShareString WRITE setShareString NOTIFY shareStringChanged)<br /> Q_PROPERTY(QUrl shareUrl READ getShareUrl WRITE setShareUrl NOTIFY shareUrlChanged)
<br /> private:<br /> /** The share string info.'''/<br /> QString m_shareString;
<br /> /'''* The share link.'''/<br /> QUrl m_shareUrl;
<br /> /'''* <code>brief Strip all HTML tags from string.<br /> * </code>param body The source string.<br /> * <code>return Plain text.<br /> * </code>author Andrew Shapovalov*/<br /> QString stripHTMLTags(QString body);
<br /> /** <code>brief Works with all applications events.<br /> * </code>param obj The object which call event.<br /> * <code>param event The event of call.<br /> * </code>author Andrew Shapovalov*/<br /> bool eventFilter(QObject* obj, QEvent* event);
<br /> /** <code>brief Share current content.<br /> * </code>author Andrew Shapovalov*/<br /> void shareCurrentContent();
<br /> public:<br /> /** <code>brief Create a new object.<br /> * </code>param parent Parent object.<br /> * <code>author Andrew Shapovalov*/<br /> explicit QfShareItem(QQuickPaintedItem '''parent = 0);
<br /> //Others<br /> /'''* </code>brief Called when current object will redrawing.<br /> * <code>param painter The object for draw data.<br /> * </code>author Andrew Shapovalov*/<br /> void paint(QPainter '''painter);
<br /> //Getters<br /> /'''* <code>brief Get share content string.<br /> * </code>return Share string content.<br /> * <code>author Andrew Shapovalov*/<br /> inline QString getShareString(){return m_shareString;}
<br /> /** </code>brief Get share url.<br /> * <code>return Share url.<br /> * </code>author Andrew Shapovalov*/<br /> inline QUrl getShareUrl(){return m_shareUrl;}
<br /> //Setters<br /> /** <code>brief Set share content string.<br /> * </code>param value Share string content.<br /> * <code>author Andrew Shapovalov*/<br /> inline void setShareString(QString value){m_shareString = value; emit shareStringChanged(m_shareString);}
<br /> /** </code>brief Set share url.<br /> * <code>param value Share url.<br /> * </code>author Andrew Shapovalov*/<br /> inline void setShareUrl(QUrl value){m_shareUrl = value; emit shareUrlChanged(m_shareUrl);}
<br /> /** <code>brief Share content and url.<br /> * </code>param text The text of share.<br /> * <code>param url The url to share.<br /> * </code>author Andrew Shapovalov*/<br /> Q_INVOKABLE void shareContent(QString text = QString(), QUrl url = QUrl());
<br /> signals:<br /> /** <code>brief Called when share content string was changed.<br /> * </code>param value Share string content.<br /> * <code>author Andrew Shapovalov*/<br /> void shareStringChanged(QString value);
<br /> /** </code>brief Called when share url was changed.<br /> * <code>param value Share url.<br /> * </code>author Andrew Shapovalov*/<br /> void shareUrlChanged(QUrl value);
<br /> /** <code>brief Called when user select share service.<br /> * </code>param serviceName The name of selected service.<br /> * <code>author Andrew Shapovalov*/<br /> void selectedService(QString serviceName);
<br /> public slots:<br />};<br /></code><br /># Change .mm file content to code:<br /><code><br />#include &quot;qfshareitem.h&amp;quot;<br />#include &quot;qfsharepicker.h&amp;quot;
<br />QString QfShareItem::stripHTMLTags(QString body)<br />{<br /> body.replace(&quot;&lt;br&amp;gt;&quot;,&quot;&quot;);<br /> body.replace(&quot;&lt;/br&amp;gt;&quot;,&quot;&quot;);<br /> body.replace(&quot;&lt;/p&amp;gt;&quot;,&quot;&quot;);<br /> body.replace(&quot;&lt;/td&amp;gt;&quot;,&quot;&quot;);<br /> body.remove(QRegExp(&quot;&amp;lt;head&amp;amp;gt;(.''')&amp;lt;/head&amp;amp;gt;&quot;));<br /> body.remove(QRegExp(&quot;&amp;lt;form(.''')&amp;lt;/form&amp;gt;&quot;));<br /> body.remove(QRegExp( &quot;&lt;(.)[</sup>&gt;]'''&gt;&quot;));
<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 />}


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.
void QfShareItem::shareCurrentContent()<br />{<br /> QQuickItem* parentItem = this-&gt;parentItem();<br /> if(!m_shareString.isEmpty() &amp;&amp; parentItem)<br /> {<br /> QRectF rect = parentItem-&gt;mapRectToItem(NULL, parentItem-&gt;boundingRect());<br /> NSView* view = reinterpret_cast&amp;lt;NSView *&gt;(parentItem-&gt;window()<s>&gt;winId());<br /> NSRect frame = NSMakeRect(rect.x(), rect.y(), rect.width(), rect.height());
<br /> m_shareString = m_shareString.replace(&quot;&amp;lt;style type=quot;text/cssquot;&amp;gt;a {color:#44a51c;text-decoration:none;}&amp;lt;/style&amp;amp;gt;&quot;, &quot;&quot;);<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, &amp;QQuickPaintedItem::parentChanged, [this](QQuickItem''' newParent){
<br /> if(newParent)<br /> {<br /> newParent</s>&gt;setFiltersChildMouseEvents(true);<br /> }<br /> });<br /> this-&gt;installEventFilter(this);


=Classes with Share <span class="caps">API</span> support.=
setFlag(QQuickPaintedItem::ItemHasContents, true);<br /> setFlag(QQuickPaintedItem::ItemClipsChildrenToShape, true);<br /> setFlag(QQuickPaintedItem::ItemAcceptsDrops, true);<br /> setRenderTarget(QQuickPaintedItem::InvertedYFramebufferObject);<br />}


First you need to create class with all methods in Objective-C/C++ to use Share <span class="caps">API</span>. To do this you need follow a few steps:
void QfShareItem::paint(QPainter *painter)<br />{<br /> try<br /> {<br /> Q_UNUSED(painter);<br /> }<br /> catch(std::exception&amp;amp; exception)<br /> {<br /> qDebug()&lt;&lt;&quot;exception: &quot;&lt;&lt;exception.what();<br /> }<br />}


# Create a new class and rename .cpp file to .mm.
void QfShareItem::shareContent(QString text, QUrl url)<br />{<br /> if(!text.isEmpty())<br /> {<br /> m_shareString = text;<br /> }
# Add .h file to .pro of your project in section “<span class="caps">OBJECTIVE</span>_HEADERS” and .mm to “<span class="caps">OBJECTIVE</span>_SOURCES”.
# Change .h file content to this code:<br />
# Change .mm file content to this code:<br /> p. More information you can find in official Apple [https://developer.apple.com/library/mac/DOCUMENTATION/AppKit/Reference/NSSharingServicePicker_Class/index.html documentation] ''[developer.apple.com]''. And you need to see information about class [https://developer.apple.com/library/mac/DOCUMENTATION/AppKit/Reference/NSSharingServicePickerDelegate_Protocol/index.html#//apple_ref/occ/intf/NSSharingServicePickerDelegate NSSharingServicePickerDelegate] ''[developer.apple.com]''.


=Create QtQuick Item to use Share <span class="caps">API</span> from App.=
if(!url.isEmpty())<br /> {<br /> m_shareUrl = url;<br /> }


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:
shareCurrentContent();<br />}<br /></code><br /># Register new type to use in QML.<br /><code><br />qmlRegisterType&amp;lt;YourShareItem&amp;gt;(uri, 1, 1, &quot;YourShareItem&amp;quot;);<br /></code>


# Create a new C++ class.
= How to use in QML. =
# Rename .cpp file to .mm and added it to “<span class="caps">OBJECTIVE</span>_SOURCES”.
# Change .h file content to code:<br />
# Change .mm file content to code:<br />
# Register new type to use in <span class="caps">QML</span>.<br />


=How to use in <span class="caps">QML</span>.=
Import the new type to your QML file where you need to use share logic and add this code:<br /><code><br />Button {<br /> id: btnLink<br /> width: 22<br /> height: 22


Import the new type to your <span class="caps">QML</span> file where you need to use share logic and add this code:<br />
onClicked: {<br /> share.shareContent();<br /> }


=Summary=
QfShareItem {<br /> id: share<br /> anchors.fill: btnLink<br /> shareUrl: userPanelView.shareUrl<br /> }<br />}<br /></code>


This Share item can share only text and link. If you need to share Image you can implement special logic to convert QImage to <span class="caps">NSI</span>mage. And added a new parameter to method: shareCurrentContent().
= Summary =

Revision as of 11:02, 24 February 2015

[toc align_right="yes&quot; depth="2&quot;]

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:
# Create a new class and rename .cpp file to .mm.
# Add .h file to .pro of your project in section "OBJECTIVE_HEADERS&quot; and .mm to "OBJECTIVE_SOURCES&quot;.
# Change .h file content to this code:

#ifdef __cplusplus<br />extern &quot;C&amp;quot; {<br />#endif

#import &lt;Foundation/Foundation.h&amp;gt;<br />#import &lt;Social/Social.h&amp;gt;

/**

typedef QfSharePickerItemClicked
*

param sharingService Selected type of sharing.<br /> *

author Andrew Shapovelov*/
typedef void(

QfSharePickerItemClicked)(NSSharingService* sharingService);
/**
class QfSharePicker<br /> *
brief Object to show share menu.
*
author Andrew Shapovalov*/<br />
interface QfSharePicker : NSObject
/**
brief Create a new object.<br /> *
param view View on what will show share menu.
*
param frame Rect to show share window.<br /> *
param datas Datas items to share.
*
param block Block of code to select item.<br /> *
author Andrew Shapovalov*/
- (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 &quot;qfsharepicker.h&amp;quot;<br />#include &lt;QtCore&amp;gt;
<br />/**
brief The private methods and properties of QfSharePicker class.
*
author Andrew Shapovalov*/<br />
interface QfSharePicker () <NSSharingServicePickerDelegate, NSSharingServiceDelegate&gt;
/** Sharing service picker./
property (nonatomic, retain) NSSharingServicePicker''' picker;
<br />/** Block of code to select item.'''/<br />
property (nonatomic, copy) QfSharePickerItemClicked onItemClicked;

end

<br />
implementation QfSharePicker


#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&gt;)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&quot;:https://developer.apple.com/library/mac/DOCUMENTATION/AppKit/Reference/NSSharingServicePicker_Class/index.html. And you need to see information about class "NSSharingServicePickerDelegate&quot;: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&quot;.
# Change .h file content to code:
<br />#include &lt;QtCore&amp;gt;<br />#include &lt;QtQuick&amp;gt;
<br />/'''*
class QfShareItem
*
brief Manager to use share logic.<br /> *
author Andrew Shapovalov*/
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 /> *
param body The source string.
*
return Plain text.<br /> *
author Andrew Shapovalov*/
QString stripHTMLTags(QString body);

/**
brief Works with all applications events.<br /> *
param obj The object which call event.
*
param event The event of call.<br /> *
author Andrew Shapovalov*/
bool eventFilter(QObject* obj, QEvent* event);
/**
brief Share current content.<br /> *
author Andrew Shapovalov*/
void shareCurrentContent();
public:
/**
brief Create a new object.<br /> *
param parent Parent object.
*
author Andrew Shapovalov*/<br /> explicit QfShareItem(QQuickPaintedItem '''parent = 0);
<br /> //Others<br /> /'''*
brief Called when current object will redrawing.
*
param painter The object for draw data.<br /> *
author Andrew Shapovalov*/
void paint(QPainter painter);
//Getters
/*
brief Get share content string.<br /> *
return Share string content.
*
author Andrew Shapovalov*/<br /> inline QString getShareString(){return m_shareString;}
<br /> /**
brief Get share url.
*
return Share url.<br /> *
author Andrew Shapovalov*/
inline QUrl getShareUrl(){return m_shareUrl;}

//Setters
/**
brief Set share content string.<br /> *
param value Share string content.
*
author Andrew Shapovalov*/<br /> inline void setShareString(QString value){m_shareString = value; emit shareStringChanged(m_shareString);}
<br /> /**
brief Set share url.
*
param value Share url.<br /> *
author Andrew Shapovalov*/
inline void setShareUrl(QUrl value){m_shareUrl = value; emit shareUrlChanged(m_shareUrl);}
/**
brief Share content and url.<br /> *
param text The text of share.
*
param url The url to share.<br /> *
author Andrew Shapovalov*/
Q_INVOKABLE void shareContent(QString text = QString(), QUrl url = QUrl());
signals:
/**
brief Called when share content string was changed.<br /> *
param value Share string content.
*
author Andrew Shapovalov*/<br /> void shareStringChanged(QString value);
<br /> /**
brief Called when share url was changed.
*
param value Share url.<br /> *
author Andrew Shapovalov*/
void shareUrlChanged(QUrl value);
/**
brief Called when user select share service.<br /> *
param serviceName The name of selected service.
*
author Andrew Shapovalov*/<br /> void selectedService(QString serviceName);
<br /> public slots:<br />};<br />

# Change .mm file content to code:
<br />#include &quot;qfshareitem.h&amp;quot;<br />#include &quot;qfsharepicker.h&amp;quot;
<br />QString QfShareItem::stripHTMLTags(QString body)<br />{<br /> body.replace(&quot;&lt;br&amp;gt;&quot;,&quot;&quot;);<br /> body.replace(&quot;&lt;/br&amp;gt;&quot;,&quot;&quot;);<br /> body.replace(&quot;&lt;/p&amp;gt;&quot;,&quot;&quot;);<br /> body.replace(&quot;&lt;/td&amp;gt;&quot;,&quot;&quot;);<br /> body.remove(QRegExp(&quot;&amp;lt;head&amp;amp;gt;(.''')&amp;lt;/head&amp;amp;gt;&quot;));<br /> body.remove(QRegExp(&quot;&amp;lt;form(.''')&amp;lt;/form&amp;gt;&quot;));<br /> body.remove(QRegExp( &quot;&lt;(.)[</sup>&gt;]'''&gt;&quot;));
<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-&gt;parentItem();<br /> if(!m_shareString.isEmpty() &amp;&amp; parentItem)<br /> {<br /> QRectF rect = parentItem-&gt;mapRectToItem(NULL, parentItem-&gt;boundingRect());<br /> NSView* view = reinterpret_cast&amp;lt;NSView *&gt;(parentItem-&gt;window()<s>&gt;winId());<br /> NSRect frame = NSMakeRect(rect.x(), rect.y(), rect.width(), rect.height());
<br /> m_shareString = m_shareString.replace(&quot;&amp;lt;style type=quot;text/cssquot;&amp;gt;a {color:#44a51c;text-decoration:none;}&amp;lt;/style&amp;amp;gt;&quot;, &quot;&quot;);<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, &amp;QQuickPaintedItem::parentChanged, [this](QQuickItem''' newParent){
<br /> if(newParent)<br /> {<br /> newParent</s>&gt;setFiltersChildMouseEvents(true);<br /> }<br /> });<br /> this-&gt;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;amp; exception)<br /> {<br /> qDebug()&lt;&lt;&quot;exception: &quot;&lt;&lt;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&amp;lt;YourShareItem&amp;gt;(uri, 1, 1, &quot;YourShareItem&amp;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 />

Summary