Results 1 to 4 of 4

Thread: Multiple Context Menus

  1. #1
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Multiple Context Menus

    Season's Greetings (I guess this works any time of the year).

    I'm using Qt 4.4.2 on a Windows platform and need to do an unconventional drag & drop.

    Left AND right mouse button drags between two QTableWidgets are allowed. When releasing the buttons in the target table, the LMB does the add/overwrite but the RMB must display a context menu to allow the user to pick specific actions.

    My normal handling of the mouseReleaseEvent shows one context menu but the mouse release of a RMB drop should show a different menu. I believe I can handle the menu item switching with setVisible() calls.

    The issue here is that in my dropEvent() handler I can determine which button was used for the drag...
    Qt Code:
    1. if ( event->mouseButtons() & Qt::RightButton )
    2. // display context menu
    To copy to clipboard, switch view to plain text mode 
    ...but I don't know how to display a context menu at this point when the RMB is released. Can someone point me in the right direction to display a context menu from within the drop event handler?

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Multiple Context Menus

    you need to do next things:
    1. set context menu policy for widget for which context menu will be displayed
    Qt Code:
    1. ...
    2. m_myWidget->setContextMenuPolicy(Qt::CustomContextMenu);
    3. ...
    To copy to clipboard, switch view to plain text mode 
    2. connect signal customContextMenuRequested with slot in which context menu will be created
    Qt Code:
    1. ...
    2. connect(m_myWidget, SIGNAL(customContextMenuRequested(const QPoint &)), SLOT(updateContextMenu(const QPoint &)));
    3. ...
    To copy to clipboard, switch view to plain text mode 
    3. create needed context menu and show it
    Qt Code:
    1. void AnotherMyWidget::updateContextMenu(const QPoint &pos)
    2. {
    3. QMenu menu(tr("Context Menu"), this);
    4. //add actions to menu and create connections
    5. menu.exec(m_myWidget->mapToGlobal(pos));
    6. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple Context Menus

    Quote Originally Posted by spirit View Post
    you need to do next things:
    1. set context menu policy for widget for which context menu will be displayed
    2. connect signal customContextMenuRequested with slot in which context menu will be created
    3. create needed context menu and show it
    This makes the context menu switching easier but doesn't solve the basic problem of making a context menu show up when releasing the right mouse button at the end of a drag.

    This is the order of operations I'm looking for:
    1. right-click and drag from TableA to TableB
    2. release the right mouse button (now in the dropEvent() code)
    3. a context menu is displayed
    4. user makes a selection from the context menu
    5. drop is processed based upon that selection

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Multiple Context Menus

    ok, just call
    Qt Code:
    1. void AnotherMyWidget::updateContextMenu(const QPoint &pos)
    To copy to clipboard, switch view to plain text mode 
    after mouse releadse. (you have current point just pass it in method)
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    mclark (24th December 2008)

Similar Threads

  1. Replies: 0
    Last Post: 21st March 2008, 22:34
  2. how to corss compile for windows in Linux
    By safknw in forum Qt Programming
    Replies: 24
    Last Post: 13th May 2006, 06:23

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.