PDA

View Full Version : QTableView + edit mode on mouse hover



louissmr
19th August 2013, 18:44
I have been reading the star delegate (http://harmattan-dev.nokia.com/docs/platform-api-reference/xml/daily-docs/libqt4/itemviews-stardelegate.html) sample in order to learn how to use custom widgets in a qtableview cell.

In the sample, the edit mode is activated using a double click or a click if the item is selected, but, I would like to activate the edit mode when the mouse cursor is hover the cell containing the custom widget. QAbstractItemView has no EditTrigger (http://harmattan-dev.nokia.com/docs/library/html/qt4/qabstractitemview.html#EditTrigger-enum) for this. What is the best approach to achive this?

Thanks

Santosh Reddy
20th August 2013, 12:52
How are you adding the custom widget? using Delegate Painting / setIndexWidget()?

A simple apprach is to use setIndexWidget().BTW tell somthing about the custom widget, do you need button clicks on it/combo box or just plain images etc?

louissmr
20th August 2013, 14:07
The use case is the same as the one seen in the star delegate example.

I am using a QStyledItemDelegate for drawing the rating of the elements in my QTableView and for creating the "editor" widget. My QTableView is populated by a custom model filled with data from a data base.

I want to achieve the same behavior used in iTunes (list view) for rating elements. If a row is selected, the user should be able to rate this element by clicking in the rating widget, the first click over the rating widget modifies the rating if the row is selected, if not, the first click selects the row. In the example this click enables the edit mode, I want to avoid this behavior.

Thank you for your help Santosh.

louissmr
21st August 2013, 09:13
Finally, I have a preliminary version working.

I have subclased QTableView, activated mouse tracking and reimplemented the mouseMoveEvent. Then using indexAt I can get the model index under the mouse cursor. If the mouse is hover a selected row and hover the my rating column I call edit.

In my editor I have reimplemented the leaveEvent for closing the editor when the mouse cursor leaves it. I am using the mouseReleaseEvent in my editor for commiting data to my model.

KalEl
4th November 2016, 15:51
Finally, I have a preliminary version working.

I have subclased QTableView, activated mouse tracking and reimplemented the mouseMoveEvent. Then using indexAt I can get the model index under the mouse cursor. If the mouse is hover a selected row and hover the my rating column I call edit.

In my editor I have reimplemented the leaveEvent for closing the editor when the mouse cursor leaves it. I am using the mouseReleaseEvent in my editor for commiting data to my model.

Hello,
Could You provide me with a code sample of how You have managed to reimplement the leaveEvent?
I have a problem with calling off the editor of my itemDelegate on mouse leaving the listView widget.
I've managed to implement a custom widget as an editor for a custom treeView, by sub-classing the QStyledItemDelegate.
Also, when mouse enters my sub-classed treeView (or listView) i call the editor (mousetracking(true) and re-implementation of mouseMoveEvent).
But I still have a problem with calling off the editor, when mouse leaves the treeView . It is crucial, because only when the editor is called of, the data are saved to the model.

KalEl
13th January 2017, 10:26
Ok. Finally I've done something like this:

void MyTreeView::mouseMoveEvent(QMouseEvent *event){
//zamiast presEvent();
if(indexAt(event->pos()) != myPreviousIndex){
// mousePressEvent(event);
closePersistentEditor(myPreviousIndex);

openPersistentEditor(indexAt(event->pos()));

myPreviousIndex = indexAt(event->pos());

/* qDebug() << indexAt(event->pos()).internalId() << myPreviousIndex.internalId() << indexAt(event->pos()).row() << myPreviousIndex.row();
edit(indexAt(event->pos()));
myPreviousIndex = indexAt(event->pos()); */
}
QTreeView::mouseMoveEvent(event);
}

void MyTreeView::leaveEvent(QEvent *event)
{
// setMouseTracking(false);
closePersistentEditor(myPreviousIndex);

qDebug() << "left treeView";
myPreviousIndex = QModelIndex();
emit saveToModelFromEditor();
QTreeView::leaveEvent(event);
}