PDA

View Full Version : Changing tab key functionallity in QItemDelegate



Nightfox
18th July 2012, 10:50
Hi,
I'm using a QTreeView and I want to change the tab functionallity so it moves focus to the next column of the treeview instead of that of the one below (same behavior as QTableView ). The problem arises when the widget, created by the QItemDelegate, is in editing mode. In the tableview, the focus will move right and bring the next index in focus - so the user can start typing right away, which is great functionallity. In the treeview however, the focus moves down and sets the index below in focus. I want the treeview to behave like the tableview and move focus right (not down).

I'm trying to solve this by subclassing the QItemDelegate and implement a special editorEvent function, like this...



bool ItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
if (event->type() == QEvent::KeyPress)
if (static_cast<QKeyEvent*>(event)->key() == Qt::Key_Tab)
qDebug() << "Tab was pressed";

return QItemDelegate::editorEvent( event , model , option , index ) ;
}


The event doesn't seem to catch the Tab keypress. It's like the keypress handler is NOT moved to the widget created by the QItemDelegate. I tried to solve this by installing the eventfilter on the widget created by the delegate..



QWidget * ItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index)

QWidget *w = QItemDelegate::createEditor(parent,option,index) ;
w->installEventFilter( const_cast< ItemDelegate * >(this) );
return w;
}


What am I doing wrong??