Qtableview 表格点击过再点击后 表格的焦点会自动移动到最后一个
时间: 2023-06-26 16:08:56 浏览: 66
这个问题可能是由于您在处理表格点击事件时,将焦点设置为了最后一行。您可以在处理表格点击事件时,避免将焦点设置为最后一行,以解决该问题。
以下是一个示例代码,可以在表格点击事件中获取所点击的行和列号,以及当前焦点所在的行号:
```
void MyTableView::mousePressEvent(QMouseEvent* event)
{
int row = this->rowAt(event->pos().y());
int col = this->columnAt(event->pos().x());
int currRow = this->currentIndex().row();
// do something with row, col and currRow
}
```
在这里,我们使用 `rowAt()` 和 `columnAt()` 函数来获取所点击的行和列号。然后,我们可以使用 `currentIndex()` 函数获取当前焦点所在的行号。在处理完表格点击事件后,不要设置焦点,以避免自动移动焦点到最后一行。
```
void MyTableView::mousePressEvent(QMouseEvent* event)
{
int row = this->rowAt(event->pos().y());
int col = this->columnAt(event->pos().x());
int currRow = this->currentIndex().row();
// do something with row, col and currRow
// don't set focus to avoid moving the focus to the last row
event->ignore();
}
```
这样,就可以避免在再次点击表格时,将焦点自动移动到最后一行。