Results 1 to 3 of 3

Thread: Question on Context Menu

  1. #1
    Join Date
    May 2011
    Posts
    14
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    5

    Question Question on Context Menu

    Good Day Everyone,

    Before getting to my question here is a brief overview of what I'm working on:

    I have a QMainWindow Form that has a few push buttons and a QTableWidget. I have created a customContextMenu for when a user right clicks on the QTableWidget.

    For the most part everything works as intended, but here are my questions/issues.

    1) Once I right on the QTableWidget the menu appears where the mouse cursor is, but will not disappear until another click ( left/right ) is detected. Is there a way to hide the menu once the cursor is no longer on top of it, or moves a certain distance away? ( I thought I saw a post on this once before, but am having trouble locating it to see if it helps with my issue )

    2) Is there a way to prevent the context menu from displaying on empty rows? My general thought is to do something with the cursor position and check where it is in relation to the table, and then see if there is a valid row at that particular point. However, I am unsure of how to really do this.

    Here is the method I used for creating my menu

    Qt Code:
    1. void QTMainform::contextMenuRequested( const QPoint& )
    2. {
    3. //QPoint globalPos = this->mapToGlobal( pos ) ;
    4.  
    5. QMenu myMenu ;
    6. myMenu.addAction( "Start" , this , SLOT( start()) ) ;
    7. myMenu.addAction( "Stop" , this , SLOT( stop() ) ) ;
    8. myMenu.addAction( "Help" , this , SLOT( help() ) ) ;
    9.  
    10. myMenu.exec( QCursor::pos() ) ;
    11. }
    To copy to clipboard, switch view to plain text mode 


    Any help is appreciated.

    Thanks
    Last edited by Sarol; 16th June 2011 at 18:18. Reason: reformatted to look better

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: Question on Context Menu

    Quote Originally Posted by Sarol
    1) Once I right on the QTableWidget the menu appears where the mouse cursor is, but will not disappear until another click ( left/right ) is detected. Is there a way to hide the menu once the cursor is no longer on top of it, or moves a certain distance away? ( I thought I saw a post on this once before, but am having trouble locating it to see if it helps with my issue )
    One of the ways to do, Install a event filter on the QTableWidget, which captures the mouse move event and check if mouse is not over context menu then hide / delete the menu, you can also install the filter on context menu itself

    Quote Originally Posted by Sarol
    2) Is there a way to prevent the context menu from displaying on empty rows? My general thought is to do something with the cursor position and check where it is in relation to the table, and then see if there is a valid row at that particular point. However, I am unsure of how to really do this.
    Qt Code:
    1. void QTMainform::contextMenuRequested( const QPoint& point )
    2. {
    3. ...
    4. QTableWidgetItem * item = tableWidget.itemAt(point);
    5. if(item != 0)
    6. {
    7. if(!item->text().isEmpty())
    8. {
    9. //Then Show Context Menu
    10. QMenu myMenu ;
    11. ...
    12. }
    13. }
    14. ...
    15. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Santosh Reddy for this useful post:

    Sarol (20th June 2011)

  4. #3
    Join Date
    May 2011
    Posts
    14
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    5

    Default Re: Question on Context Menu

    Santosh,

    Thank you very much for the response. I was able to solve the second question with your help.

    As for my first issue, I have not dealt with Event Filters before, so I will have to research these and then implement your suggestion.

    Have a great day,

    Sarol

Similar Threads

  1. Context menu on tab
    By wolfi3b in forum Newbie
    Replies: 2
    Last Post: 18th October 2010, 17:35
  2. Context Menu not working
    By waynew in forum Qt Programming
    Replies: 1
    Last Post: 10th January 2010, 03:48
  3. Context menu
    By dejvis in forum Newbie
    Replies: 2
    Last Post: 20th September 2009, 22:02
  4. Qwt and context menu
    By giusepped in forum Qwt
    Replies: 1
    Last Post: 9th December 2008, 08:55
  5. Context Menu
    By RY in forum Newbie
    Replies: 1
    Last Post: 10th September 2008, 07:59

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
  •  
Qt is a trademark of The Qt Company.