このページでは

QHeaderView Class

QHeaderView クラスは、アイテムビューに対してヘッダー行またはヘッダー列を提供します。詳細...

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

パブリック型

enum ResizeMode { Interactive, Fixed, Stretch, ResizeToContents, Custom }

プロパティ

パブリック関数

QHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr)
virtual ~QHeaderView()
bool cascadingSectionResizes() const
int count() const
Qt::Alignment defaultAlignment() const
int defaultSectionSize() const
int hiddenSectionCount() const
void hideSection(int logicalIndex)
bool highlightSections() const
bool isFirstSectionMovable() const
bool isSectionHidden(int logicalIndex) const
bool isSortIndicatorClearable() const
bool isSortIndicatorShown() const
int length() const
int logicalIndex(int visualIndex) const
int logicalIndexAt(const QPoint &pos) const
int logicalIndexAt(int position) const
int logicalIndexAt(int x, int y) const
int maximumSectionSize() const
int minimumSectionSize() const
void moveSection(int from, int to)
int offset() const
Qt::Orientation orientation() const
void resetDefaultSectionSize()
int resizeContentsPrecision() const
void resizeSection(int logicalIndex, int size)
void resizeSections(QHeaderView::ResizeMode mode)
bool restoreState(const QByteArray &state)
QByteArray saveState() const
int sectionPosition(int logicalIndex) const
QHeaderView::ResizeMode sectionResizeMode(int logicalIndex) const
int sectionSize(int logicalIndex) const
int sectionSizeHint(int logicalIndex) const
int sectionViewportPosition(int logicalIndex) const
bool sectionsClickable() const
bool sectionsHidden() const
bool sectionsMovable() const
bool sectionsMoved() const
void setCascadingSectionResizes(bool enable)
void setDefaultAlignment(Qt::Alignment alignment)
void setDefaultSectionSize(int size)
void setFirstSectionMovable(bool movable)
void setHighlightSections(bool highlight)
void setMaximumSectionSize(int size)
void setMinimumSectionSize(int size)
void setResizeContentsPrecision(int precision)
void setSectionHidden(int logicalIndex, bool hide)
void setSectionResizeMode(QHeaderView::ResizeMode mode)
void setSectionResizeMode(int logicalIndex, QHeaderView::ResizeMode mode)
void setSectionsClickable(bool clickable)
void setSectionsMovable(bool movable)
void setSortIndicator(int logicalIndex, Qt::SortOrder order)
void setSortIndicatorClearable(bool clearable)
void setSortIndicatorShown(bool show)
void setStretchLastSection(bool stretch)
void showSection(int logicalIndex)
Qt::SortOrder sortIndicatorOrder() const
int sortIndicatorSection() const
bool stretchLastSection() const
int stretchSectionCount() const
void swapSections(int first, int second)
int visualIndex(int logicalIndex) const
int visualIndexAt(int position) const

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

virtual void reset() override
virtual void setModel(QAbstractItemModel *model) override
virtual void setVisible(bool v) override
virtual QSize sizeHint() const override

パブリックスロット

void headerDataChanged(Qt::Orientation orientation, int logicalFirst, int logicalLast)
void setOffset(int offset)
void setOffsetToLastSection()
void setOffsetToSectionPosition(int visualSectionNumber)

シグナル

void geometriesChanged()
void sectionClicked(int logicalIndex)
void sectionCountChanged(int oldCount, int newCount)
void sectionDoubleClicked(int logicalIndex)
void sectionEntered(int logicalIndex)
void sectionHandleDoubleClicked(int logicalIndex)
void sectionMoved(int logicalIndex, int oldVisualIndex, int newVisualIndex)
void sectionPressed(int logicalIndex)
void sectionResized(int logicalIndex, int oldSize, int newSize)
void sortIndicatorChanged(int logicalIndex, Qt::SortOrder order)
void sortIndicatorClearableChanged(bool clearable)

protected関数

virtual void initStyleOption(QStyleOptionHeader *option) const
(since 6.0) virtual void initStyleOptionForIndex(QStyleOptionHeader *option, int logicalIndex) const
virtual void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
virtual QSize sectionSizeFromContents(int logicalIndex) const

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

virtual void currentChanged(const QModelIndex &current, const QModelIndex &old) override
virtual bool event(QEvent *e) override
virtual int horizontalOffset() const override
virtual void mouseDoubleClickEvent(QMouseEvent *e) override
virtual void mouseMoveEvent(QMouseEvent *e) override
virtual void mousePressEvent(QMouseEvent *e) override
virtual void mouseReleaseEvent(QMouseEvent *e) override
virtual void paintEvent(QPaintEvent *e) override
virtual void setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags flags) override
virtual int verticalOffset() const override
virtual bool viewportEvent(QEvent *e) override

再実装されたプロテクトされたスロット

void resizeSections()
void sectionsAboutToBeRemoved(const QModelIndex &parent, int logicalFirst, int logicalLast)
void sectionsInserted(const QModelIndex &parent, int logicalFirst, int logicalLast)

詳細な説明

QHeaderViewは、QTableView やQTreeView といったアイテムビューで使用されるヘッダーを表示します。これは、以前は同じ目的で使用されていたQt3のQHeader クラスに代わるものですが、アイテムビュークラスとの整合性を保つため、Qtのモデル/ビューアーキテクチャを採用しています。

QHeaderViewクラスはモデル/ビュークラスの一つであり、Qtのモデル/ビューフレームワークの一部を構成しています。

ヘッダーは、QAbstractItemModel::headerData() 関数を使用して、モデルから各セクションのデータを取得します。データはQAbstractItemModel::setHeaderData() を使用して設定できます。

各ヘッダーには、orientation() 関数と、count() 関数によって指定される複数のセクションがあります。セクションとはヘッダーの一部を指し、向きに応じて行または列となります。

セクションは、moveSection() およびresizeSection() を使用して移動やサイズ変更が可能であり、hideSection() およびshowSection() を使用して非表示や表示を切り替えることもできます。

ヘッダーの各セクションは、section() で指定されるセクション ID によって識別され、ヘッダー内の特定のvisualIndex() に配置することができます。セクションには、setSortIndicator() を使用してソートインジケータを設定することができ、これにより、関連するアイテムビュー内のアイテムが、そのセクションで指定された順序でソートされるかどうかが示されます。

水平ヘッダーの場合、セクションはモデル内の列に相当し、垂直ヘッダーの場合、セクションはモデル内の行に相当します。

ヘッダーセクションの移動

ヘッダーは、setSectionsMovable() を使用して固定することも、移動可能にすることもできます。setSectionsClickable() を使用してクリック可能にすることができ、setSectionResizeMode() に基づいてサイズ変更の挙動が設定されます。

注: ヘッダーをダブルクリックして セクションのサイズを変更できるのは、表示されている行にのみ適用されます。

ヘッダーは、ユーザーがセクションを移動した際にsectionMoved()を、セクションのサイズを変更した際にsectionResized()を、またマウスクリックに対してはsectionClicked()およびsectionHandleDoubleClicked()を発火します。さらに、ヘッダーはsectionCountChanged()も発火します。

セクションの識別には、logicalIndex() およびlogicalIndexAt() 関数を使用するか、visualIndex() およびvisualIndexAt() 関数を使用してインデックス位置を参照します。セクションが移動されると視覚的なインデックスは変更されますが、論理的なインデックスは変更されません。

外観

QTableWidget および `QTableView ` は、デフォルトのヘッダーを作成します。ヘッダーを表示させたい場合は、`setVisible()` を使用できます。

すべてのItemDataRoleがQHeaderViewに影響を与えるわけではありません。他のロールを描画する必要がある場合は、QHeaderViewをサブクラス化し、paintEvent()を再実装できます。QHeaderViewは、スタイルと競合しない限り(デスクトップテーマに従うスタイルでは競合が発生する可能性があります)、以下の項目データロールを尊重します:

TextAlignmentRole、DisplayRole 、FontRole 、DecorationRole 、ForegroundRole 、およびBackgroundRole 。

注:各ヘッダーは 、各セクションのデータを独自にレンダリングしており、デリゲートに依存していません。そのため、ヘッダーのsetItemDelegate()関数を呼び出しても何の効果もありません。

大規模なモデルに関する特別な考慮事項

headerviewはセクションごとに8~16バイトのメモリを使用します。ただし、Qt 6.9以降、このセクション用メモリは、1つ以上のセクションのサイズ変更や順序変更が行われた場合にのみ使用されます。 つまり、swapSections 、resizeSection 、hideSection 、moveSection 、およびstretchLastSection (これを有効にするもの)が呼び出されない限り、モデルに数百万のセクションがあっても、QHeaderViewがそれに比例した(したがって膨大な)量のメモリを消費することはありません。 ユーザーの操作によるこうした呼び出しを防ぐためには、resize mode をFixed に設定する必要があります(特定のインデックスに対して指定すると逆の効果となるため、インデックス単位での指定は避けてください)。また、sectionsMovable を無効にしておくことで、ユーザーによるセクションの移動も防止する必要があります。

セキュリティ上の考慮事項

restoreState() 関数は、列の順序、セクションのサイズ、ソートインジケータ、および関連するビューの状態を記述する、バージョン管理されたバイナリブロブを逆シリアル化します。 フォーマットのマジックナンバー、バージョン、および報告された合計長さは検証されますが、デシリアル化された視覚インデックスおよび論理インデックス、ならびにソートインジケータのセクションは、内部データ構造のインデックス付けに使用される前に、ヘッダーの実際のセクション数に対してチェックされることはありません。

restoreState() には、saveState() によって事前に生成され、同一または互換性のあるバージョンのアプリケーションによって永続化された(通常はQSettings を通じて往復処理された)QByteArray のみを渡してください。 ネットワークからダウンロードしたファイル、同期または共有された設定ファイル、あるいは侵害されている可能性のある別のアプリケーションから提供されたデータなど、出所が不明または信頼できないデータに対して、restoreState() を呼び出さないでください。

関連項目: Model/Viewプログラミング、QListView 、QTableView 、QTreeView 、saveState()、およびrestoreState()。

会員種別に関するドキュメント

enum QHeaderView::ResizeMode

リサイズモードは、ヘッダーセクションの動作を指定します。これは、ヘッダービュー全体に対して設定することも、setSectionResizeMode() を使用して個々のセクションに対して設定することもできます。

定数値説明
QHeaderView::Interactive0ユーザーはセクションのサイズを変更できます。また、resizeSection() を使用してプログラムからセクションのサイズを変更することもできます。セクションのサイズは、デフォルトでdefaultSectionSize になります。(cascadingSectionResizes も参照してください。)
QHeaderView::Fixed2ユーザーはセクションのサイズを変更できません。セクションのサイズ変更は、resizeSection() を使用してプログラム的に行うことしかできません。セクションのデフォルトサイズはdefaultSectionSize です。
QHeaderView::Stretch1QHeaderView は、利用可能なスペースを埋めるようにセクションのサイズを自動的に調整します。このサイズは、ユーザーやプログラムによって変更することはできません。
QHeaderView::ResizeToContents3QHeaderView は、列または行全体のコンテンツに基づいて、セクションを最適なサイズに自動的に調整します。このサイズは、ユーザーやプログラムによって変更することはできません。(この値は 4.2 で導入されました)

以下の値は非推奨です:

定数値説明
QHeaderView::CustomFixed代わりに「Fixed」を使用してください。

関連項目: ` setSectionResizeMode()`、`stretchLastSection`、および `minimumSectionSize`。

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

cascadingSectionResizes : bool

このプロパティは、ユーザーがサイズ変更を行っているセクションが最小サイズに達した時点で、インタラクティブなサイズ変更が後続のセクションに波及するかどうかを指定します

このプロパティは、リサイズモードとしてInteractive が設定されているセクションにのみ影響します。

デフォルト値は false です。

アクセス関数:

bool cascadingSectionResizes() const
void setCascadingSectionResizes(bool enable)

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

defaultAlignment : Qt::Alignment

このプロパティは、各ヘッダーセクション内のテキストのデフォルトの配置を指定します

アクセス関数:

Qt::Alignment defaultAlignment() const
void setDefaultAlignment(Qt::Alignment alignment)

defaultSectionSize : int

このプロパティは、サイズ変更前のヘッダーセクションのデフォルトサイズを保持します。

このプロパティは、リサイズモードとして `Interactive ` または `Fixed ` が設定されているセクションにのみ影響します。

デフォルトでは、このプロパティの値はスタイルに依存します。したがって、スタイルが変更されると、このプロパティはその変更に基づいて更新されます。setDefaultSectionSize() を呼び出すと更新が停止し、resetDefaultSectionSize() を呼び出すとデフォルトの動作が復元されます。

アクセス関数:

int defaultSectionSize() const
void setDefaultSectionSize(int size)
void resetDefaultSectionSize()

setSectionResizeMode() およびminimumSectionSizeも参照してください 。

firstSectionMovable : bool

このプロパティは、ユーザーが最初の列を移動できるかどうかを指定します

このプロパティは、ユーザーが最初の列を移動できるかどうかを制御します。QTreeView では、最初の列はツリー構造を保持しているため、setSectionsMovable (true)に設定された後でも、デフォルトでは移動できません。

たとえば、ツリー構造を持たないフラットなリストの場合など、このメソッドを呼び出すことで、最初の列を再び移動可能にすることができます。そのようなシナリオでは、QTreeView::setRootIsDecorated(false)も併せて呼び出すことを推奨します。

treeView->setRootIsDecorated(false);
treeView->header()->setFirstSectionMovable(true);

setSectionsMovable (true)も同時に呼び出さない限り、これをtrueに設定しても効果はありません。

アクセス関数:

bool isFirstSectionMovable() const
void setFirstSectionMovable(bool movable)

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

highlightSections : bool

このプロパティは、選択された項目を含むセクションがハイライト表示されるかどうかを指定します

デフォルトでは、このプロパティの値はfalse です。

Accessの関数:

bool highlightSections() const
void setHighlightSections(bool highlight)

maximumSectionSize : int

このプロパティは、ヘッダーセクションの最大サイズを指定します。

最大セクションサイズは、許可される最大のセクションサイズです。このプロパティのデフォルト値は 1048575 であり、これはセクションの最大許容サイズでもあります。maximum を -1 に設定すると、値は最大のセクションサイズにリセットされます。

stretch を除き、このプロパティはすべてのresize modes

Access関数でサポートされています:

int maximumSectionSize() const
void setMaximumSectionSize(int size)

関連項目: setSectionResizeMode() およびdefaultSectionSize 。

minimumSectionSize : int

このプロパティは、ヘッダーセクションの最小サイズを指定します。

最小セクションサイズは、許可される最小のセクションサイズです。最小セクションサイズが -1 に設定されている場合、QHeaderView はfont metrics のサイズを使用します。

このプロパティは、すべてのresize modes で有効です。

アクセス関数:

int minimumSectionSize() const
void setMinimumSectionSize(int size)

関連項目: ` setSectionResizeMode()` および `defaultSectionSize`。

sectionsClickable : bool

ヘッダーがクリック可能な場合は `true ` を、そうでない場合は `false` を返します。クリック可能なヘッダーを設定することで、ユーザーがヘッダーに関連するビュー内のデータの表示方法を変更できるようにすることができます。

アクセス関数:

bool sectionsClickable() const
void setSectionsClickable(bool clickable)

sectionPressed() およびsetSortIndicatorShown()も参照してください 。

sectionsMovable : bool

sectionsMovable が true の場合、ヘッダーセクションはユーザーによって移動可能ですが、そうでない場合は固定されます。

QTreeView と組み合わせて使用する場合、デフォルトでは最初の列は(ツリー構造が含まれているため)移動できません。setFirstSectionMovable (true)を設定することで、この列を移動可能にすることができます。

アクセス関数:

bool sectionsMovable() const
void setSectionsMovable(bool movable)

sectionMoved() およびsetFirstSectionMovable()も参照してください 。

showSortIndicator : bool

このプロパティは、ソートインジケーターが表示されるかどうかを指定します

デフォルトでは、このプロパティは「false 」に設定されています。

アクセス関数:

bool isSortIndicatorShown() const
void setSortIndicatorShown(bool show)

「setSectionsClickable()」も参照してください 。

[since 6.1] sortIndicatorClearable : bool

このプロパティは、セクションを複数回クリックすることで並べ替えインジケーターを解除できるかどうかを指定します

このプロパティは、ユーザーが特定のセクションを複数回クリックすることで、そのセクションのソートインジケーターを解除できるかどうかを制御します。通常、セクションをクリックすると、そのセクションのソート順が変更されるだけです。このプロパティを true に設定すると、昇順と降順を交互に切り替えた後にソートインジケーターがクリアされ、これにより通常はモデルの元のソート順が復元されます。

このプロパティを true に設定しても、sectionsClickable() が true でない限り効果はありません(これは、QTableView などの特定のビューではデフォルト値であり、QTreeView::setSortingEnabled を呼び出すなどしてビューをソート可能にした際に自動的に設定されます)。

この列挙型は Qt 6.1 で導入されました。

アクセス関数:

bool isSortIndicatorClearable() const
void setSortIndicatorClearable(bool clearable)

Notifierシグナル:

void sortIndicatorClearableChanged(bool clearable)

stretchLastSection : bool

このプロパティは、ヘッダー内の最後に表示されるセクションが利用可能なスペースをすべて占めるかどうかを指定します

デフォルト値は false です。

注: QTreeView が提供する水平ヘッダーは 、このプロパティが true に設定された状態で構成されており、ビューがヘッダー用に割り当てられたスペースを一切無駄にしないよう保証されています。この値が true に設定されている場合、このプロパティはヘッダー内の最後のセクションに設定されたリサイズモードを上書きします。

アクセス関数:

bool stretchLastSection() const
void setStretchLastSection(bool stretch)

関連項目 :setSectionResizeMode()

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

[explicit] QHeaderView::QHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr)

指定されたorientation およびparent を用いて、新しい汎用ヘッダーを作成します。

[virtual noexcept] QHeaderView::~QHeaderView()

ヘッダーを削除します。

int QHeaderView::count() const

ヘッダー内のセクション数を返します。

sectionCountChanged() およびlength()も参照してください 。

[override virtual protected] void QHeaderView::currentChanged(const QModelIndex &current, const QModelIndex &old)

QAbstractItemView::currentChanged (const QModelIndex &current, const QModelIndex &previous)を再実装します。

[override virtual protected] bool QHeaderView::event(QEvent *e)

QAbstractItemView::event(QEvent *event) を再実装します。

[signal] void QHeaderView::geometriesChanged()

このシグナルは、ヘッダーのジオメトリが変更されたときに発信されます。

[slot] void QHeaderView::headerDataChanged(Qt::Orientation orientation, int logicalFirst, int logicalLast)

指定されたorientation (logicalFirst からlogicalLast までを含む)を使用して、変更されたヘッダーセクションを更新します。

int QHeaderView::hiddenSectionCount() const

ヘッダー内で非表示にされているセクションの数を返します。

setSectionHidden() およびisSectionHidden()も参照してください 。

void QHeaderView::hideSection(int logicalIndex)

logicalIndex で指定されたセクションを非表示にします。

showSection()、isSectionHidden()、hiddenSectionCount()、およびsetSectionHidden()も参照してください 。

[override virtual protected] int QHeaderView::horizontalOffset() const

QAbstractItemView::horizontalOffset() const を再実装します。

ヘッダーの水平方向のオフセットを返します。垂直方向のヘッダーの場合は 0 になります。

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

[virtual protected] void QHeaderView::initStyleOption(QStyleOptionHeader *option) const

option を、QHeaderView の値を用いて初期化します。この方法は、サブクラスがQStyleOptionHeader が必要であるものの、すべての情報を自分で入力したくない場合に役立ちます。

QStyleOption::initFrom() およびinitStyleOptionForIndex()も参照してください 。

[virtual protected, since 6.0] void QHeaderView::initStyleOptionForIndex(QStyleOptionHeader *option, int logicalIndex) const

指定されたlogicalIndex から、スタイルoption を初期化します。この関数は、initStyleOption が呼び出された後、paintSection のデフォルト実装によって呼び出されます。

この関数は Qt 6.0 で導入されました。

paintSection() およびinitStyleOption()も参照してください 。

bool QHeaderView::isSectionHidden(int logicalIndex) const

logicalIndex で指定されたセクションがユーザーに対して明示的に非表示にされている場合はtrue を返し、そうでない場合はfalse を返します。

hideSection()、showSection()、setSectionHidden()、およびhiddenSectionCount()も参照してください 。

int QHeaderView::length() const

ヘッダーの向きに沿った長さを返します。

sizeHint()、setSectionResizeMode()、およびoffset()も参照してください 。

int QHeaderView::logicalIndex(int visualIndex) const

指定されたvisualIndex の位置にあるセクションのlogicalIndexを返します。visualIndex が0未満の場合、またはvisualIndex がQHeaderView::count()以上である場合は-1を返します。

なお、visualIndex は非表示のセクションの影響を受けないことに注意してください。

visualIndex() およびsectionPosition()も参照してください 。

int QHeaderView::logicalIndexAt(const QPoint &pos) const

pos で指定された位置にあるセクションの論理インデックスを返します。ヘッダーが水平方向にある場合は x 座標が、それ以外の場合は y 座標が、論理インデックスの特定に使用されます。

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

int QHeaderView::logicalIndexAt(int position) const

ビューポート内で、指定されたposition をカバーするセクションを返します。

visualIndexAt() およびisSectionHidden()も参照してください 。

int QHeaderView::logicalIndexAt(int x, int y) const

指定された座標にあるセクションの論理インデックスを返します。ヘッダーが水平方向の場合は `x ` が使用され、それ以外の場合は `y ` を使用して論理インデックスが求められます。

[override virtual protected] void QHeaderView::mouseDoubleClickEvent(QMouseEvent *e)

QAbstractItemView::mouseDoubleClickEvent(QMouseEvent *event) を再実装します。

[override virtual protected] void QHeaderView::mouseMoveEvent(QMouseEvent *e)

QAbstractItemView::mouseMoveEvent(QMouseEvent *event) を再実装します。

[override virtual protected] void QHeaderView::mousePressEvent(QMouseEvent *e)

QAbstractItemView::mousePressEvent(QMouseEvent *event) を再実装します。

[override virtual protected] void QHeaderView::mouseReleaseEvent(QMouseEvent *e)

QAbstractItemView::mouseReleaseEvent(QMouseEvent *event) を再実装します。

void QHeaderView::moveSection(int from, int to)

視覚インデックスfrom にあるセクションを、視覚インデックスto に移動させます。

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

int QHeaderView::offset() const

ヘッダーのオフセットを返します。これは、ヘッダーの左端(縦方向のヘッダーの場合は最上部)にある、画面上に表示されているピクセルです。

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

Qt::Orientation QHeaderView::orientation() const

ヘッダーの向きを返します。

Qt::Orientationも参照してください 。

[override virtual protected] void QHeaderView::paintEvent(QPaintEvent *e)

QAbstractScrollArea::paintEvent(QPaintEvent *event) を再実装します。

[virtual protected] void QHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const

指定されたlogicalIndex で指定されたセクションを、指定されたpainter およびrect を使用して描画します。

通常、この関数を呼び出す必要はありません。

[override virtual] void QHeaderView::reset()

QAbstractItemView::reset() を再実装します。

int QHeaderView::resizeContentsPrecision() const

`QHeaderView ` が `ResizeToContents` に対してどの程度の精度で計算を行うかを返します。

setResizeContentsPrecision() およびsetSectionResizeMode()も参照してください 。

void QHeaderView::resizeSection(int logicalIndex, int size)

logicalIndex で指定されたセクションのサイズを、size ピクセル単位で変更します。size パラメータの値は 0 以上でなければなりません。ただし、size を 0 に設定することは推奨されません。その場合は、代わりにhideSection を使用してください。

sectionResized()、sectionSize()、およびhideSection()も参照してください 。

[protected slot] void QHeaderView::resizeSections()

サイズヒントに基づいてセクションのサイズを変更します。通常、この関数を呼び出す必要はありません。

注:この スロットは オーバーロードされています。このスロットに接続するには:

// Connect using qOverload:
connect(sender, &SenderClass::signal,
        headerView, qOverload<>(&QHeaderView::resizeSections));

// Or using a lambda as wrapper:
connect(sender, &SenderClass::signal,
        headerView, [receiver = headerView]() { receiver->resizeSections(); });
その他の例や手法については、「オーバーロードされたスロットへの接続」を参照してください。

void QHeaderView::resizeSections(QHeaderView::ResizeMode mode)

現在のリサイズモードを無視し、指定されたmode に従ってセクションのサイズを変更します。

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

bool QHeaderView::restoreState(const QByteArray &state)

このヘッダービューのstate を復元します。この関数は、状態が復元された場合はtrue を返し、そうでない場合はfalseを返します。

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

QByteArray QHeaderView::saveState() const

このヘッダービューの現在の状態を保存します。

保存した状態を復元するには、戻り値をrestoreState()に渡してください。

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

[signal] void QHeaderView::sectionClicked(int logicalIndex)

このシグナルは、セクションがクリックされたときに発火します。セクションの論理インデックスは、logicalIndex で指定されます。

なお、sectionPressed シグナルも同様に発火することに注意してください。

setSectionsClickable() およびsectionPressed()も参照してください 。

[signal] void QHeaderView::sectionCountChanged(int oldCount, int newCount)

このシグナルは、セクションの数に変化があった場合、つまりセクションが追加または削除された場合に発行されます。元のセクション数は `oldCount` で指定され、新しいセクション数は `newCount` で指定されます。

count()、length()、およびheaderDataChanged()も参照してください 。

[signal] void QHeaderView::sectionDoubleClicked(int logicalIndex)

このシグナルは、セクションがダブルクリックされたときに発生します。セクションの論理インデックスは、logicalIndex で指定されます。

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

[signal] void QHeaderView::sectionEntered(int logicalIndex)

このシグナルは、カーソルがセクション上を移動し、マウスの左ボタンが押されたときに発生します。セクションの論理インデックスは、logicalIndex で指定されます。

setSectionsClickable() およびsectionPressed()も参照してください 。

[signal] void QHeaderView::sectionHandleDoubleClicked(int logicalIndex)

このシグナルは、セクションがダブルクリックされたときに発生します。セクションの論理インデックスは、logicalIndex で指定されます。

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

[signal] void QHeaderView::sectionMoved(int logicalIndex, int oldVisualIndex, int newVisualIndex)

このシグナルは、セクションが移動されたときに発火します。セクションの論理インデックスは `logicalIndex` で指定され、以前のインデックスは `oldVisualIndex` で、新しいインデックス位置は `newVisualIndex` で指定されます。

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

int QHeaderView::sectionPosition(int logicalIndex) const

指定されたlogicalIndex のセクション位置を返します。セクションが非表示の場合は-1を返します。位置は、最初に表示される項目の左上隅から、logicalIndex が設定された項目の左上隅までの距離(ピクセル単位)で測定されます。水平ヘッダーの場合はx軸に沿って、垂直ヘッダーの場合はy軸に沿って測定されます。

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

[signal] void QHeaderView::sectionPressed(int logicalIndex)

このシグナルは、セクションが押されたときに発せられます。セクションの論理インデックスは、logicalIndex で指定されます。

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

QHeaderView::ResizeMode QHeaderView::sectionResizeMode(int logicalIndex) const

指定されたlogicalIndex で指定されたセクションに適用されるリサイズモードを返します。

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

[signal] void QHeaderView::sectionResized(int logicalIndex, int oldSize, int newSize)

このシグナルは、セクションのサイズが変更されたときに発生します。セクションの論理番号は `logicalIndex` で指定され、以前のサイズは `oldSize` で、新しいサイズは `newSize` で指定されます。

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

int QHeaderView::sectionSize(int logicalIndex) const

指定されたlogicalIndex の幅(縦方向のヘッダーの場合は高さ)を返します。

length()、setSectionResizeMode()、およびdefaultSectionSize()も参照してください 。

[virtual protected] QSize QHeaderView::sectionSizeFromContents(int logicalIndex) const

指定されたlogicalIndex で指定されたセクションの内容のサイズを返します。

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

int QHeaderView::sectionSizeHint(int logicalIndex) const

logicalIndex で指定されたセクションに対して、適切なサイズヒントを返します。

Qt::SizeHintRole

sizeHint()、defaultSectionSize()、minimumSectionSize()、およびmaximumSectionSize()も参照してください 。

int QHeaderView::sectionViewportPosition(int logicalIndex) const

指定されたlogicalIndex のセクションビューポートの位置を返します。

セクションが非表示の場合、戻り値は未定義となります。

sectionPosition() およびisSectionHidden()も参照してください 。

[protected slot] void QHeaderView::sectionsAboutToBeRemoved(const QModelIndex &parent, int logicalFirst, int logicalLast)

このスロットは、parent からセクションが削除された際に呼び出されます。logicalFirst およびlogicalLast は、セクションが削除された箇所を示します。

削除されたセクションが 1 つだけの場合は、logicalFirst とlogicalLast は同じ値になります。

bool QHeaderView::sectionsClickable() const

sectionsClickable を返します。

注: セクションのセクションクリック可能状態を取得するゲッター 関数。

関連項目: setSectionsClickable()。

bool QHeaderView::sectionsHidden() const

ヘッダー内のセクションが非表示になっている場合は `true ` を返し、そうでない場合は `false` を返します。

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

[protected slot] void QHeaderView::sectionsInserted(const QModelIndex &parent, int logicalFirst, int logicalLast)

このスロットは、parent にセクションが挿入された際に呼び出されます。logicalFirst およびlogicalLast のインデックスは、新しいセクションが挿入された位置を示します。

セクションが1つだけ挿入された場合、logicalFirst とlogicalLast は同じ値になります。

bool QHeaderView::sectionsMovable() const

sectionsMovable を返します。

注: プロパティ sectionsMovable のゲッター 関数です。

関連項目: setSectionsMovable()。

bool QHeaderView::sectionsMoved() const

ヘッダー内のセクションが移動された場合は `true ` を返し、そうでない場合は `false` を返します。

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

[override virtual] void QHeaderView::setModel(QAbstractItemModel *model)

QAbstractItemView::setModel(QAbstractItemModel *model) を再実装します。

[slot] void QHeaderView::setOffset(int offset)

ヘッダーのオフセットをoffset に設定します。

offset() およびlength()も参照してください 。

[slot] void QHeaderView::setOffsetToLastSection()

最後のセクションが表示されるようにオフセットを設定します。

setOffset()、sectionPosition()、およびsetOffsetToSectionPosition()も参照してください 。

[slot] void QHeaderView::setOffsetToSectionPosition(int visualSectionNumber)

指定されたvisualSectionNumber におけるセクションの先頭までのオフセットを設定します。visualSectionNumber は、hiddenSectionsを考慮しない場合の実際に表示されるセクションです。これは、必ずしもvisualIndex()の結果と同じとは限りません。

setOffset() およびsectionPosition()も参照してください 。

void QHeaderView::setResizeContentsPrecision(int precision)

ResizeToContents が使用される際、QHeaderView がサイズをどの程度正確に計算するかを設定します。値を小さくすると、精度は低下しますが自動リサイズが高速に行われ、値を大きくすると精度は高くなりますが、処理に時間がかかる場合があります。

precision で指定する数値は、推奨サイズを計算する際に考慮すべきセクションの数を示します。

デフォルト値は 1000 であり、これは自動リサイズが設定された水平カラムが、自動リサイズを行う際の計算において最大 1000 行を対象とすることを意味します。

特別な値「0」は、表示領域のみを対象とすることを意味します。特別な値「-1」は、すべての要素を対象とすることを意味します。

この値は、QTableView::sizeHintForColumn()、QTableView::sizeHintForRow()、およびQTreeView::sizeHintForColumn() で使用されます。これらの関数を再実装すると、この関数の効果が無効になる場合があります。

resizeContentsPrecision()、setSectionResizeMode()、resizeSections()、QTableView::sizeHintForColumn()、QTableView::sizeHintForRow()、およびQTreeView::sizeHintForColumn()も参照してください 。

void QHeaderView::setSectionHidden(int logicalIndex, bool hide)

hide が true の場合、logicalIndex で指定されたセクションは非表示になります。そうでない場合は、そのセクションが表示されます。

isSectionHidden() およびhiddenSectionCount()も参照してください 。

void QHeaderView::setSectionResizeMode(QHeaderView::ResizeMode mode)

ヘッダーのサイズ変更に関する制約を、指定されたmode で記述されている内容に設定します。

sectionResizeMode()、length()、およびsectionResized()も参照してください 。

void QHeaderView::setSectionResizeMode(int logicalIndex, QHeaderView::ResizeMode mode)

ヘッダー内の `logicalIndex ` で指定されたセクションのサイズ変更に関する制約を、指定された `mode` で記述されたものに変更します。この関数が呼び出される時点で、論理インデックスが存在している必要があります。

注: stretchLastSection プロパティが true に設定されている場合、この 設定は 最後のセクションでは無視されます。これは、QTreeView によって提供される水平ヘッダーのデフォルト設定です。

関連項目: setStretchLastSection() およびresizeContentsPrecision()。

void QHeaderView::setSectionsClickable(bool clickable)

sectionsClickable をclickable に設定します。

注: プロパティ `sectionsClickable`のセッター 関数です。

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

void QHeaderView::setSectionsMovable(bool movable)

sectionsMovable をmovable に設定します。

注: プロパティ `sectionsMovable`のセッター 関数です。

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

[override virtual protected] void QHeaderView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags flags)

QAbstractItemView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags flags) を再実装します。

指定されたflags に従って、指定されたrect 内の項目を選択します。

基底クラスの実装では何も行われません。

void QHeaderView::setSortIndicator(int logicalIndex, Qt::SortOrder order)

指定されたlogicalIndex で指定されたセクションのソートインジケータを、order で指定された方向に設定し、ソートインジケータが表示されていた他のすべてのセクションからそれを削除します。

logicalIndex 引数として -1 を指定した場合、ソートインジケータは表示されず、モデルは元のソートされていない順序に戻ります。なお、すべてのモデルがこの機能をサポートしているわけではなく、この場合にクラッシュする可能性もある点に注意してください。

sortIndicatorSection() およびsortIndicatorOrder()も参照してください 。

[override virtual] void QHeaderView::setVisible(bool v)

プロパティ `QWidget::visible` のアクセス関数を再実装します。

void QHeaderView::showSection(int logicalIndex)

logicalIndex で指定されたセクションを表示します。

hideSection()、isSectionHidden()、hiddenSectionCount()、およびsetSectionHidden()も参照してください 。

[override virtual] QSize QHeaderView::sizeHint() const

QAbstractScrollArea::sizeHint() const を再実装します。

このヘッダーに適したサイズヒントを返します。

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

[signal] void QHeaderView::sortIndicatorChanged(int logicalIndex, Qt::SortOrder order)

このシグナルは、ソートインジケータを含むセクション、または指定された順序が変更されたときに発せられます。セクションの論理インデックスは `logicalIndex ` で指定され、ソート順は `order` で指定されます。

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

Qt::SortOrder QHeaderView::sortIndicatorOrder() const

ソートインジケータの順序を返します。ソートインジケータが設定されているセクションがない場合、この関数の戻り値は未定義となります。

setSortIndicator() およびsortIndicatorSection()も参照してください 。

int QHeaderView::sortIndicatorSection() const

ソートインジケータが設定されているセクションの論理インデックスを返します。デフォルトでは、これはセクション 0 です。

setSortIndicator()、sortIndicatorOrder()、およびsetSortIndicatorShown()も参照してください 。

int QHeaderView::stretchSectionCount() const

「stretch」リサイズモードに設定されているセクションの数を返します。ビューでは、ビューのジオメトリが変更された際に、ヘッダービューがセクションのサイズを変更する必要があるかどうかを確認するためにこれを使用できます。

stretchLastSectionも参照してください 。

void QHeaderView::swapSections(int first, int second)

視覚インデックスfirst のセクションと、視覚インデックスsecond のセクションを入れ替えます。

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

[override virtual protected] int QHeaderView::verticalOffset() const

再実装:QAbstractItemView::verticalOffset() const。

ヘッダーの垂直方向のオフセットを返します。水平方向のヘッダーの場合は 0 になります。

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

[override virtual protected] bool QHeaderView::viewportEvent(QEvent *e)

QAbstractItemView::viewportEvent(QEvent *event) を再実装しています。

int QHeaderView::visualIndex(int logicalIndex) const

指定されたlogicalIndex で示されるセクションのビジュアルインデックスの位置を返します。それ以外の場合は-1を返します。

非表示のセクションでも、ビジュアルインデックスは有効です。

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

int QHeaderView::visualIndexAt(int position) const

ビューポート内で、指定されたposition を覆うセクションのビジュアルインデックスを返します。

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

© 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.