Clipboard operations using Qt
Jump to navigation
Jump to search
Source code[edit | edit source]
Note: This code is used only to copy the text content, code for MIME data is shown later in this section.
Header File[edit | edit source]
- 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[edit | edit source]
- 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)[edit | edit source]
- 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);
}
}