文字列「__FORCETOC__」を「{{#seo: |title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki |keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 |description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This pag…
 
(同じ利用者による、間の3版が非表示)
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>
== クリップボード監視 ==
以下の例では、クリップボードの変更を監視して、クリップボードの内容が変更されるごとにリアルタイムで通知を受け取り、その内容を表示している。<br>
<br>
これは、クリップボードの監視や、クリップボードを介したアプリケーション間の通信を実装する場合に役立つ。<br>
<br>
テキストクリップボード、画像クリップボード、クリップボード監視をを組み合わせることにより、複雑なクリップボード関連の機能を実装することができる。<br>
例えば、複数の形式のデータを同時に扱ったり、カスタムデータ型をクリップボードで使用したりすることが可能である。<br>
<br>
<syntaxhighlight lang="c++">
// ClipboardMonitorExample.hファイル
#include <QApplication>
#include <QClipboard>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QLabel>
#include <QTextEdit>
class ClipboardMonitorExample : public QMainWindow
{
    Q_OBJECT
private:
    QLabel *statusLabel;
    QTextEdit *contentLabel;
public:
    ClipboardMonitorExample(QWidget *parent = nullptr) : QMainWindow(parent)
    {
      setWindowTitle("Clipboard Monitor Example");
      QVBoxLayout *layout = new QVBoxLayout;
      statusLabel = new QLabel("Clipboard Status: Monitoring", this);
      contentLabel = new QTextEdit(this);
      contentLabel->setReadOnly(true);
      layout->addWidget(statusLabel);
      layout->addWidget(contentLabel);
      QWidget *centralWidget = new QWidget(this);
      centralWidget->setLayout(layout);
      setCentralWidget(centralWidget);
      // クリップボードの監視設定
      QClipboard *clipboard = QApplication::clipboard();
      connect(clipboard, &QClipboard::dataChanged, this, &ClipboardMonitorExample::onClipboardChanged);
    }
private slots:
    // クリップボードからテキストと画像の両方を取得
    // テキストがある場合はそれを表示、画像がある場合はその寸法を表示
    // 取得した情報をQTextEditコントロールに表示して、ステータスラベルを更新
    void onClipboardChanged()
    {
      QClipboard *clipboard = QApplication::clipboard();
      QString text = clipboard->text();
      QPixmap pixmap = clipboard->pixmap();
     
      QString content;
      if (!text.isEmpty()) {
          content = "Text: " + text;
      }
      else if (!pixmap.isNull()) {
          content = "Image: " + QString::number(pixmap.width()) + "x" + QString::number(pixmap.height());
      }
      else {
          content = "Unknown content";
      }
      statusLabel->setText("Clipboard Status: Content changed");
      contentLabel->setText(content);
    }
};
</syntaxhighlight>
<br>
<syntaxhighlight lang="c++">
// main.cppファイル
#include "ClipboardMonitorExample.h"
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    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();
    return app.exec();
}
</syntaxhighlight>
<br><br>
{{#seo:
|title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki
|keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板
|description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This page is {{PAGENAME}} in our wiki about electronic circuits and SUSE Linux
|image=/resources/assets/MochiuLogo_Single_Blue.png
}}


__FORCETOC__
__FORCETOC__
[[カテゴリ:Qt]]
[[カテゴリ:Qt]]