PDA

View Full Version : menu on right click on canvas



quickNitin
7th October 2006, 10:30
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

jpn
7th October 2006, 10:46
Like I just said in the overlapping canvases (http://www.qtcentre.org/forum/f-newbie-4/t-overlapping-canvases-3928.html) 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.

quickNitin
7th October 2006, 15:10
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-newbie-4/t-overlapping-canvases-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.


...
abc=new QAction(tr("hello"),this);
connect(abc,SIGNAL(triggered()),this,SLOT(ABC()));
...
void ABC()
{
QMessageBox::information(this,tr("Hello"),tr("Sample Context Menu Acion"));

}
...
void contextMenuEvent(QContextMenuEvent *event)
{
QMenu mn(this);
mn.addAction(abc);
mn.exec(event->globalPos());

}




same here


void contentsMousePressEvent( QMouseEvent *e )
{
Q3CanvasItemList il = canvas()->collisions( e->pos() );
if(e->button()==Qt::LeftButton)
{
for( Q3CanvasItemList::Iterator it=il.begin(); it!=il.end(); ++it ) {
if( (*it)->rtti() != Q3CanvasText::RTTI )
{
dragging = (*it);

xoffset = (int)(e->x() - dragging->x());
yoffset = (int)(e->y() - dragging->y());

return;
}
}
}
else if(e->button()==Qt::RightButton)
{
for( Q3CanvasItemList::Iterator it=il.begin(); it!=il.end(); ++it ) {
if( (*it)->rtti() != Q3CanvasText::RTTI )
{
if( (*it)->rtti() == Q3CanvasRectangle::RTTI)
{
QMessageBox::information(this,tr("Hello"),tr("Sample Context Menu Acion on rectangle"));
//can generate a specific menu here but how will pass this canvas element to that action for operations over that
}
}
}
}
}


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).

jpn
7th October 2006, 18:45
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..

quickNitin
8th October 2006, 12:09
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.