Qt::QTableWidget: SIGNAL currentCellChanged:鼠标和键盘之间的差异

Qt::QTableWidget: SIGNAL currentCellChanged: Differ between mouse and keyboard

本文关键字:之间 键盘 鼠标 QTableWidget SIGNAL currentCellChanged Qt      更新时间:2023-10-16

我有一个QTableWidget,每当我用鼠标或键盘(制表键或箭头键)选择一行时,它都会发出调用SLOT方法的SIGNAL currentCellChanged。是否有可能找出使用的是哪一个(鼠标还是键盘)?

我假设您使用的是一个普通的Designer表单类,表格小部件是该表单的一部分。

在你的表单类的构造函数中,你应该这样做:

ui->tableWidget->viewport()->installEventFilter(this);

添加eventFilter方法到你的类(它重新实现虚拟的QObject::eventFilter):

bool MyForm::eventFilter(QObject* object, QEvent* event) {
  if (object == ui->tableWidget->viewport()) {
    if (event->type() == QEvent::KeyPress) {
      method = method_keyboard;
    } else if (event->type() == QEvent::MouseButtonPress) {
      method = method_mouse;
    }
  }
  return false;
}

在槽中,您可以查看method变量的值,以确定使用了哪个控件。