PDA

View Full Version : overlapping canvases



quickNitin
6th October 2006, 16:16
greetings,

Currently i want to show 2 canvas overalapping. I mean a full canvas abd other a small providing a global eye view. I am trying it hard to imagine how to pack it on a layout.

how i can do this

jacek
6th October 2006, 19:13
You should be able to do this by making that second canvas view a child of the first one (with some help of move() and setGeometry()).

quickNitin
7th October 2006, 10:09
hi jacek,
there are no move() or setGeometry() defined for Q3Canvas class but are there for QWidget.There is resize() in Q3Canvas but no move for canvas. move is there for canvas Item classes.

quickNitin

jpn
7th October 2006, 10:25
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.



Q3Canvas* canvas = ...
Q3CanvasView* large = ...
// create the small canvas view as a child of
// the large canvas view as jacek proposed
Q3CanvasView* small = (canvas, large);

quickNitin
7th October 2006, 11:15
thanks jpn,
this did the trick.
I have one more issue.
I want to have mouse event over this small canvasView. I want to move it to any place using mouse.There is a event handler mousePressEvent() in QWidget but how i will get to know which widgets are under mouse. How i will make this event to respond to entrance of mous just in smallCanvas.
Also such mouse events are also required for canvas items idsplayed on smallCanvas.For canvas elements using Q3Canvas::collision() i can find all canvas elements. and it is doing the task.

regards
quickNitin

jpn
7th October 2006, 12:09
Subclass Q3CanvasView and override appropriate mouse event handlers (mousePressEvent(), mouseMoveEvent() and mouseReleaseEvent()) and use that Q3CanvasView subclass as the small view. You may as well just filter the events (http://doc.trolltech.com/4.1/qobject.html#eventFilter) if you don't want to subclass.

quickNitin
7th October 2006, 12:57
i had tried this also but not working for moving smallCanvasview.



#include <QMouseEvent>
#include <Q3CanvasView>
#include <QPoint>
class MyCanvasView : public Q3CanvasView
{
Q_OBJECT
public:
MyCanvasView( Q3Canvas *c, QWidget *parent=0, const char *name=0, Qt::WFlags f=0 ) : Q3CanvasView( c, parent, name, f )
{
dragging = 0;
wt=0;
}

protected:

void mousePressEvent(QMouseEvent *e)
{
wt=childAt(e->x(),e->y());
if(wt!=0 && e->button()==Qt::LeftButton)
{
wxoffset=(int)(e->x()- (wt->pos).x());//!not using currently
wyoffset=(int)(e->y()- (wt->pos).y());


}
}

void mouseMoveEvent(QMouseEvent *e)
{
if(wt!=0)
{
wt->move(e->x(),e->y());
}
}

void mouseReleaseEvent(QMouseEvent *e)
{
if( wt!=0)
{
wt->move(e->x(),e->y());
wt=0;
}
}


void contentsMousePressEvent( QMouseEvent *e )
{
Q3CanvasItemList il = canvas()->collisions( e->pos() );
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;
}
}
}

void contentsMouseReleaseEvent( QMouseEvent *e )
{
if( dragging )
{
dragging->setX( e->x() - xoffset );
dragging->setY( e->y() - yoffset );

dragging = 0;

canvas()->update();
}
}

void contentsMouseMoveEvent( QMouseEvent *e )
{
if( dragging )
{
dragging->setX( e->x() - xoffset );
dragging->setY( e->y() - yoffset );

canvas()->update();
}
}

private:


QWidget *wt;
Q3CanvasItem *dragging;
int xoffset, yoffset;
int wxoffset,wyoffset;
};

jpn
7th October 2006, 18:31
The mouse events go directly to the small child view. Override and catch them in the child view, not in the large parent view. Alternatively you could install an event filter on the child.

quickNitin
8th October 2006, 07:05
code i have posted earlier is being used for both parent and child canvasViews.
Can you give some more information on how i can catch events in child rather than on parent? A bit confused here. I will also dig through docs for this.

jpn
8th October 2006, 10:11
Maybe you should use specialized versions for the small and the large? Do you really want to be able to move canvas items around in the small view too?

quickNitin
8th October 2006, 10:39
not rally but i want a rectangle there which will let me pan to other area in large canvas. and also i want to move this small canvas to any position on large canvas as per my convenience.

jpn
8th October 2006, 11:22
So the behaviour is pretty much different when comparing the small to the large view. Maybe you need to take a break in implementation and give yourself a moment to think what kind of functionalities do you actually need from those both views and how could you possibly be able to implement the required functionalities.

- when the rectangle inside the small view shall be moved (dragging anywhere inside the rect / dragging rect boundaries..)?
- when the small view itself shall be moved (always when not moving the rectangle?)
- how do you connect the views so that the large view adjusts it's world to correspond the small view's rectangle
- and so on...

Remember that a child widget is on top of it's parent. The child (the small view in your case) receives for example mouse events itself, you cannot catch them (without doing certain tricks) in the parent (the large view in your case).