QTableView:选择单元格时,如何使选择的第一个单元格成为当前索引?
我有一个继承 QTableView 的简单类,我想要以下行为:当用户选择几个单元格时,我希望将选定的第一个单元格设置为当前索引。
因此,例如,如果我从 (0, 0) 到 (2, 2) 选择,当我开始输入时,文本将显示在 (0, 0),而不是 (2, 2),这似乎是默认值。
我尝试使用以下内容覆盖 setSelection 函数:
void SampleTable::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command)
{
if((command & QItemSelectionModel::Current) != 0)
{
QModelIndex curr = indexAt(rect.topLeft());
selectionModel()->select(curr, QItemSelectionModel::Current);
command ^= QItemSelectionModel::Current;
}
QTableView::setSelection(rect, command);
}
但无济于事。这似乎与鼠标事件有关,但我无法在源代码中找到问题所在,我希望有一种更简单的方法。
I have a simple class which inherits QTableView and I want the following behavior: when the user selects a few cells, I want the first cell selected to be set as the current index.
So for example if I select from (0, 0) towards (2, 2), when I start typing the text would show up in (0, 0), not (2, 2) which seems to be the default.
I have tried overriding the setSelection function with the following:
void SampleTable::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command)
{
if((command & QItemSelectionModel::Current) != 0)
{
QModelIndex curr = indexAt(rect.topLeft());
selectionModel()->select(curr, QItemSelectionModel::Current);
command ^= QItemSelectionModel::Current;
}
QTableView::setSelection(rect, command);
}
but to no avail. It seems to have something to do with the mouse events, but I can't quite locate the problem in the source code and I'm hoping there's an easier way anyway.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你想达到什么目的?如果您希望用户仅编辑/选择单个单元格,请使用 setSelectionBehaviour 来强制执行此操作。否则,您可以尝试 chinfoo 的想法,但确保以用户能够理解的方式传达行为(即他能够看到他的编辑将更改第一个单元格/行)。
What are you trying to achieve? If you'd like the user to edit/select a single cell only, use setSelectionBehaviour to force this. Otherwise you could try chinfoo's idea but make sure to communicate the behavior in a way the user is able to understand it (i.e. he's able to see that his edit will change the first cell/row).
我找出了问题所在以及如何解决它,但情况并不好。问题出在 QAbstractItemView 的鼠标移动事件中。经过多次调试和搜索源代码后,我在 qabstractitemview.cpp 中发现了这一点:
我通过给我的类提供一个 QModelIndex 成员来修复它,该成员存储左上角 QModelIndex 的最后一个位置(在我的 setSelection 的重写版本中设置,如上面所示),然后我用以下内容覆盖 mouseMoveEvent:
Not a pretty solution, but it works.
I figured out the problem and how to fix it, but it's not pretty. The problem is in the mouse moved event for a QAbstractItemView. After much debugging and searching through the source code I found this in qabstractitemview.cpp:
I fixed it by giving my class a QModelIndex member which stores the last location of the top left QModelIndex (set in my overriden version of setSelection like above) and then I overrode the mouseMoveEvent with this:
Not a pretty solution, but it works.
QtableWidget
类有一个信号itemSelectionChanged()
,将其连接到您的自定义插槽。在该槽中,使用selectedIndexes()
获取所有索引,然后使用setCurrentIndex()
设置要作为当前索引的单元格。The
QtableWidget
class has a signalitemSelectionChanged()
, connect it to your custom slot. In that slot, useselectedIndexes()
to get all indexes, then usesetCurrentIndex()
to set the cell which you want to be the current index.