QItemSelectionModel Class
QItemSelectionModel クラスは、ビューで選択されている項目を追跡します。詳細...
| ヘッダー: | #include <QItemSelectionModel> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
| 継承元: | QObject |
パブリック型
| enum | SelectionFlag { NoUpdate, Clear, Select, Deselect, Toggle, …, ClearAndSelect } |
| flags | SelectionFlags |
プロパティ
- selectedIndexes : QModelIndexList
パブリック関数
| QItemSelectionModel(QAbstractItemModel *model = nullptr) | |
| QItemSelectionModel(QAbstractItemModel *model, QObject *parent) | |
| virtual | ~QItemSelectionModel() |
| bool | columnIntersectsSelection(int column, const QModelIndex &parent = QModelIndex()) const |
| QModelIndex | currentIndex() const |
| bool | hasSelection() const |
| bool | isColumnSelected(int column, const QModelIndex &parent = QModelIndex()) const |
| bool | isRowSelected(int row, const QModelIndex &parent = QModelIndex()) const |
| bool | isSelected(const QModelIndex &index) const |
| QAbstractItemModel * | model() |
| const QAbstractItemModel * | model() const |
| bool | rowIntersectsSelection(int row, const QModelIndex &parent = QModelIndex()) const |
| QModelIndexList | selectedColumns(int row = 0) const |
| QModelIndexList | selectedIndexes() const |
| QModelIndexList | selectedRows(int column = 0) const |
| const QItemSelection | selection() const |
| void | setModel(QAbstractItemModel *model) |
パブリックスロット
| virtual void | clear() |
| virtual void | clearCurrentIndex() |
| void | clearSelection() |
| virtual void | reset() |
| virtual void | select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command) |
| virtual void | select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) |
| virtual void | setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) |
シグナル
| void | currentChanged(const QModelIndex ¤t, const QModelIndex &previous) |
| void | currentColumnChanged(const QModelIndex ¤t, const QModelIndex &previous) |
| void | currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous) |
| void | modelChanged(QAbstractItemModel *model) |
| void | selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) |
保護された関数
| void | emitSelectionChanged(const QItemSelection &newSelection, const QItemSelection &oldSelection) |
詳細な説明
QItemSelectionModel は、1 つのビュー、あるいは同じモデルを参照する複数のビューで選択された項目を追跡します。また、ビュー内で現在選択されている項目も追跡します。
QItemSelectionModel クラスは、Model/View クラスの一つであり、Qtのモデル/ビューフレームワークの一部です。
選択された項目は、範囲を使用して格納されます。選択された項目を変更したい場合は、select() を使用し、QItemSelection 、またはQModelIndex とQItemSelectionModel::SelectionFlag のいずれかを指定してください。
QItemSelectionModelは、選択管理において2層のアプローチを採用しており、確定済みの選択項目と、現在の選択範囲に含まれる項目の両方を扱います。現在選択されている項目は、現在の対話型選択(例えば、ラバーバンド選択やキーボードのShiftキーを使った選択など)の一部です。
現在選択されている項目を更新するには、QItemSelectionModel::Current と、その他のSelectionFlags のいずれかをビット単位の OR 演算で組み合わせます。QItemSelectionModel::Current コマンドを省略すると、新しい現在の選択範囲が作成され、以前の選択範囲は全体的な選択範囲に追加されます。すべての関数は両方のレイヤーに対して動作します。たとえば、selecteditems() は両方のレイヤーの項目を返します。
注: バージョン 5.5以降 、model 、hasSelection 、およびcurrentIndex はメタオブジェクトのプロパティとなっています。
「Model/View プログラミング」および「QAbstractItemModel 」も参照してください 。
メンバ型のドキュメント
enum QItemSelectionModel::SelectionFlag
flags QItemSelectionModel::SelectionFlags
この列挙型は、選択モデルがどのように更新されるかを定義します。
| 定数 | 値 | 説明 |
|---|---|---|
QItemSelectionModel::NoUpdate | 0x0000 | 選択は行われません。 |
QItemSelectionModel::Clear | 0x0001 | 選択内容がすべてクリアされます。 |
QItemSelectionModel::Select | 0x0002 | 指定されたすべてのインデックスが選択されます。 |
QItemSelectionModel::Deselect | 0x0004 | 指定されたすべてのインデックスの選択が解除されます。 |
QItemSelectionModel::Toggle | 0x0008 | 指定されたすべてのインデックスは、現在の状態に応じて選択または選択解除されます。 |
QItemSelectionModel::Current | 0x0010 | 現在の選択が更新されます。 |
QItemSelectionModel::Rows | 0x0020 | すべてのインデックスが、行にまたがるように展開されます。 |
QItemSelectionModel::Columns | 0x0040 | すべてのインデックスが列にまたがるように拡張されます。 |
QItemSelectionModel::SelectCurrent | Select | Current | 利便性のために、「選択」と「現在」の組み合わせが用意されています。 |
QItemSelectionModel::ToggleCurrent | Toggle | Current | 利便性のために、「Toggle」と「Current」の組み合わせが用意されています。 |
QItemSelectionModel::ClearAndSelect | Clear | Select | 利便性のために、「Clear」と「Select」の組み合わせが用意されています。 |
SelectionFlags 型は、QFlags<SelectionFlag> の typedef です。SelectionFlag 値の OR 組み合わせを格納します。
プロパティのドキュメント
[read-only] selectedIndexes : QModelIndexList
アクセス機能:
| QModelIndexList | selectedIndexes() const |
通知信号:
| void | selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) |
メンバ関数のドキュメント
[explicit] QItemSelectionModel::QItemSelectionModel(QAbstractItemModel *model = nullptr)
指定された項目model に対して動作する選択モデルを構築します。
[explicit] QItemSelectionModel::QItemSelectionModel(QAbstractItemModel *model, QObject *parent)
指定された項目 `model ` に対して、parent を用いて動作する選択モデルを構築します。
[virtual noexcept] QItemSelectionModel::~QItemSelectionModel()
選択モデルを破棄します。
[virtual slot] void QItemSelectionModel::clear()
選択モデルをクリアします。selectionChanged() およびcurrentChanged() を発行します。
[virtual slot] void QItemSelectionModel::clearCurrentIndex()
現在のインデックスをクリアします。currentChanged() を発行します。
[slot] void QItemSelectionModel::clearSelection()
選択モデル内の選択内容をクリアします。selectionChanged() を発行します。
[invokable] bool QItemSelectionModel::columnIntersectsSelection(int column, const QModelIndex &parent = QModelIndex()) const
指定されたparent を持つアイテムがcolumn 内で選択されている場合、true を返します。
注: Qt 5.15以降 、parent のデフォルト引数は空のモデルインデックスです。
注:この関数は 、メタオブジェクトシステムを介して、また QML から呼び出すことができます。Q_INVOKABLE を参照してください。
[signal] void QItemSelectionModel::currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
このシグナルは、現在のアイテムが変更されるたびに発火します。previous モデルのアイテムインデックスが、current のインデックスに置き換えられ、選択項目の現在のアイテムとなります。
なお、アイテムモデルがリセットされた場合、このシグナルは発火しません。
currentIndex()、setCurrentIndex()、およびselectionChanged()も参照してください 。
[signal] void QItemSelectionModel::currentColumnChanged(const QModelIndex ¤t, const QModelIndex &previous)
このシグナルは、current アイテムが変更され、その列がprevious の現在のアイテムの列と異なる場合に発火します。
なお、アイテムモデルがリセットされた場合は、このシグナルは発火しません。
currentChanged()、currentRowChanged()、currentIndex()、およびsetCurrentIndex()も参照してください 。
QModelIndex QItemSelectionModel::currentIndex() const
現在の項目のモデル項目のインデックスを返します。現在の項目がない場合は、無効なインデックスを返します。
setCurrentIndex()も参照してください 。
[signal] void QItemSelectionModel::currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous)
current アイテムが変更され、その行がprevious の現在のアイテムの行と異なる場合、このシグナルが発信されます。
なお、アイテムモデルがリセットされた場合、このシグナルは発火しません。
currentChanged()、currentColumnChanged()、currentIndex() およびsetCurrentIndex()も参照してください 。
[protected] void QItemSelectionModel::emitSelectionChanged(const QItemSelection &newSelection, const QItemSelection &oldSelection)
newSelection とoldSelection の 2 つの選択内容を比較し、選択解除された項目と選択された項目を指定してselectionChanged() を出力します。
bool QItemSelectionModel::hasSelection() const
選択モデルに選択された項目が含まれている場合は `true ` を返し、そうでない場合は `false` を返します。
[invokable] bool QItemSelectionModel::isColumnSelected(int column, const QModelIndex &parent = QModelIndex()) const
指定されたparent を持つcolumn 内のすべての項目が選択されている場合、true を返します。
なお、この関数は通常、同じ列内のすべての項目に対してisSelected() を呼び出すよりも高速であり、選択不可能な項目は無視されることに注意してください。
注: Qt 5.15以降 、parent のデフォルト引数は空のモデルインデックスです。
注:この関数は 、メタオブジェクトシステムや QML から呼び出すことができます。Q_INVOKABLE を参照してください。
[invokable] bool QItemSelectionModel::isRowSelected(int row, const QModelIndex &parent = QModelIndex()) const
指定されたparent を持つrow 内のすべての項目が選択されている場合、true を返します。
なお、この関数は、同じ行内のすべての項目に対して `isSelected()` を呼び出すよりも通常高速であり、選択不可能な項目は無視される点に注意してください。
注: Qt 5.15以降 、parent のデフォルト引数は空のモデルインデックスとなっています。
注:この関数は 、メタオブジェクトシステムを介して、また QML から呼び出すことができます。Q_INVOKABLE を参照してください。
[invokable] bool QItemSelectionModel::isSelected(const QModelIndex &index) const
指定されたモデル項目 `index ` が選択されている場合、true を返します。
注:この関数は 、メタオブジェクトシステムおよび QML から呼び出すことができます。Q_INVOKABLE を参照してください。
QAbstractItemModel *QItemSelectionModel::model()
選択モデルによって操作されたアイテムモデルを返します。
setModel()も参照してください 。
const QAbstractItemModel *QItemSelectionModel::model() const
選択モデルによって操作されたアイテムモデルを返します。
[signal] void QItemSelectionModel::modelChanged(QAbstractItemModel *model)
このシグナルは、`setModel()` を使用して `model ` の設定に成功した際に発生します。
model() およびsetModel()も参照してください 。
[virtual slot] void QItemSelectionModel::reset()
選択モデルをクリアします。信号は一切出力されません。
[invokable] bool QItemSelectionModel::rowIntersectsSelection(int row, const QModelIndex &parent = QModelIndex()) const
指定されたparent を持つアイテムがrow 内で選択されている場合、true を返します。
注: Qt 5.15以降 、parent のデフォルト引数は空のモデルインデックスです。
注:この関数は 、メタオブジェクトシステムを介して、および QML から呼び出すことができます。Q_INVOKABLE を参照してください。
[virtual slot] void QItemSelectionModel::select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command)
指定されたcommand を使用して、selection という項目を選択し、selectionChanged()を出力します。
注:この スロットは オーバーロードされています。このスロットに接続するには:
// Connect using qOverload:
connect(sender, &SenderClass::signal,
itemSelectionModel, qOverload(&QItemSelectionModel::select));
// Or using a lambda as wrapper:
connect(sender, &SenderClass::signal,
itemSelectionModel, [receiver = itemSelectionModel](const QItemSelection &selection, QItemSelectionModel::SelectionFlags command) { receiver->select(selection, command); }); 「QItemSelectionModel::SelectionFlag」も参照してください 。
[virtual slot] void QItemSelectionModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
指定されたcommand を使用してモデルアイテムindex を選択し、selectionChanged()を出力します。
注:この スロットは オーバーロードされています。このスロットに接続するには:
// Connect using qOverload:
connect(sender, &SenderClass::signal,
itemSelectionModel, qOverload(&QItemSelectionModel::select));
// Or using a lambda as wrapper:
connect(sender, &SenderClass::signal,
itemSelectionModel, [receiver = itemSelectionModel](const QModelIndex &index, QItemSelectionModel::SelectionFlags command) { receiver->select(index, command); }); 「QItemSelectionModel::SelectionFlags」も参照してください 。
[invokable] QModelIndexList QItemSelectionModel::selectedColumns(int row = 0) const
指定されたrow 内で、すべての行が選択されている列のインデックスを返します。
注:この関数は 、メタオブジェクトシステムやQMLから呼び出すことができます。Q_INVOKABLE を参照してください。
関連項目: ` selectedIndexes()` および `selectedRows()`。
QModelIndexList QItemSelectionModel::selectedIndexes() const
選択されたすべてのモデル項目のインデックスのリストを返します。このリストには重複は含まれず、ソートもされていません。
注: プロパティ `selectedIndexes`のゲッター関数です 。
[invokable] QModelIndexList QItemSelectionModel::selectedRows(int column = 0) const
指定されたcolumn において、すべての列が選択されている行のインデックスを返します。
注:この関数は 、メタオブジェクトシステムやQMLから呼び出すことができます。Q_INVOKABLE を参照してください。
selectedIndexes() およびselectedColumns()も参照してください 。
const QItemSelection QItemSelectionModel::selection() const
選択モデルに保存されている選択範囲を返します。
[signal] void QItemSelectionModel::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
このシグナルは、選択内容が変更されるたびに発火します。選択内容の変更は、deselected のアイテムの選択と、selected のアイテムの選択として表されます。
なお、現在のインデックスは選択状態とは独立して変化することに注意してください。また、アイテムモデルがリセットされた場合、このシグナルは発火しない点にも注意してください。
選択されたままですがインデックスが変更された項目は、selected およびdeselected には含まれません。したがって、選択された項目のインデックスのみが変更された場合、selected およびdeselected の両方が空の状態でも、このシグナルが発火する可能性があります。
注: このシグナルに直接接続されたスロット内から、モデルを(例:setData() の呼び出しによって)変更することは 許可されていません。 このシグナルは、行や列の削除、またはモデルのリセット中など、モデルが変更されている最中に発火する可能性があります。 このようなタイミングで追加の変更を試みると、未定義の挙動を引き起こす可能性があります。特に、ネストされた変更は、QSortFilterProxyModel によって管理されるマッピング構造などの内部状態を破損させる恐れがあります。
注: プロパティ `selectedIndexes` に対する通知 シグナルです。
select() およびcurrentChanged()も参照してください 。
[virtual slot] void QItemSelectionModel::setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
モデル項目index を現在の項目に設定し、currentChanged()を発行します。現在の項目は、キーボードによるナビゲーションやフォーカス表示に使用されます。これは選択された項目とは独立していますが、選択された項目が現在の項目となる場合もあります。
指定されたcommand によっては、index が現在の選択範囲の一部となることもあります。
currentIndex() およびselect()も参照してください 。
void QItemSelectionModel::setModel(QAbstractItemModel *model)
モデルをmodel に設定します。modelChanged()シグナルが発信されます。
model() およびmodelChanged()も参照してください 。
© 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.