1

次のコードを使用して、QTreeView コンポーネントで行全体を選択しようとしています。

const QModelIndex topLeft = model->index(0, 0);
const QModelIndex bottomRight = model->index(model->rowCount(), model->columnCount());
ui->hidDescriptorView->selectionModel()->selection().select(topLeft, bottomRight);

私は少し無知で、 const_cast などを使用して選択を機能させようと探し回っていましたが、コンパイラーは次のエラーを出しています:

/.../mainwindow.cpp:93: error: member function 'select' not viable: 'this' argument has type 'const QItemSelection', but function is not marked const
            ui->hidDescriptorView->selectionModel()->selection().select(topLeft, bottomRight);
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

私は選択を行うことができた前のビットから来ていますが、単一のセルしか選択されないため、ユーザーがクリックしたかのように、行全体が適切に選択されるように上記を試しています.

どんな助けでも大歓迎です!

4

2 に答える 2

3

selection() のシグネチャは

const QItemSelection selection () const

つまり、const コピーであるため、QItemSelection をその場で変更することはできません。コピーであるため、変更しても効果はありません。

代わりに、コピーを作成し (代わりに新しい QItemSelection を作成するだけです)、select() 経由で渡します。

QItemSelection selection = view->selectionModel()->selection();
selection.select(topLeft, bottomRight);
view->selectionModel()->select(selection, QItemSelectionModel::ClearAndSelect);

行を選択したいと言ったように、もっと簡単な方法があるかもしれません:

view->selectionModel()->select(topLeft, QItemSelectionModel::ClearAndSelect|QItemSelectionModel::Rows);

ユーザーによる選択を行全体に展開するには:

view->setSelectionBehavior(QAbstractItemView::SelectRows);
于 2013-05-16T05:08:50.217 に答える