Clipboard operations using Qt
The Qt clipboard offers a simple mechanism to copy and paste data between different applications or between the same widget in am application. QClipboard supports the same data types as QDrag, and uses similar mechanisms.
Article Metadata
Tested with
Devices(s): S60 for Emulator
Compatibility
Platform(s): S60 3rd Edition, FP1, FP2
S60 5th Edition
S60 5th Edition
Article
Keywords: QClipboard
Created: mind_freak
(09 Jul 2009)
Last edited: hamishwillee
(11 Oct 2012)
Contents |
Source code
Note: This code is used only to copy the text content, code for MIME data is shown later in this section.
Header File
#ifndef BOARD_H
#define BOARD_H
#include <QtGui/QWidget>
#include<QClipboard>
#include<QString>
#include<QLineEdit>
#include<QPushButton>
#include<QVBoxLayout>
#include<QApplication>
#include<QMessageBox>
class board : public QWidget
{
Q_OBJECT
public:
board(QWidget *parent = 0);
~board();
private slots:
void copy();
void paste();
private:
QLineEdit *edit;
QPushButton *but;
QPushButton *but1;
QLineEdit *edit1;
QVBoxLayout *lay;
QClipboard *cb;
QString str;
QString str1;
QMessageBox *box;
};
#endif // BOARD_H
Source File
#include "board.h"
board::board(QWidget *parent)
: QWidget(parent)
{
edit=new QLineEdit();
box=new QMessageBox(this);
but=new QPushButton(tr("Copy"),this);
but1=new QPushButton(tr("Paste"),this);
edit1=new QLineEdit(this);
lay=new QVBoxLayout(this);
cb=QApplication::clipboard();
setStyleSheet("* { background-color:rgb(0,0,0);color:rgb(255,255,255); padding: 7px}}");
QObject::connect(but,SIGNAL(clicked()),this,SLOT(copy()));
QObject::connect(but1,SIGNAL(clicked()),this,SLOT(paste()));
lay->addWidget(edit);
lay->addWidget(but);
lay->addWidget(but1);
lay->addWidget(edit1);
setLayout(lay);
showMaximized();
}
board::~board()
{
// No need to delete any object that got a parent that is properly deleted.
}
void board::copy()
{
str=edit->text();
cb->setText(str);//Text copied to clipboard
box->setText("Text copied to clipboard");
box->exec();
}
void board::paste()
{
str1=cb->text();//text reterived from clipboard
edit1->setText(str1);
}
Advance clipboard(MIME data manipulation)
- Copying of MIME data
void board::copy()
{
QApplication::clipboard()->setData(dragObject());
}
- Pasting of MIME data
void MyTable::paste()
{
QMimeSource *source = QApplication::clipboard()->data();
if (CellDrag::canDecode(source)) {
QString str;
CellDrag::decode(source, str);
performPaste(str);
}
}

