0
這是我的問題。當我在QTableView中編輯單元格時,如果按TAB鍵,當前單元格會更新,但下一個單元格不處於編輯模式。如何在編輯QTableView中的單元格之後使用Tab鍵前進時避免編輯模式?
我嘗試在表中創建一個eventFilter,但它不起作用。如果我在委託的編輯器小部件中創建eventFilter, 這是我在QTableView中嘗試的一個eventFilter。我的想法是,如果當前單元格在最後一列和當前行並且當前行不是空的,我插入另一行(這工作正常),如果我正在編輯任何單元格,當按TAB時,我可以放置在下一行細胞,但沒有編輯模式
這是代碼片段:
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鍵做一個稍微不同的處理,否則調用基類實現。參看內置處理:https://code.woboq.org/qt5/qtbase/src/widgets/itemviews/qabstractitemdelegate.cpp.html#476 – peppe
eventFilter方法的返回值是什麼?你是否試圖爲'Key_Tab'情況返回「true」? – ramzes2
謝謝佩佩。這只是我正在尋找。和ramzes2,另一個線索是返回false讓表「按下」使用TabKey – user3733164