【发布时间】:2016-12-29 09:11:30
【问题描述】:
这是我的问题。当我在 QTableView 中编辑一个单元格时,如果我按 TAB 键,我希望当前单元格被更新,但下一个单元格不处于编辑模式。
我尝试在表中创建一个 eventFilter,但它不起作用。如果我在委托的编辑器小部件中创建 eventFilter 也不会。 这是我在 QTableView 中尝试的 eventFilter。我的想法是,如果当前单元格在最后一列和最后一行并且当前行不为空,我插入另一行(这工作正常),如果我正在编辑任何单元格,当我按 TAB 时,我可以放置在下一个单元格但没有编辑模式
这是一个sn-p的代码:
bool MiTabla::eventFilter(QObject *watched, QEvent *e)
{
if (e->type() == QEvent::KeyPress)
{
QModelIndex indice = this->currentIndex();
QKeyEvent *ke =static_cast<QKeyEvent*>(e);
switch (ke->key())
{
case (Qt::Key_Delete):
{
if (this->selectionModel()->isRowSelected(indice.row(),QModelIndex()))
{
//borrarLineas();
}
else
{
this->model()->setData(this->currentIndex(),"",Qt::EditRole);
}
break;
}
case (Qt::Key_Tab):
{
if (indice.row() == this->model()->rowCount(QModelIndex())-1
&& indice.column() == this->model()->columnCount(QModelIndex())-1
&& !NombreVacio())
{
this->model()->insertRow(this->model()->rowCount(QModelIndex()));
QModelIndex ind = this->model()->index(indice.row()+1,0);
this->setCurrentIndex(ind);
}
else //this doesn't work
{
QModelIndex ind = this->model()->index(indice.row(),indice.column()+1);
this->setCurrentIndex(ind);
}
break;
【问题讨论】:
-
解决方案草图:子类 QStyledItemDelegate,覆盖其
eventFilter,对 Tab 键进行稍微不同的处理,否则调用基类实现。参照。内置处理:code.woboq.org/qt5/qtbase/src/widgets/itemviews/… -
eventFilter方法的返回值是多少?您是否尝试为
Key_Tab案例返回“true”? -
谢谢佩佩。这只是我在寻找。而 ramzes2,另一个线索是返回 false 让表“使用”按下的 TabKey