Clipboard operations are often used together with the previously mentioned drag-and-drop technology. Therefore, let's talk about clipboard operations first.
Everyone is familiar with clipboard. We can simply think of it as a data storage pool, where we can place external data or retrieve the data. The clipboard is maintained by the operating system, so this provides a way to interact data across applications. Qt has encapsulated a lot of clipboard operations for us, so we can easily implement them in our own applications. Start with the code:
Clipboarddemo. h
- #ifndef CLIPBOARDDEMO_H
- #define CLIPBOARDDEMO_H
-
- #include <QtGui/QWidget>
-
- class ClipboardDemo : public QWidget
- {
- Q_OBJECT
-
- public:
- ClipboardDemo(QWidget *parent = 0);
-
- private slots:
- void setClipboard();
- void getClipboard();
- };
-
- #endif // CLIPBOARDDEMO_H
Clipboarddemo. cpp
- #include <QtGui>
- #include "clipboarddemo.h"
-
- ClipboardDemo::ClipboardDemo(QWidget *parent)
- : QWidget(parent)
- {
- QVBoxLayout *mainLayout = new QVBoxLayout(this);
- QHBoxLayout *northLayout = new QHBoxLayout;
- QHBoxLayout *southLayout = new QHBoxLayout;
-
- QTextEdit *editor = new QTextEdit;
- QLabel *label = new QLabel;
- label->setText("Text Input: ");
- label->setBuddy(editor);
- QPushButton *copyButton = new QPushButton;
- copyButton->setText("Set Clipboard");
- QPushButton *pasteButton = new QPushButton;
- pasteButton->setText("Get Clipboard");
-
- northLayout->addWidget(label);
- northLayout->addWidget(editor);
- southLayout->addWidget(copyButton);
- southLayout->addWidget(pasteButton);
- mainLayout->addLayout(northLayout);
- mainLayout->addLayout(southLayout);
-
- connect(copyButton, SIGNAL(clicked()), this, SLOT(setClipboard()));
- connect(pasteButton, SIGNAL(clicked()), this, SLOT(getClipboard()));
- }
-
- void ClipboardDemo::setClipboard()
- {
- QClipboard *board = QApplication::clipboard();
- board->setText("Text from Qt Application");
- }
-
- void ClipboardDemo::getClipboard()
- {
- QClipboard *board = QApplication::clipboard();
- QString str = board->text();
- QMessageBox::information(NULL, "From clipboard", str);
- }
Main. cpp
- #include "clipboarddemo.h"
-
- #include <QtGui>
- #include <QApplication>
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- ClipboardDemo w;
- w.show();
- return a.exec();
- }
The main () function is very simple, that is, to display our ClipboardDemo class. Let's focus on the code in ClipboardDemo.
The constructor does not have any complicated content. We need to put a label. One textedit and two buttons are placed in the window. These codes can be easily written and connected to the signal slot.
- void ClipboardDemo::setClipboard()
- {
- QClipboard *board = QApplication::clipboard();
- board->setText("Text from Qt Application");
- }
-
- void ClipboardDemo::getClipboard()
- {
- QClipboard *board = QApplication::clipboard();
- QString str = board->text();
- QMessageBox::information(NULL, "From clipboard", str);
- }
In the slot function, we use the QApplication: clipboard () function to access the system clipboard. The returned value of this function is the QClipboard pointer. We can see from the API of this class that the data can be placed in the Clipboard through the setText (), setImage () or setPixmap () function, this is usually referred to as the clipboard or copy operation. Using the text (), image () or pixmap () function, you can obtain data from the clipboard, that is, paste.
In addition, the preceding example shows that QTextEdit supports shortcut keys such as Ctrl + C and Ctrl + V by default. In addition, many Qt components provide convenient operations, so we need to obtain specific information from the document to avoid re-inventing the wheel.
QClipboard provides few data types. If needed, we can inherit the QMimeData class and call the setMimeData () function to enable the clipboard to support our own data types.
In the X11 system, you can click the middle mouse button (usually the scroll wheel) to support the clipboard operation. To implement this function, we need to pass the QClipboard: text () parameter to the QClipboard: Selection function. For example, we can handle the following in the release event:
- void MyTextEditor::mouseReleaseEvent(QMouseEvent *event)
- {
- QClipboard *clipboard = QApplication::clipboard();
- if (event->button() == Qt::MidButton
- && clipboard->supportsSelection()) {
- QString text = clipboard->text(QClipboard::Selection);
- pasteText(text);
- }
- }
SupportsSelection () returns true on the X11 platform, and false on other platforms.
In addition, the QClipboard provides a dataChanged () signal to listen to Clipboard data changes.
This article is from the "bean space" blog, please be sure to keep this source http://devbean.blog.51cto.com/448512/292229