Results 1 to 10 of 10

Thread: ContextMenu

  1. #1

    Question ContextMenu

    How do I restrict my contextmenu to be enabled only within my widget....
    say my widget is Rectangle and i want to enable my context menu only within this rectangle.....

    How am I restrict my menu.exec() to the coordinates of my rectangle...

    Thanks in Advance
    Naveen

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: ContextMenu

    If you dont want play with calculating you can reinplement mousePressEvent only for your rectangle
    a life without programming is like an empty bottle

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: ContextMenu

    I don't quite get your point. If you are implementing a context menu event for your widget, isn't it within your widget then?

    Another thing is, if you wanna show the context menu only for a specific part of your widget.
    Then you could set your widget's contextMenuPolicy to Qt::CustomContextMenu and connect to the customContextMenuRequested signal which provides a position parameter.

  4. #4
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: ContextMenu

    I mean reinplementing mousePressEvent() that can give ignore right button pressed
    a life without programming is like an empty bottle

  5. #5
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ContextMenu

    Quote Originally Posted by zlatko
    I mean reinplementing mousePressEvent() that can give ignore right button pressed
    Reimplementing contextMenuEvent() is the right thing to do, not mousePressEvent().
    Save yourself some pain. Learn C++ before learning Qt.

  6. #6
    Join Date
    Jan 2006
    Location
    N.B. Canada
    Posts
    47
    Thanked 8 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ContextMenu

    Although I haven't tried this, and I am not at a comp w/ Qt installed right now and won't be for several hours, but as far as I understand the docs, you can also do the following:

    1. Create all the actions you want in the menu.
    2. Add these actions to the widget, using QWidget::addAction(...)
    3. Set the widgets contextPolicy to Qt::ActionsContextMenu

    If I understand correctly, this way, when your widget receives the context menu event, it will display its QWidget::actions() as context menu.

    Bojan
    The march of progress:
    C:
    printf("%10.2f", x);
    C++:
    cout << setw(10) << setprecision(2) << showpoint << x;
    Java:
    java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance();
    formatter.setMinimumFractionDigits(2);
    formatter.setMaximumFractionDigits(2);
    String s = formatter.format(x);
    for (int i = s.length(); i < 10; i++) System.out.print(' ');
    System.out.print(s);

  7. #7
    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: ContextMenu

    Is the issue here about having a context menu for the whole widget or only for a part of it? Is that rectangle a widget on its own or is it just some hotspot not being a standalone widget itself? If the former, then all above mentioned methods apply, but if the latter, the issue is about checking whether the event is to be handled or ignored and you have to do it by checking coordinates of the event -- if they are within specified bounds, execute the menu (QMenu::exec), otherwise just do nothing.

  8. #8

    Question Re: ContextMenu

    What i'm actually trying out is that .....

    When we minimise a window and then right click on it, we a shown with these options right("close","maximise","minimise","move","size", "restore")...... and even when we right click on the tiltle bar(in blue) when the window is open we are shown with these options...

    These are given to us by the operating System ...

    IN my application....
    What i did was, I cut off the title bar( the thing in blue in every window we open with minimise,restore, and close options ) and implemented a widget which is a simple rectangle on top of my UI inteface of my application and I was trying to implement a defaultContextMenu on this and tried to see whether this could act as the context menu given to us by the operating system......

    Is there any thing which could give us this and
    And if it does not exists what could be the solution given to this.....

    This is where I'm stuck .....Please give me the solution in detail.... including the code to do this ......

  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: ContextMenu

    This is where I'm stuck .....Please give me the solution in detail.... including the code to do this ......
    @Naveen:
    All though you have the right to ask this, you should be aware that this is not very appreciated on forums such as this one.
    People like to help, but you should show some effort to solve your own problems, therefore asking for the full solution is not liked, never the less, allowed.

    It also doesn't help if you post double, and then when the double post was deleted post it again...

  10. #10
    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: ContextMenu

    What exactly do you want to achieve? Maybe there are better ways to do this. But if you insist on doing it this way, just create that rectangle as a QFrame and either reimplement its contextMenuEvent directly if you code by hand or install an event filter on the frame to catch its events and handle them elsewhere. With Qt4 there is also an option to use custom context menu on the frame which gives an ability to use signals and slots for handling the menu.

Similar Threads

  1. QTreeWidgetItem have contextMenu ?
    By yunpeng880 in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2012, 03:56
  2. Contextmenu in Qtreeview
    By LordQt in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2009, 16:39
  3. Modify a ContextMenu
    By smarinr in forum Qt Programming
    Replies: 5
    Last Post: 10th May 2008, 17:41
  4. contextmenu in QDockWidget title
    By klipko in forum Newbie
    Replies: 4
    Last Post: 7th March 2008, 22:32
  5. contextmenu with MDI
    By Thoosle in forum Qt Programming
    Replies: 1
    Last Post: 1st December 2006, 07:29

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.