uniformRowHeights
あなたのQTreeView
ここに出発するとともに、私が試してみたいことです。
これを行うにはいくつかの方法があります。私は Qt のシグナル/スロットを使用するのが好きなので、 のカスタムQAbstractItemModel
で高さを変更しQTreeView
ます。このカスタム モデルは、のselectionChanged
からの信号に接続されQItemSelectionModel
ますQTreeView
。サンプル コード/スニペットは単一選択モードで動作しますが、選択した複数の行を処理するように簡単に変更できます。
ステップ 1 - 選択スロットを使用してカスタム モデルを作成する
から派生するカスタム モデル クラスをQAbstractItemModel
作成し、次のようなスロットを作成してください。
Q_SLOTS:
void onSelectionChanged( const QItemSelection&, const QItemSelection& );
モデル クラス内に、次のスニペット/メソッドを追加します。
void MyModelClass::onSelectionChanged( const QItemSelection& selected,
const QItemSelection& deselected )
{
if( !selected.empty() )
{
// Save the index within the class.
m_selectedIndex = selected.first();
Q_EMIT dataChanged( m_selectedIndex, m_selectedIndex );
}
}
QVariant MyModelClass::data( const QModelIndex& index, int role ) const
{
// Use the selected index received from the selection model.
if( m_selectedIndex.isValid() &&
index == m_selectedIndex &&
role == Qt::SizeHintRole )
{
// Return our custom size!
return QSize( 50, 50 );
}
...
}
ステップ 2 - 選択の変更をモデルに接続する
QTreeView
カスタムモデルを作成し、次の手順を実行します。
MyTreeView::MyTreeView( QWidget* parent ) : QWidget( parent )
{
...
MyModelClass* model = new MyModelClass();
setModel( model );
setSelectionMode( QAbstractItemView::SingleSelection );
setSelectionBehavior( QAbstractItemView::SelectRows );
connect
(
selectionModel(),
SIGNAL( selectionChanged(const QItemSelection&, const QItemSelection&) ),
model,
SLOT( onSelectionChanged(const QItemSelection&, const QItemSelection&) )
);
}
QItemSelectionModel
これを行うにはいくつかの方法があると確信しています。つまり、を直接あなたに渡しますQAbstractItemModel
が、シグナル/スロットを使用して選択をモデルに保存することを好みます。
お役に立てれば。