PDA

View Full Version : How to define previous and next items to edit with a qtreeview/qabstractitemview



mathieuS
11th January 2011, 20:54
Hi everybody

I created a tree model subclassing QAbstractItemModelwhich I render using a QTreeView.

When I edit an item and press the tab key, the editor jumps to the item in the next row, same column. I'd like the converse, I would like the editor to jump in the next column, same row.

All I found in Qt Assistant was this mysterious statement :

... the current index defines the next and previous items to edit, ...
in the documentation for QAbstractItemView::edit().
Though, I don't understand how a QModelIndex may hold other indexes than parent's and children's.

Thank you for lightning my candle,
Mathieu

tbscope
12th January 2011, 06:36
http://doc.qt.nokia.com/4.7/qabstractitemview.html#moveCursor

mathieuS
12th January 2011, 21:39
Thank, it is working great.

Here is my code, if anyone needs an example. The cursor moves forwards/backwards in a row. If it reaches row end/beginning, it jumps to the next/previous row.


QModelIndex MyTreeView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
{
if(cursorAction == QAbstractItemView::MoveNext)
{
QModelIndex index = currentIndex();
if(index.column() != 2)
return model()->index(index.row(), index.column()+1, index.parent());

setCurrentIndex(model()->index(index.row(), 0, index.parent()));
}
else if(cursorAction == QAbstractItemView::MovePrevious)
{
QModelIndex index = currentIndex();
if(index.column() != 0)
return model()->index(index.row(), index.column()-1, index.parent());

setCurrentIndex(model()->index(index.row(), 2, index.parent()));
}

return QTreeView::moveCursor(cursorAction, modifiers);
}