Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Providing a copy-paste utility between two Qt applications using clipboard

From Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): All (S60)
Compatibility
Platform(s): S60 3rd Edition, FP1
S60 3rd Edition, FP2
S60 5th Edition
Symbian
S60 5th Edition
S60 3rd Edition FP2
S60 3rd Edition FP1
Article
Keywords: QClipboard
Created: User:Kbwiki (14 Dec 2009)
Last edited: hamishwillee (11 Oct 2012)

Overview

This article describes how to provide a copy-paste operation between two Qt applications.

Description

QClipboard can be used to get access to the system clipboard. Every application has access to the global clipboard object with QApplication::clipboard().

Solution

Lets assume there are two Qt applications:

  • QClipBoardCopyEx      Copies some data to the global clipboard
  • QClipBoardPasteEx    Retrieves data from the global clipboard


Support for copy-paste operations between these applications can be implemented as follows:

Headers

 #include <QClipboard>

QClipBoardCopyEx implementation

 class QClipBoardCopyEx : public QWidget
{
...
QClipboard* m_clipBoard;
};

QClipBoardCopyEx implementation

 QClipBoardCopyEx::QClipBoardCopyEx( QWidget *parent )
: QWidget( parent )
{
// Retrieve pointer to the global clipboard
m_clipBoard = QApplication::clipboard();
QString copyTxt( "From another app!" );
 
// Copy text to clipboard
m_clipBoard->setText( copyTxt );
...
}

QClipBoardPasteEx implementation

 class QClipBoardPasteEx : public QWidget
{
...
QLabel* m_label;
QClipboard* m_clipBoard;
};

Pasting from the global clipboard

 QClipBoardPasteEx::QClipBoardPasteEx(QWidget *parent)
: QWidget(parent)
{
...
// Retrieve a pointer to the global clipboard
m_clipBoard = QApplication::clipboard();
 
// Retrieve text from the global clipboard
QString pasteTxt = m_clipBoard->text();
m_label = new QLabel( this );
 
// Show the text in a label
m_label->setText( pasteTxt );
m_label->setGeometry( QApplication::desktop()->availableGeometry() );
m_label->show();
}

Notes

To get notifications of the changes to clipboard contents, connect the QClipboard::dataChanged() signal to an appropriate slot in the application.

This page was last modified on 11 October 2012, at 01:18.
66 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×