📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
| 292行目: | 292行目: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br><br> | <br><br> | ||
== QBitmapクラスの使用例 == | |||
==== 描画ツール ==== | |||
以下の例では、1ビット深度の描画ツールクラスを定義している。<br> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// SimpleDrawingTool.hファイル | |||
#include <QWidget> | |||
#include <QBitmap> | |||
#include <QPainter> | |||
#include <QMouseEvent> | |||
#include <QPushButton> | |||
#include <QVBoxLayout> | |||
#include <QLabel> | |||
#include <QMessageBox> | |||
class SimpleDraingTool : public QWidget | |||
{ | |||
Q_OBJECT | |||
private: | |||
QLabel *drawingLabel; // 描画エリア用ラベル | |||
QPushButton *clearButton; // クリアボタン | |||
QBitmap bitmap; // 描画用ビットマップ | |||
bool drawing; // 描画中フラグ | |||
// 描画用ビットマップを初期化する関数 | |||
void initializeBitmap() | |||
{ | |||
bitmap = QBitmap(300, 300); | |||
bitmap.fill(Qt::color0); // 背景を黒で初期化 | |||
updateLabel(); | |||
} | |||
// 指定された位置に点を描画する関数 | |||
void drawPoint(const QPoint &pos) | |||
{ | |||
QPainter painter(&bitmap); | |||
painter.setPen(Qt::color1); // 白で描画 | |||
painter.drawPoint(pos); | |||
updateLabel(); | |||
} | |||
// ラベルの表示を更新する関数 | |||
void updateLabel() | |||
{ | |||
drawingLabel->setPixmap(QPixmap::fromImage(bitmap.toImage())); | |||
} | |||
public: | |||
// コンストラクタ: ウィジェットの初期化 | |||
SimpleDraingTool(QWidget *parent = nullptr) : QWidget(parent), drawing(false) | |||
{ | |||
// 描画エリア用ラベル | |||
drawingLabel = new QLabel(this); | |||
drawingLabel->setAlignment(Qt::AlignCenter); | |||
// クリアボタン | |||
clearButton = new QPushButton("クリア", this); | |||
connect(clearButton, &QPushButton::clicked, this, &SimpleDraingTool::clearDrawing); | |||
// レイアウトの設定 | |||
QVBoxLayout *layout = new QVBoxLayout(this); | |||
layout->addWidget(drawingLabel); | |||
layout->addWidget(clearButton); | |||
setLayout(layout); | |||
// 描画用ビットマップの初期化 | |||
initializeBitmap(); | |||
} | |||
protected: | |||
// マウスボタンが押下された時のイベントハンドラ | |||
void mousePressEvent(QMouseEvent *event) override | |||
{ | |||
if (event->button() == Qt::LeftButton) { | |||
drawing = true; | |||
drawPoint(event->pos()); | |||
} | |||
} | |||
// マウスが移動した時のイベントハンドラ | |||
void mouseMoveEvent(QMouseEvent *event) override | |||
{ | |||
if ((event->buttons() & Qt::LeftButton) && drawing) { | |||
drawPoint(event->pos()); | |||
} | |||
} | |||
// マウスボタンが離された時のイベントハンドラ | |||
void mouseReleaseEvent(QMouseEvent *event) override | |||
{ | |||
if (event->button() == Qt::LeftButton && drawing) { | |||
drawing = false; | |||
} | |||
} | |||
private slots: | |||
// 描画をクリアするスロット | |||
void clearDrawing() | |||
{ | |||
try { | |||
initializeBitmap(); | |||
updateLabel(); | |||
} | |||
catch (const std::exception& e) { | |||
QMessageBox::critical(this, "エラー", QString("描画のクリアに失敗: %1").arg(e.what())); | |||
} | |||
} | |||
}; | |||
</syntaxhighlight> | |||
<br> | |||
==== カスタムカーソル作成器 ==== | |||
以下の例では、カスタムカーソルを作成および適用するウィジェットクラスを定義している。<br> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// CustomCursorCreator.hファイル | |||
#include <QWidget> | |||
#include <QBitmap> | |||
#include <QPainter> | |||
#include <QPushButton> | |||
#include <QVBoxLayout> | |||
#include <QCursor> | |||
#include <QMessageBox> | |||
class CustomCursorCreator : public QWidget | |||
{ | |||
Q_OBJECT | |||
private: | |||
QPushButton *createButton; // カーソル作成ボタン | |||
public: | |||
// コンストラクタ: ウィジェットの初期化 | |||
CustomCursorCreator(QWidget *parent = nullptr) : QWidget(parent) | |||
{ | |||
// カーソル作成ボタン | |||
createButton = new QPushButton("カスタムカーソルを作成", this); | |||
connect(createButton, &QPushButton::clicked, this, &CustomCursorCreator::createAndApplyCursor); | |||
// レイアウトの設定 | |||
QVBoxLayout *layout = new QVBoxLayout(this); | |||
layout->addWidget(createButton); | |||
setLayout(layout); | |||
} | |||
private slots: | |||
// カスタムカーソルを作成・適用するスロット | |||
void createAndApplyCursor() | |||
{ | |||
try { | |||
// 32x32ピクセルのビットマップを作成 | |||
QBitmap bitmap(32, 32); | |||
bitmap.fill(Qt::color0); // 透明で初期化 | |||
// ビットマップ上に描画 | |||
QPainter painter(&bitmap); | |||
painter.setPen(Qt::color1); // 不透明な色で描画 | |||
// 簡単な十字型のカーソルを描画 | |||
painter.drawLine(16, 0, 16, 31); // 縦線 | |||
painter.drawLine(0, 16, 31, 16); // 横線 | |||
// マスクを作成 (カーソルの形状を定義) | |||
QBitmap mask = bitmap; | |||
// カーソルを作成・適用 | |||
QCursor cursor(bitmap, mask, 16, 16); // ホットスポットを中心に設定 | |||
this->setCursor(cursor); | |||
QMessageBox::information(this, "成功", "カスタムカーソルを作成・適用"); | |||
} | |||
catch (const std::exception& e) { | |||
QMessageBox::critical(this, "エラー", QString("カーソルの作成に失敗: %1").arg(e.what())); | |||
} | |||
} | |||
}; | |||
</syntaxhighlight> | |||
== QGraphicsViewクラスを使用する == | == QGraphicsViewクラスを使用する == | ||