4

複数の列を持つ Qtreeview である Qt 4.7.0 を使用しています。

私がやりたいことは「シンプル」です:選択されたときに線の高さを増やしたいです。

デリゲートはこれを行うのに十分でしょうか?

私は QTableView でいくつかのことを経験しました:

m_pMyTableView->verticalHeader()->setResizeMode(QHeaderView::Interactive);
...
QSize AbstractItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;

このテーブルビューで動作していますが、最初は垂直ヘッダーがないため、QTreeviewでこれを行う方法がわかりません...

誰かが私の道を教えてくれますか?

4

1 に答える 1

1

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が、シグナル/スロットを使用して選択をモデルに保存することを好みます。

お役に立てれば。

于 2012-07-06T00:56:24.697 に答える