12,964
回編集
311行目: | 311行目: | ||
ClipboardMonitorExample window; | ClipboardMonitorExample window; | ||
window.show(); | |||
return app.exec(); | |||
} | |||
</syntaxhighlight> | |||
<br><br> | |||
== MIMEデータ == | |||
以下の例では、<code>QMimeData</code>を使用して複数の形式のデータをクリップボードに設定および取得している。<br> | |||
これにより、異なるアプリケーション間でより豊富なデータ交換が可能になる。<br> | |||
<br> | |||
例えば、テキストエディタがリッチテキストとプレーンテキストの両方を提供する、または、画像エディタが画像データと関連するメタデータを同時に提供することができる。<br> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// MimeDataClipboardExample.hファイル | |||
#include <QApplication> | |||
#include <QClipboard> | |||
#include <QMainWindow> | |||
#include <QPushButton> | |||
#include <QVBoxLayout> | |||
#include <QLabel> | |||
#include <QMimeData> | |||
#include <QPixmap> | |||
#include <QBuffer> | |||
class MimeDataClipboardExample : public QMainWindow | |||
{ | |||
Q_OBJECT | |||
private: | |||
QLabel *statusLabel; | |||
public: | |||
MimeDataClipboardExample(QWidget *parent = nullptr) : QMainWindow(parent) | |||
{ | |||
setWindowTitle("MimeData Clipboard Example"); | |||
QVBoxLayout *layout = new QVBoxLayout; | |||
QPushButton *setDataButton = new QPushButton("Set Complex Data to Clipboard", this); | |||
QPushButton *getDataButton = new QPushButton("Get Data from Clipboard", this); | |||
statusLabel = new QLabel("Status: ", this); | |||
layout->addWidget(setDataButton); | |||
layout->addWidget(getDataButton); | |||
layout->addWidget(statusLabel); | |||
QWidget *centralWidget = new QWidget(this); | |||
centralWidget->setLayout(layout); | |||
setCentralWidget(centralWidget); | |||
connect(setDataButton, &QPushButton::clicked, this, &MimeDataClipboardExample::setComplexDataToClipboard); | |||
connect(getDataButton, &QPushButton::clicked, this, &MimeDataClipboardExample::getDataFromClipboard); | |||
} | |||
private slots: | |||
void setComplexDataToClipboard() | |||
{ | |||
// QMimeDataオブジェクトにより、異なる形式のデータを格納できるコンテナを作成 | |||
QMimeData *mimeData = new QMimeData(); | |||
// プレーンテキストデータの設定 | |||
mimeData->setText("Hello, this is some text data!"); | |||
// HTMLデータの設定 | |||
mimeData->setHtml("<h1>Hello</h1><p>This is some <b>HTML</b> data!</p>"); | |||
// アプリケーション固有のカスタムデータを設定 | |||
// MIMEタイプを指定することにより、他のアプリケーションとの互換性を管理 | |||
mimeData->setData("application/x-myapp", "Some custom data"); | |||
// 画像データの設定 | |||
QPixmap pixmap(100, 100); | |||
pixmap.fill(Qt::blue); | |||
QByteArray byteArray; | |||
QBuffer buffer(&byteArray); | |||
buffer.open(QIODevice::WriteOnly); | |||
pixmap.save(&buffer, "PNG"); | |||
mimeData->setData("image/png", byteArray); | |||
// クリップボードにデータを設定 | |||
QClipboard *clipboard = QApplication::clipboard(); | |||
clipboard->setMimeData(mimeData); | |||
statusLabel->setText("Status: Complex data set to clipboard"); | |||
} | |||
void getDataFromClipboard() | |||
{ | |||
// クリップボードからMIMEデータの取得 | |||
QClipboard *clipboard = QApplication::clipboard(); | |||
const QMimeData *mimeData = clipboard->mimeData(); | |||
QString result; | |||
// テキストデータの確認と取得 | |||
if (mimeData->hasText()) { | |||
result += "Text: " + mimeData->text() + "\n\n"; | |||
} | |||
// HTMLデータの確認と取得 | |||
if (mimeData->hasHtml()) { | |||
result += "HTML: " + mimeData->html() + "\n\n"; | |||
} | |||
// カスタムデータの確認と取得 | |||
if (mimeData->hasFormat("application/x-myapp")) { | |||
result += "Custom data: " + QString(mimeData->data("application/x-myapp")) + "\n\n"; | |||
} | |||
// 画像データの確認 | |||
if (mimeData->hasImage()) { | |||
result += "Image: Available\n"; | |||
} | |||
statusLabel->setText("Status: Data retrieved from clipboard\n\n" + result); | |||
} | |||
}; | |||
</syntaxhighlight> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// main.cppファイル | |||
#include "MimeDataClipboardExample.h" | |||
int main(int argc, char *argv[]) | |||
{ | |||
QApplication app(argc, argv); | |||
MimeDataClipboardExample window; | |||
window.show(); | window.show(); | ||