gpt4 book ai didi

c++ - 试图在 Qt QTreeView 中选择整行

转载 作者:行者123 更新时间:2023-11-28 03:17:01 25 4
gpt4 key购买 nike

我正在尝试使用以下代码在 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);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

我从前一点开始,在那里我设法做出了选择,但只有一个单元格会被选中,所以我正在尝试上面的操作以确保正确选择整行,就好像用户会点击它。

如有任何帮助,我们将不胜感激!

最佳答案

selection() 的签名是

const QItemSelection selection () 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);

关于c++ - 试图在 Qt QTreeView 中选择整行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16578462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com