Results 1 to 5 of 5

Thread: menu on right click on canvas

  1. #1
    Join Date
    Apr 2006
    Posts
    122
    Thanks
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default menu on right click on canvas

    greetings,
    i want to have a contect menu over canvas and its items. This items wil contain the operation i want to perform over it.
    I have gone throught doc there is contextMenuEvent() for qwidget but nothing such for QCanvas. How i can code such functionality.

    quickNitin

  2. #2
    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: menu on right click on canvas

    Like I just said in the overlapping canvases thread,
    Well, Q3Canvas isn't a widget and can't be displayed on its own. You're displaying the canvas in a Q3CanvasView, right? Q3CanvasView is a widget and can be moved, resized, etc. like any other widget.
    The same applies in this. You are supposed to handle the context menu event for Q3CanvasView, not the Q3Canvas.
    J-P Nurmi

  3. #3
    Join Date
    Apr 2006
    Posts
    122
    Thanks
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Unhappy Re: menu on right click on canvas

    i tried and menu did worked.
    I need some advice on how to proceed further.
    As we discussed in thread http://www.qtcentre.org/forum/f-newb...ases-3928.html
    , i have a large canvas , over this i have many canvs elements and some other smallcanvas. Now i want this context menu to be specific to element on which it is clicked.I mean i have diffrent function corresponding to diffrent element. These operation includes , changing elements shape and displayig information(selective) about that element.

    if i do this, this is generating a general context menu, orignating same at each place irrespective of click where it happenend or i can get position from QContextMenuEvent class.
    Qt Code:
    1. ...
    2. abc=new QAction(tr("hello"),this);
    3. connect(abc,SIGNAL(triggered()),this,SLOT(ABC()));
    4. ...
    5. void ABC()
    6. {
    7. QMessageBox::information(this,tr("Hello"),tr("Sample Context Menu Acion"));
    8.  
    9. }
    10. ...
    11. void contextMenuEvent(QContextMenuEvent *event)
    12. {
    13. QMenu mn(this);
    14. mn.addAction(abc);
    15. mn.exec(event->globalPos());
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

    same here
    Qt Code:
    1. void contentsMousePressEvent( QMouseEvent *e )
    2. {
    3. Q3CanvasItemList il = canvas()->collisions( e->pos() );
    4. if(e->button()==Qt::LeftButton)
    5. {
    6. for( Q3CanvasItemList::Iterator it=il.begin(); it!=il.end(); ++it ) {
    7. if( (*it)->rtti() != Q3CanvasText::RTTI )
    8. {
    9. dragging = (*it);
    10.  
    11. xoffset = (int)(e->x() - dragging->x());
    12. yoffset = (int)(e->y() - dragging->y());
    13.  
    14. return;
    15. }
    16. }
    17. }
    18. else if(e->button()==Qt::RightButton)
    19. {
    20. for( Q3CanvasItemList::Iterator it=il.begin(); it!=il.end(); ++it ) {
    21. if( (*it)->rtti() != Q3CanvasText::RTTI )
    22. {
    23. if( (*it)->rtti() == Q3CanvasRectangle::RTTI)
    24. {
    25. QMessageBox::information(this,tr("Hello"),tr("Sample Context Menu Acion on rectangle"));
    26. //[COLOR="Red"]can generate a specific menu here but how will pass this canvas element to that action for operations over that[/COLOR]
    27. }
    28. }
    29. }
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 

    i know on which element i clicked but how i can pass this information( on which object on canvas or window i clicked ) to actions on context menu. Why i need context menu is since i have 5-6 oprns. User will select between them.

    all advices are welcome in this regard as i am getting very raw ideas(working on such task for first time).

  4. #4
    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: menu on right click on canvas

    Maybe you could just simply store the item as a member variable? Or use QAction::setData() for storing a pointer to the item. It's totally up to you how do you want to implement it. It might also depend on which slots and where do the actions trigger..
    J-P Nurmi

  5. #5
    Join Date
    Apr 2006
    Posts
    122
    Thanks
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: menu on right click on canvas

    i am getting some feel of it.
    Ihave declared a member variable Q3canvasitem class pointer.in contentsMousePressEvents() i am checking rtti() of canvas item( obatained through collision) .There i am storing corresponding item in member pointer and generating menu there only.
    With other development i will keep updating.

Similar Threads

  1. Context Menu on QTableWidget
    By ankurjain in forum Qt Programming
    Replies: 9
    Last Post: 17th December 2009, 09:52
  2. When is the best time to delete a QCanvasItem
    By irudkin in forum Qt Programming
    Replies: 12
    Last Post: 8th March 2007, 21:28
  3. Setting focus on Canvas
    By Kapil in forum Qt Programming
    Replies: 10
    Last Post: 17th May 2006, 12:55
  4. Tracking separators in a menu (insertSeparator)
    By PrimeCP in forum Qt Programming
    Replies: 4
    Last Post: 25th January 2006, 18:10
  5. Replies: 5
    Last Post: 12th January 2006, 15:40

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.