×
Namespaces

Variants
Actions

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

From Nokia Developer 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 04:18.
86 page views in the last 30 days.
×