Clipboard operations using Qt

From Qt Wiki
Jump to navigation Jump to search

Template:Abstract

Template:ArticleMetaData

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]

  1. ifndef BOARD_H
  2. define BOARD_H
  1. include <QtGui/QWidget>
  2. include<QClipboard>
  3. include<QString>
  4. include<QLineEdit>
  5. include<QPushButton>
  6. include<QVBoxLayout>
  7. include<QApplication>
  8. 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;


};

  1. endif // BOARD_H

Source File[edit | edit source]

  1. 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);
   }

}

Screenshots[edit | edit source]

File:Clipboard1.JPG

File:Clipboard2.JPG