12,964
回編集
(→概要) |
|||
35行目: | 35行目: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br><br> | <br><br> | ||
== テキストのクリップボード操作 == | |||
以下の例では、テキストのクリップボード操作において、ユーザはテキストを入力して、クリップボードに設定およびクリップボードから取得している。<br> | |||
<syntaxhighlight lang="c++"> | |||
// TextClipboardExample.hファイル | |||
#include <QApplication> | |||
#include <QClipboard> | |||
#include <QMainWindow> | |||
#include <QPushButton> | |||
#include <QVBoxLayout> | |||
#include <QLabel> | |||
#include <QLineEdit> | |||
class TextClipboardExample : public QMainWindow | |||
{ | |||
Q_OBJECT | |||
private: | |||
QLineEdit *textInput; | |||
QLabel *statusLabel; | |||
public: | |||
TextClipboardExample(QWidget *parent = nullptr) : QMainWindow(parent) | |||
{ | |||
setWindowTitle("Text Clipboard Example"); | |||
QVBoxLayout *layout = new QVBoxLayout; | |||
textInput = new QLineEdit(this); | |||
QPushButton *setTextButton = new QPushButton("Set Text to Clipboard", this); | |||
QPushButton *getTextButton = new QPushButton("Get Text from Clipboard", this); | |||
statusLabel = new QLabel("Status: ", this); | |||
layout->addWidget(textInput); | |||
layout->addWidget(setTextButton); | |||
layout->addWidget(getTextButton); | |||
layout->addWidget(statusLabel); | |||
QWidget *centralWidget = new QWidget(this); | |||
centralWidget->setLayout(layout); | |||
setCentralWidget(centralWidget); | |||
connect(setTextButton, &QPushButton::clicked, this, &TextClipboardExample::setTextToClipboard); | |||
connect(getTextButton, &QPushButton::clicked, this, &TextClipboardExample::getTextFromClipboard); | |||
} | |||
private slots: | |||
void setTextToClipboard() | |||
{ | |||
QClipboard *clipboard = QApplication::clipboard(); | |||
QString text = textInput->text(); | |||
// クリップボードに設定 | |||
clipboard->setText(text); | |||
statusLabel->setText("Status: Text set to clipboard: " + text); | |||
} | |||
void getTextFromClipboard() | |||
{ | |||
QClipboard *clipboard = QApplication::clipboard(); | |||
// クリップボードの内容を取得 | |||
QString text = clipboard->text(); | |||
textInput->setText(text); | |||
statusLabel->setText("Status: Text retrieved from clipboard: " + text); | |||
} | |||
}; | |||
</syntaxhighlight> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// main.cppファイル | |||
#include "TextClipboardExample.h" | |||
int main(int argc, char *argv[]) | |||
{ | |||
QApplication app(argc, argv); | |||
TextClipboardExample window; | |||
window.show(); | |||
return app.exec(); | |||
} | |||
</syntaxhighlight> | |||
<br><br> | |||
== 画像のクリップボード操作 == | |||
以下の例では、画像のクリップボード操作において、画像をクリップボードに設定およびクリップボードから画像を取得している。<br> | |||
これは、画像操作とクリップボードとの間でのデータ転送を示している。<br> | |||
<syntaxhighlight lang="c++"> | |||
// ImageClipboardExample.hファイル | |||
#include <QApplication> | |||
#include <QClipboard> | |||
#include <QMainWindow> | |||
#include <QPushButton> | |||
#include <QVBoxLayout> | |||
#include <QLabel> | |||
#include <QPixmap> | |||
class ImageClipboardExample : public QMainWindow | |||
{ | |||
Q_OBJECT | |||
private: | |||
QLabel *statusLabel; | |||
QLabel *imageLabel; | |||
public: | |||
ImageClipboardExample(QWidget *parent = nullptr) : QMainWindow(parent) | |||
{ | |||
setWindowTitle("Image Clipboard Example"); | |||
QVBoxLayout *layout = new QVBoxLayout; | |||
QPushButton *setImageButton = new QPushButton("Set Image to Clipboard", this); | |||
QPushButton *getImageButton = new QPushButton("Get Image from Clipboard", this); | |||
statusLabel = new QLabel("Status: ", this); | |||
imageLabel = new QLabel(this); | |||
imageLabel->setFixedSize(200, 200); | |||
imageLabel->setScaledContents(true); | |||
layout->addWidget(setImageButton); | |||
layout->addWidget(getImageButton); | |||
layout->addWidget(statusLabel); | |||
layout->addWidget(imageLabel); | |||
QWidget *centralWidget = new QWidget(this); | |||
centralWidget->setLayout(layout); | |||
setCentralWidget(centralWidget); | |||
connect(setImageButton, &QPushButton::clicked, this, &ImageClipboardExample::setImageToClipboard); | |||
connect(getImageButton, &QPushButton::clicked, this, &ImageClipboardExample::getImageFromClipboard); | |||
} | |||
private slots: | |||
void setImageToClipboard() | |||
{ | |||
QPixmap pixmap(100, 100); | |||
pixmap.fill(Qt::red); | |||
QClipboard *clipboard = QApplication::clipboard(); | |||
// 画像をクリップボードに設定 | |||
clipboard->setPixmap(pixmap); | |||
imageLabel->setPixmap(pixmap); | |||
statusLabel->setText("Status: Image set to clipboard"); | |||
} | |||
void getImageFromClipboard() | |||
{ | |||
QClipboard *clipboard = QApplication::clipboard(); | |||
// クリップボードから画像を取得 | |||
QPixmap pixmap = clipboard->pixmap(); | |||
if (!pixmap.isNull()) { | |||
// 取得した画像が有効な場合 | |||
imageLabel->setPixmap(pixmap); | |||
statusLabel->setText("Status: Image retrieved from clipboard"); | |||
} | |||
else { | |||
// 取得した画像が無効な場合 | |||
statusLabel->setText("Status: No image in clipboard"); | |||
} | |||
} | |||
}; | |||
</syntaxhighlight> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// main.cppファイル | |||
#include "ImageClipboardExample.h" | |||
int main(int argc, char *argv[]) | |||
{ | |||
QApplication app(argc, argv); | |||
ImageClipboardExample window; | |||
window.show(); | |||
return app.exec(); | |||
} | |||
</syntaxhighlight> | |||
<br><br> | |||
__FORCETOC__ | __FORCETOC__ | ||
[[カテゴリ:Qt]] | [[カテゴリ:Qt]] |