Results 1 to 4 of 4

Thread: Context menu on QStyledItemDelegate

  1. #1
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Context menu on QStyledItemDelegate

    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

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Context menu on QStyledItemDelegate

    Quote Originally Posted by sindy View Post
    1. Which slot is called on clicking this delete action
    Here is the slot, called from here.

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

    3. Is there any other way I could clear the QDateTimeEdit widget
    Why 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.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Context menu on QStyledItemDelegate

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Context menu on QStyledItemDelegate

    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.

  5. The following user says thank you to sindy for this useful post:


Similar Threads

  1. Qt Context menu
    By jeyanthinatarajan in forum Newbie
    Replies: 3
    Last Post: 4th April 2012, 22:26
  2. Replies: 1
    Last Post: 4th November 2011, 11:25
  3. Context menu on tab
    By wolfi3b in forum Newbie
    Replies: 2
    Last Post: 18th October 2010, 17:35
  4. Context menu
    By dejvis in forum Newbie
    Replies: 2
    Last Post: 20th September 2009, 22:02
  5. Qwt and context menu
    By giusepped in forum Qwt
    Replies: 1
    Last Post: 9th December 2008, 08:55

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.