Qt4-preview-feedback Archive, January 2008
QGraphicsLayout fun
Message 1 in thread
Hi,
I got a few problems using the GraphicsView layout system. I'm trying to
use it now instead of my handmade solutions I had before. (One reason this
would be nice for QGraphicsItems as well !!!)
Given the following testcode: (sorry it's a bit verbose, but should show a
bit what I was trying to get at)
//a widget being a layout
QGraphicsWidget* lo = new QGraphicsWidget;
QGraphicsGridLayout* l = new QGraphicsGridLayout;
lo->setLayout(l);
aScene->addItem(lo);
//a red box widget being put into slot 0,0 of grid layout
QGraphicsWidget* w1 = new QGraphicsWidget;
w1->setZValue(10);
w1->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed,
QSizePolicy::DefaultType);
QGraphicsRectItem* r1 = new QGraphicsRectItem(0, 0, 100, 100, w1);
r1->setBrush(Qt::red);
QSizeF s1 = r1->boundingRect().size();
w1->resize(s1);
w1->setMinimumSize(s1);
l->addItem(w1, 0, 0);
//and a blue box, put to 0,1 besides the red box
QGraphicsWidget* w2 = new QGraphicsWidget;
w2->setZValue(20);
w2->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed,
QSizePolicy::DefaultType);
QGraphicsRectItem* r2 = new QGraphicsRectItem(0, 0, 100, 100, w2);
r2->setBrush(Qt::blue);
QSizeF s2 = r2->boundingRect().size();
w2->resize(s2);
w2->setMinimumSize(s2);
l->addItem(w2, 0, 1);
//and another higher yellow box in slot 3 (0,2)
QGraphicsWidget* w3 = new QGraphicsWidget;
w3->setZValue(30);
w3->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed,
QSizePolicy::DefaultType);
QGraphicsRectItem* r3 = new QGraphicsRectItem(0, 0, 100, 200, w3);
r3->setBrush(Qt::yellow);
QSizeF s3 = r3->boundingRect().size();
w3->resize(s3);
w3->setMinimumSize(s3);
l->addItem(w3, 0, 2);
//this rectangle is supposed to show the boundaries (in various ways -
//here only shown one) of the layout container (lo), respectively the
//layout l
QGraphicsRectItem* rl2 =
new QGraphicsRectItem(lo->childrenBoundingRect(), lo);
rl2->setZValue(100);
rl2->setBrush(Qt::white);
//and this loop was just supposed to find out, why
//the rectangle rl2 has just the size of w3 (because
//all widgets are virtually on each other)
for(int i = 0; i < l->count(); i++){
QGraphicsLayoutItem* item = l->itemAt(i);
QGraphicsWidget* w = (QGraphicsWidget*)item;
qDebug() << l->contentsRect() << w->transform();
}
I just wanted to use QGraphicsWidgets or a
minimally implemented subclass of it in order to use the layout mechanism.
From the docs it's not 100% clear what exactly happends if widgets are put
into a layout. It's written there that setGeometry gets called and the
like and it all makes kind of sense, but I'm not able find out how to get
the simple behavior I would need right now. Actually what I would like to
do, is draw some QGraphicsItems in a size that fits me. Fix that size for
the widget (QSizePolicy::Fixed), put it into a layout. But let the layout
adjust to the size of the widgets it contains, as they are fixed size. Get
that size and adjust the size of the container itself, so it can be used
in other containers again (by hand or via some subclassing and
interception of simple uses). I know that's not the way it is supposed to
be (actually kind of reverse), but would be ok for my use right now. But
anyway, first there is the problem that the layout seams to magically put
the layoutitems to their positions, without any trace (none I could find
at least), aka, being able to use the childrenBoundingRect() or something
a long that line. For some reasons in the one case I use it (not the above
example) it even looks like the arranged items fit, but I don't know why.
Somehow a QGraphicsLayout gets the real size of a sublayout from somewhere.
I checked the Qt-source and at least it's not obvious to me how it
works. There is the Layout-code from the normal widgets is being used, but
shouldn't the repositioning be visible in the QGraphicsItem objects
as well ?
The correct way would be the following ?:
A widget gets send a setGeometry message (actually
didn't observe this in my test-subclass of QGraphicsWidget). Then the
widget has the chance to transform itself (for instance just scale) to the
rect it has been given in the message so it fits into the new space ?
Any elucidation is welcome :-)
Michael
To unsubscribe - send "unsubscribe" in the subject to qt4-preview-feedback-request@xxxxxxxxxxxxx
Message 2 in thread
Michael Berg wrote:
> Any elucidation is welcome :-)
The first question that pops to my mind is - have you tried this with
regular widgets, and found the behavior to be different?
I'm not sure that I grasp entirely what you want to do, especially the part
about how you want the widget sizes to be fixed, but you still want the
layouts to resize them.
--
[ signature omitted ]
Message 3 in thread
> The first question that pops to my mind is - have you tried this with
> regular widgets, and found the behavior to be different?
No, I haven't but it seams that one has to follow quite carefully the way
the layout-systems is supposed to be used and then it works.
> I'm not sure that I grasp entirely what you want to do, especially the part
> about how you want the widget sizes to be fixed, but you still want the
> layouts to resize them.
I tried to do quite some things in a pretty awkward way and this caused
already too much mistakes on my side, so it's hard to explain what was a
warranted question and what was just my wrong understanding.
I wrote already a few versions of this reply and thanks to thinking about
the problems more slowly (and checking the QT-source), so far everything
is now resolved. :-) For instance one of my problems of the first post was
that I couldn't get the position and size of widgets which just had been
added to a layout. This was necessary to find out, in order to do my
awkward approach to (mis)use the layout-system for my purposes. But just a
few minutes ago I finally understood (and tested) that this never worked
because upon an "invalidate()" of the layout (for instance after adding
widgets to the layout) a "LayoutRequest" event is being posted. But this
request has been processed only after I checked the new pos() and size()
of the widgets (when control returns to the event-loop).
Thanks for your help.
Michael
To unsubscribe - send "unsubscribe" in the subject to qt4-preview-feedback-request@xxxxxxxxxxxxx