PDA

View Full Version : Context menu on QStyledItemDelegate



sindy
10th October 2012, 16:43
I have a QTableView with custom item delegates defined for some of the columns. One of the columns is of type QDateTime. The delegate for this column is a QDateTimeEdit. I am able to edit the column using this delegate. However, once a date is set on a column, I am unable to go back and clear it. I can only edit it. I understand that this is due to the bug reported here https://bugreports.qt-project.org/browse/QTBUG-277

I want to be able to clear this column entry using the delegate. I notice that there is a default context menu defined on the delegate with actions like cut, copy, paste, delete.
As a work around to my problem, I want to customize this delete action. I am unable to figure out where this delete action leads to however, that is what slot it calls. What I would like to know is :
1. Which slot is called on clicking this delete action
2. Is it possible to customize this slot without adding a custom context menu
3. Is there any other way I could clear the QDateTimeEdit widget

norobro
11th October 2012, 20:43
1. Which slot is called on clicking this delete actionHere (http://qt.gitorious.org/qt/qt/blobs/4.8/src/gui/text/qtextcontrol.cpp#line755) is the slot, called from here (http://qt.gitorious.org/qt/qt/blobs/4.8/src/gui/text/qtextcontrol.cpp#line2102).


2. Is it possible to customize this slot without adding a custom context menuPossible? I guess, but complicated.


3. Is there any other way I could clear the QDateTimeEdit widgetWhy do you not want to add a custom context menu? One way to do this is to subclass QDateTimeEdit, add a custom context menu with a delete action, and set the year to some year far in the future, say the default maximum 7999. Then in your delegate don't paint anything if the year == 7999.

wysota
11th October 2012, 21:36
There is something called QAbstractSpinBox::specialValueText. You can use it to something like "none" or " ". Then reimplement QAbstractItemDelegate::setModelData() and interpret that special value properly by clearing (or whatever else your usecase needs) the respective data in the model.

sindy
12th October 2012, 14:49
Thanks norobro and Wysota

You are right norobro, customizing that slot does look complicated.
The only reason I did not want to add a custom context menu is because the standard context menu has other actions like cut, copy paste etc which suit my need. I only wanted to customize the delete action.
But anyway, adding custom context menu seems like the easiest way to do it and thats how I have done it. It works. However, I added the custom context menu on the delegate rather than QDateTimeEdit. Thats because I already have a custom delegate and this way I will not need to subclass QDateTimeEdit.

Wysota,
I think I could use QAbstractSpinBox::specialValueText. Right now the way I am clearing the model in the delete slot is a little twisted logic and I wasn't really happy with it. Setting specialValueText in the delete slot and letting the setModelData take care of the special value seems like a much better approach.