このページでは

QItemDelegate Class

QItemDelegate クラスは、モデルからのデータ項目の表示および編集機能を提供します。詳細...

ヘッダー: #include <QItemDelegate>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
継承元: QAbstractItemDelegate

プロパティ

パブリック関数

QItemDelegate(QObject *parent = nullptr)
virtual ~QItemDelegate()
bool hasClipping() const
QItemEditorFactory *itemEditorFactory() const
void setClipping(bool clip)
void setItemEditorFactory(QItemEditorFactory *factory)

再実装されたパブリック関数

virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const override
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override

プロテクトされた関数

void drawBackground(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
virtual void drawCheck(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, Qt::CheckState state) const
virtual void drawDecoration(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QPixmap &pixmap) const
virtual void drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const
virtual void drawFocus(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect) const

再実装されたプロテクト関数

virtual bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override
virtual bool eventFilter(QObject *object, QEvent *event) override

詳細な説明

QItemDelegate を使用すると、QAbstractItemView のサブクラスに基づくアイテムビューに対して、カスタム表示機能やエディタウィジェットを提供できます。この目的でデリゲートを使用することで、モデルやビューとは独立して、表示および編集の仕組みをカスタマイズ・開発することが可能になります。

QItemDelegateクラスはモデル/ビュークラスの一つであり、Qtのモデル/ビューフレームワークの一部です。なお、QStyledItemDelegate がQtのアイテムビューの描画処理を引き継いでいる点に注意してください。新しいデリゲートを作成する際は、QStyledItemDelegate の使用を推奨します。

カスタムモデルからのアイテムを標準ビューに表示する場合、ビュー内のアイテムの外観を決定する各roles に対して、モデルが適切なデータを返すようにするだけで十分な場合がしばしばあります。 Qtの標準ビューで使用されるデフォルトのデリゲートは、この役割情報を利用して、ユーザーが期待する一般的な形式のほとんどでアイテムを表示します。しかし、デフォルトのデリゲートが提供できる範囲を超えて、アイテムの外観をさらに細かく制御する必要がある場合もあります。

このクラスは、ビュー内でのアイテムデータの描画およびアイテムモデルからのデータ編集を行う関数のデフォルト実装を提供します。QAbstractItemDelegate で定義されている仮想関数paint()およびsizeHint()のデフォルト実装は、デリゲートがビューから期待される正しい基本動作を確実に実装するために提供されています。サブクラスでこれらの関数を再実装することで、アイテムの外観をカスタマイズすることができます。

アイテムビューでデータを編集する際、QItemDelegate はエディタウィジェットを提供します。これは、編集が行われている間、ビューの上に配置されるウィジェットです。 エディタは `QItemEditorFactory` を使用して作成されます。QItemEditorFactory によって提供されるデフォルトの静的インスタンスは、すべてのアイテムデリゲートにインストールされます。setItemEditorFactory() を使用してカスタムファクトリを設定したり、QItemEditorFactory::setDefaultFactory() を使用して新しいデフォルトファクトリを設定したりできます。編集されるのは、Qt::EditRole を持つアイテムモデルに格納されたデータです。

ここでは、ウィジェットベースのデリゲート向けの標準的な編集関数のみが再実装されています:

  • createEditor() は、モデルからデータを変更するために使用されるウィジェットを返し、編集動作をカスタマイズするために再実装することができます。
  • setEditorData() は、ウィジェットに操作対象のデータを提供します。
  • updateEditorGeometry() は、アイテムビューに対してエディタが正しく表示されるようにします。
  • setModelData() は、更新されたデータをモデルに返します。

closeEditor() シグナルは、ユーザーがデータの編集を完了し、エディタウィジェットを破棄できることを示します。

標準ロールとデータ型

Qt に付属する標準ビューで使用されるデフォルトのデリゲートは、各標準ロール(Qt::ItemDataRole によって定義される)を特定のデータ型に関連付けます。これらの型でデータを返すモデルは、次の表に示すように、デリゲートの外観に影響を与えることができます。

デフォルトのデリゲートでは、表示やデータ編集の目的で必要なレベルのカスタマイズができない場合、QItemDelegate をサブクラス化して、目的の動作を実装することができます。

サブクラス化

QItemDelegate をサブクラス化して、カスタムレンダラーを使用してアイテムを表示するデリゲートを作成する場合、そのデリゲートが、選択済み、無効、チェック済みなど、必要なすべての状態に対して適切にアイテムをレンダリングできることを確認することが重要です。paint() 関数のドキュメントには、これを実現する方法に関するヒントがいくつか記載されています。

QItemEditorFactory を使用することで、カスタムエディタを提供できます。以下のコードは、デフォルトのアイテムエディタファクトリを使用して、デリゲートがカスタムエディタを利用できるようにする方法を示しています。

QItemEditorFactory *editorFactory = new QItemEditorFactory;
QItemEditorCreatorBase *creator = new QStandardItemEditorCreator<MyFancyDateTimeEdit>();
editorFactory->registerEditor(QMetaType::QDateTime, creator);

QItemEditorFactory::setDefaultFactory(editorFactory);

デフォルトのファクトリが設定されると、すべての標準的なアイテムデリゲートがそれを使用します(デフォルトのファクトリを設定する前に作成されたデリゲートも同様です)。

これにより、QItemDelegate をサブクラス化することを回避でき、指定された型(たとえばQMetaType::QDateTime )のすべての値は、提供されたエディタ(上記の例ではMyFancyDateTimeEdit )を使用して編集されるようになります。

別の方法として、createEditor()、setEditorData()、setModelData()、およびupdateEditorGeometry()を再実装することもできます。この手順については、「Model/Viewプログラミングの概要」のドキュメントで説明されています。

QStyledItemDelegate と QItemDelegate の違い

Qt 4.4 以降、QItemDelegate とQStyledItemDelegate の 2 つのデリゲートクラスが存在します。ただし、デフォルトのデリゲートはQStyledItemDelegate です。これら 2 つのクラスは、ビュー内のアイテムの描画およびエディタの提供を行うための、互いに独立した選択肢です。 両者の違いは、QStyledItemDelegate が現在のスタイルを使用してアイテムを描画する点にあります。したがって、カスタムデリゲートを実装する場合やQtスタイルシートを使用する場合は、基底クラスとしてQStyledItemDelegate を使用することを推奨します。カスタムデリゲートが描画にスタイルを使用する必要がない限り、どちらのクラスに必要なコードも同等であるはずです。

「デリゲートクラス」、QStyledItemDelegate 、およびQAbstractItemDelegateも参照してください 。

プロパティのドキュメント

clipping : bool

デリゲートがペイントイベントをクリップすべきかどうか

このプロパティを設定すると、ペイントクリップがアイテムのサイズに合わせて設定されます。デフォルト値は「on」です。画像がアイテムのサイズよりも大きい場合などに役立ちます。

アクセス関数:

bool hasClipping() const
void setClipping(bool clip)

メンバ関数のドキュメント

[explicit] QItemDelegate::QItemDelegate(QObject *parent = nullptr)

指定されたparent を使用して、アイテムデリゲートを構築します。

[virtual noexcept] QItemDelegate::~QItemDelegate()

アイテムデリゲートを破棄します。

[override virtual] QWidget *QItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const

QAbstractItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const を再実装します。

index で指定された項目の編集に使用されるウィジェットを返します。parent ウィジェットおよびスタイルoption を使用して、エディタウィジェットの表示方法を制御します。

QAbstractItemDelegate::createEditor()も参照してください 。

[protected] void QItemDelegate::drawBackground(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const

指定されたindex に対して、指定されたpainter およびスタイルoption を使用して、項目の背景を描画します。

[virtual protected] void QItemDelegate::drawCheck(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, Qt::CheckState state) const

rect で指定された矩形内に、指定されたpainter およびスタイルoption を使用し、指定されたstate を用いて、チェックマークを表示します。

[virtual protected] void QItemDelegate::drawDecoration(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QPixmap &pixmap) const

指定された `rect ` で囲まれた矩形内に、指定された `painter ` およびスタイル `option` を使用して、装飾 `pixmap ` を描画します。

[virtual protected] void QItemDelegate::drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const

指定された `rect ` で指定された矩形内に、指定された `painter ` およびスタイル `option` を使用して、アイテムビュー `text ` をレンダリングします。

[virtual protected] void QItemDelegate::drawFocus(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect) const

rect で指定された矩形内の領域を、指定されたpainter およびスタイルoption を使用して、その領域にフォーカスがあることを示すように描画します。

[override virtual protected] bool QItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)

QAbstractItemDelegate::editorEvent (QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)を再実装しています。

[override virtual protected] bool QItemDelegate::eventFilter(QObject *object, QEvent *event)

QObject::eventFilter(QObject *watched, QEvent *event) を再実装します。

詳細はQAbstractItemDelegate::handleEditorEvent() を参照してください。

QItemEditorFactory *QItemDelegate::itemEditorFactory() const

アイテムデリゲートで使用されるエディタファクトリを返します。エディタファクトリが設定されていない場合、この関数は null を返します。

setItemEditorFactory()も参照してください 。

[override virtual] void QItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const

QAbstractItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const を再実装します。

index で指定されたアイテムに対して、指定されたpainter およびスタイルoption を使用して、デリゲートをレンダリングします。

サブクラスでこの関数を再実装する場合、オプションの `rect ` 変数が保持する領域を更新し、オプションの `state ` 変数を使用して表示する項目の状態を判断し、それに応じて描画方法を調整する必要があります。

たとえば、次のコードに示すように、選択された項目は選択されていない項目とは異なる方法で表示する必要がある場合があります。

if (option.state & QStyle::State_Selected)
    painter->fillRect(option.rect, option.palette.highlight());

描画後は、この関数が呼び出された際に渡された状態へペインターを戻すようにしてください。たとえば、描画前に `QPainter::save()` を呼び出し、描画後に `QPainter::restore()` を呼び出すと便利です。

QStyle::Stateも参照してください 。

[override virtual] void QItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const

QAbstractItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const を再実装します。

モデル `index` で指定されたデータモデル項目から、editor によって表示および編集されるデータを設定します。

デフォルトの実装では、データはeditor ウィジェットのuser プロパティに格納されます。

QMetaProperty::isUser()も参照してください 。

void QItemDelegate::setItemEditorFactory(QItemEditorFactory *factory)

アイテムデリゲートが使用するエディタファクトリを、指定されたfactory に設定します。エディタファクトリが設定されていない場合、アイテムデリゲートはデフォルトのエディタファクトリを使用します。

itemEditorFactory()も参照してください 。

[override virtual] void QItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const

QAbstractItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const を再実装します。

editor ウィジェットからデータを取得し、指定されたmodel の項目index に格納します。

デフォルトの実装では、editor ウィジェットのuser プロパティから、データモデルに格納される値を取得します。

「QMetaProperty::isUser()」も参照してください 。

[override virtual] QSize QItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const

QAbstractItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const を再実装します。

index option で指定されたアイテムを表示するためにデリゲートが必要とするサイズを、 で提供されたスタイル情報を考慮して返します。

この関数を再実装する際、テキスト項目の場合、QItemDelegate はテキストの長さにマージン(すなわち 2 *QStyle::PM_FocusFrameHMargin )を追加することに注意してください。

[override virtual] void QItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const

QAbstractItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const を再実装します。

index で指定された項目のeditor を、指定されたスタイルoption に基づいて更新します。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.