PDA

View Full Version : QGraphicsItem with QSizeGrip



elsheikhmh
7th April 2007, 16:27
Hi,

I'm developing a basic flowchart drawing application. I decided to use Graphics View Framework to take the advantages like scrolling, selection.
Now I'm stuck on how to let the user resize QGraphicsItem? I see in programs like "dia" once the item (say for simplicity a rectangle) is selected, a four corners appear and user can drag one of them to resize.
How can I implement a similar functionality? I have my own subclassed QGraphicsItem. I tried to used QSizeGrip with it, but I'm lost.

I appreciate your advice too much.

Thanks,
-Mustafa

marcel
7th April 2007, 18:55
QSizeGrip is used only to resize widgets( main windows ). It is not very easy what you want to do but I'll try to describe what you have to do.

To obtain the desired functionality you will have to add a new state to your graphics item ( the first one being when the item is dragged around with the mouse ). In this new state, let's call it ResizeState, you should draw on top of your item a resize grip ( which should be your own custom QGraphicsItem ). The detect when you drag the resize grip corners.

To detect dragging of a QGraphicsItem ( or a portion of it ) you can use any number of methods. One would be to reimplement the mousePressEvent, mouseMoveEvent and mouseReleaseEvent. When you are in ResizeState you detect dragging and resize your object and the resize grip accordingly.

If you have any other questions, please ask.

Marcel.

wysota
7th April 2007, 21:01
Use QStyle::drawControl() with QStyle::CE_SizeGrip to draw a size grip on your item.

elsheikhmh
8th April 2007, 11:52
hi,
@wysota: i tried QSytle::drawControl() in my QGraphicsItem subclass's paint():


void MyCustomQGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget) {
QCommonStyle s;
s.drawControl(QStyle::CE_SizeGrip, item, painter);
...

I realize that SizeGrip is not exactly what I want.

@marcel: you mean I'll have to implement my own resize grip as a custom QGraphicsItem, and place it on top of MyCustomQGraphicsItem.

I need a functionality similar to this
http://elsheikhmh.googlepages.com/Capture_193.gif
Now, for MySizeGripQGraphicsItem, I'll do paint a four (or six) corners. And show the grip on top of MyCustomQGraphicsItem if isSelected().
Now, for MySizeGripQGraphicsItem, how can I detect dragging a portion of QGraphicsItem namely the corners via mouseEvent's?

Thanks,
- Mustafa

marcel
8th April 2007, 16:42
Now, for MySizeGripQGraphicsItem, how can I detect dragging a portion of QGraphicsItem namely the corners via mouseEvent's?
You will just have to test if the user drags those corners. To see this is easy, because you know the position of your MySizeGripQGraphicsItem ( the resize handles are at the corners of the item ).
Then you can translate QMouseEvent::pos() (from mouseMoveEvent ) to parent ( graphics scene ) coordinates and resize the items.

To know in mouseMoveEvent exactly what corner is dragged, you could use a convenience enum, like:



typedef enum EResizeHandle
{
eNone,
eHandleTopLeft,
eHandleTop,
eHandleTopRight,
eHandleRight,
eHandleBottomRight,
...
} EResizeHandle;

Assuming you have a member of type EResizeHandle:


void Class::mousePressEvent( QMouseEvent *e )
{
//
// Look at e->pos() to establish which corner is dragged and assign a value to
// mResizeHandle
//
}

void Class::mouseMoveEvent( QMouseEvent *e )
{
// if mResizeHandle is eNone then nothing is resized
switch( mResizeHandle )
{
case eTopLeft:
// code to resize the object
break;
...
}
}

void Class::mouseReleaseEvent( ... )
{
mResizeHandle = eNone;
}

Gopala Krishna
8th April 2007, 17:36
@elsheikhmh:

I don't know whether this fits for your purpose but still I feel this link (http://labs.trolltech.com/blogs/2006/12/15/interact-with-volatile-graphics-items/) along with above advices might help you a bit :)

elsheikhmh
10th April 2007, 19:52
@Gopala: thanx!

matko
19th November 2009, 18:53
hi, link forward to the site qt.labs, but the example doesn`t work, someone have this file? thx http://www.andreas.hanssen.name/interactive.tar.bz2
http://labs.trolltech.com/blogs/category/labs/graphics_view/graphics_items/page/2/