qt5-入门-剪贴板-简单例子

时间:2024-04-03 18:55:08
#ifndef CLIPBOARDDEMO_H #define CLIPBOARDDEMO_H #include <QtGui> #include <QWidget> #include <QVBoxLayout> #include <QHBoxLayout> #include <QPushButton> #include <QTextEdit> #include <QLabel> #include <QApplication> #include <QMessageBox> class ClipboardDemo : public QWidget { Q_OBJECT public: ClipboardDemo(QWidget *parent = 0):QWidget(parent) { QVBoxLayout *mainLayout = new QVBoxLayout(this); QHBoxLayout *northLayout = new QHBoxLayout; QHBoxLayout *southLayout = new QHBoxLayout; 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())); } private slots: void setClipboard() { QClipboard *board = QApplication::clipboard(); //board->setText("Text from Qt Application"); board->setText(editor->toPlainText()); } void getClipboard() { QClipboard *board = QApplication::clipboard(); QString str = board->text(); QMessageBox::information(NULL, "From clipboard", str); } private: QTextEdit *editor; }; #endif // CLIPBOARDDEMO_H // main // ClipboardDemo w; // w.show();