QAbstractTableModel Class
QAbstractTableModel クラスは、テーブルモデルを作成するためにサブクラス化できる抽象モデルを提供します。詳細...
| ヘッダー: | #include <QAbstractTableModel> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
| 継承元: | QAbstractItemModel |
| 継承元: |
パブリック関数
| QAbstractTableModel(QObject *parent = nullptr) | |
| virtual | ~QAbstractTableModel() |
再実装されたパブリック関数
| virtual bool | dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override |
| virtual Qt::ItemFlags | flags(const QModelIndex &index) const override |
| virtual QModelIndex | index(int row, int column, const QModelIndex &parent = QModelIndex()) const override |
| virtual QModelIndex | sibling(int row, int column, const QModelIndex &idx) const override |
詳細な説明
QAbstractTableModel は、データを項目の 2 次元配列として表現するモデル向けの標準インターフェースを提供します。これは直接使用されるものではなく、サブクラス化する必要があります。
このモデルは、QAbstractItemModel よりもより特殊なインターフェースを提供するため、QListView にデータを提供するには使用できますが、ツリービューでの使用には適していません。単純な項目のリストを表現し、モデルに 1 列のデータのみを含める必要がある場合は、QAbstractListModel をサブクラス化するのがより適切かもしれません。
rowCount() およびcolumnCount() 関数は、テーブルの次元を返します。モデル内の項目に対応するモデルインデックスを取得するには、index() を使用し、行番号と列番号のみを指定します。
サブクラス化
QAbstractTableModel をサブクラス化する場合は、rowCount()、columnCount()、およびdata() を実装する必要があります。index() およびparent() 関数のデフォルトの実装は、QAbstractTableModel によって提供されます。適切に動作するモデルでは、headerData() も実装する必要があります。
編集可能なモデルは、setData() を実装する必要があり、またflags() を実装して、Qt::ItemIsEditable を含む値を返す必要があります。
サイズ変更可能なデータ構造へのインターフェースを提供するモデルは、insertRows()、removeRows()、insertColumns()、およびremoveColumns() の実装を提供できます。これらの関数を実装する際は、接続されたすべてのビューが変更を認識できるように、適切な関数を呼び出すことが重要です:
- insertRows() の実装では、データ構造に新しい行を挿入する前に beginInsertRows() を呼び出し、その直後に endInsertRows() を呼び出す必要があります。
- insertColumns() の実装では、データ構造に新しい列を挿入する前に beginInsertColumns() を呼び出し、その直後に endInsertColumns() を呼び出す必要があります。
- removeRows() の実装は、データ構造から行が削除される前に beginRemoveRows() を呼び出し、その直後に endRemoveRows() を呼び出さなければなりません。
- removeColumns() の実装では、データ構造から列が削除される前に beginRemoveColumns() を呼び出し、その直後に endRemoveColumns() を呼び出す必要があります。
注: モデルのサブクラス化に関する一般的なガイドラインについては 、『モデルサブクラス化リファレンス』を参照してください。
スレッドセーフ
QObject のサブクラスである QAbstractTableModel は、thread-safe ではありません。QAbstractTableModel に関連する API は、モデルオブジェクトが存在するスレッドからのみ呼び出す必要があります。QAbstractTableModel がビューに接続されている場合、ビューはそのスレッドに存在し、そのスレッドからモデルとやり取りを行うため、QAbstractTableModel は GUI スレッド上で動作します。 バックグラウンドスレッドを使用してモデルの内容を設定または変更することは可能ですが、モデル関連のAPIを直接呼び出すことはできないため、慎重に行う必要があります。代わりに、更新内容をキューに入れ、メインスレッドで適用する必要があります。これは、キュー付き接続を使用して行うことができます。
「Model Classes」、QAbstractItemModel 、QAbstractListModel 、およびQRangeModelも参照してください 。
メンバ関数のドキュメント
[explicit] QAbstractTableModel::QAbstractTableModel(QObject *parent = nullptr)
指定されたparent に対して、抽象テーブルモデルを構築します。
[virtual noexcept] QAbstractTableModel::~QAbstractTableModel()
抽象テーブルモデルを破棄します。
[override virtual] bool QAbstractTableModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
QAbstractItemModel::dropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)を再実装します。
[override virtual] Qt::ItemFlags QAbstractTableModel::flags(const QModelIndex &index) const
QAbstractItemModel::flags(const QModelIndex &index) const を再実装します。
[override virtual] QModelIndex QAbstractTableModel::index(int row, int column, const QModelIndex &parent = QModelIndex()) const
QAbstractItemModel::index(int row, int column, const QModelIndex &parent) const を再実装します。
parent を使用して、row およびcolumn 内のデータのインデックスを返します。
parent()も参照してください 。
[override virtual] QModelIndex QAbstractTableModel::sibling(int row, int column, const QModelIndex &idx) const
QAbstractItemModel::sibling (int row, int column, const QModelIndex &index)const を再実装します。
© 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.