Results 1 to 5 of 5

Thread: QMenu as QItemDelegate

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QMenu as QItemDelegate

    Hi!

    I am trying to open a menu when user double clicks on a cell of the table view. The problem is that the menu is appearing at the point (0,0) of the screen and not at the place where the cell is located.

    Is there a way by which I can get the coordinates of the cell and move the menu to that location?

    Thanks

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QMenu as QItemDelegate


  3. #3
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMenu as QItemDelegate

    Thanks Caduel!

    I am aware of this function. But the problem is that I have re-implemented QItemDelegate class and in createEditor function I am creating the menu.

    In order to show this menu in the right position in need the rect of the cell. But to get this I need to access QAbstractItemView function from the sub -class of QItemDelegate class.

    Is there a way by which I can do this? What QWidget is the parent on the editor?

    Thanks

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMenu as QItemDelegate

    In the createEditor() method try to convert QStyleOptionViewItem &option to QStyleOptionViewItemV3 or later, because it has a member const QWidget *widget (from Qt source):
    Qt Code:
    1. class Q_GUI_EXPORT QStyleOptionViewItemV3 : public QStyleOptionViewItemV2
    2. {
    3. public:
    4. enum StyleOptionVersion { Version = 3 };
    5.  
    6. QLocale locale;
    7. const QWidget *widget;
    8.  
    9. QStyleOptionViewItemV3();
    10. QStyleOptionViewItemV3(const QStyleOptionViewItemV3 &other)
    11. : QStyleOptionViewItemV2(Version) { *this = other; }
    12. QStyleOptionViewItemV3(const QStyleOptionViewItem &other);
    13. QStyleOptionViewItemV3 &operator = (const QStyleOptionViewItem &other);
    14.  
    15. protected:
    16. QStyleOptionViewItemV3(int version);
    17. };
    To copy to clipboard, switch view to plain text mode 
    There is high possibility that widget is a pointer to your view but it is not sure so first try to cast it with qobject_cast. If it is not the view, then you need to add the pointer to view in your delegate subclass and set it with your view while adding delegate to the view.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. #5
    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: QMenu as QItemDelegate

    Creating QMenu as the editor is probably not the best idea. It would be better to reimplement contextMenuEvent() in the view or connect to appropriate click events in the view. Then using indexAt() you can determine which item the mouse was over and you can trigger the menu in appropriate point of the screen.
    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.


Similar Threads

  1. QMenu popup: how close when clicked outside
    By powermax in forum Qt Programming
    Replies: 5
    Last Post: 4th March 2009, 04:18
  2. QStyle : how to make the QMenu display in middle?
    By nish in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 27th November 2008, 11:39
  3. premature call to setmodeldata with QItemDelegate
    By placebo in forum Qt Programming
    Replies: 1
    Last Post: 25th November 2007, 18:39
  4. how to popup and close a QMenu
    By Placido Currò in forum Qt Programming
    Replies: 15
    Last Post: 14th May 2007, 17:41
  5. Qmenu Stable close on QTableWidget
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 17th July 2006, 11:03

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.