当我在 treeView 中使用 Key_Up 或 Key_Down 移动当前索引时,我发现移动的当前索引仅在此之后应用第return QWidget::eventFilter(watched, event);
行。如何获得return QWidget::eventFilter(watched, event);
之前的新移动索引?我试图用currentIndex = currentIndex-1;
手动更改已移动的当前索引,但是没有用。
bool TipManager::eventFilter(QObject *watched, QEvent *event)
{
if(watched == ui->treeView && event->type() == QEvent::KeyPress){
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
QModelIndex currentIndex = ui->treeView->currentIndex();
if( keyEvent->key() == Qt::Key_Up )
{
//currentIndex = currentIndex-1;
}
if( keyEvent->key() == Qt::Key_Down )
{
//currentIndex = currentIndex+1;
}
if(currentIndex.isValid())
{
ui->treeView->setCurrentIndex(currentIndex);
trimCurrentPath(currentIndex);
}
}
return QWidget::eventFilter(watched, event);
}
答案 0 :(得分:0)
您可以先将事件传递下去,然后检查索引:
bool TipManager::eventFilter(QObject *watched, QEvent *event)
{
bool const ret = QWidget::eventFilter(watched, event);
if(watched == ui->treeView && event->type() == QEvent::KeyPress){
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
QModelIndex currentIndex = ui->treeView->currentIndex();
if( keyEvent->key() == Qt::Key_Up )
{
//currentIndex = currentIndex-1;
}
if( keyEvent->key() == Qt::Key_Down )
{
//currentIndex = currentIndex+1;
}
if(currentIndex.isValid())
{
ui->treeView->setCurrentIndex(currentIndex);
trimCurrentPath(currentIndex);
}
}
return ret;
}
但是我想实际上只有在离开 eventFilter(watched, event)
...