View Full Version : Sublassing QGraphicsLayoutItem
onurozcelik
3rd May 2010, 22:10
How exactly can I subclass QGraphicsLayoutItem?
I write a class that subclass QGraphicsLayoutItem and reimplement sizeHint and setGeometry but when I add my custom item to linear or grid layout. It does not shown?
What is missing?
SixDegrees
3rd May 2010, 23:01
QGraphicsItem is not a QWidget; it's meant to be displayed when it is placed into a QGraphicsScene which is then viewed with a QGraphicsView. It won't show up in a QLayout. In fact, I'm surprised you're able to add it in the first place.
A QGraphicsView descends from QWidget, and can be added to a QLayout.
onurozcelik
4th May 2010, 07:20
I think you got me wrong. Cause there are existing layouts for QGraphicsView.
Look at these QGraphicsLinearLayout, QGraphicsGridLayout
SixDegrees
4th May 2010, 08:48
In your original post, you were talking about a QGraphicsItem, not a QGraphicsView.
Maybe you could post a short example that demonstrates the problem?
onurozcelik
4th May 2010, 10:23
Here my trial.
//compositeitem.h
class CompositeItem : public QGraphicsLayoutItem,public QGraphicsItemGroup
{
CompositeItem(QString id, QList<QGraphicsWidget *> children);
~CompositeItem();
QSizeF sizeHint() const;
void setGeometry(const QRectF &rect);
};
//compositeitem.cpp
CompositeItem::CompositeItem(QString id,QList<QGraphicsWidget *> children)
{
QGraphicsWidget *child;
foreach(child,children)
{
addToGroup(child);
}
}
CompositeItem::~CompositeItem()
{
}
QSizeF CompositeItem::sizeHint()
{
...
}
void CompositeItem:: setGeometry(const QRectF &rect)
{
QGraphicsLayoutItem::setGeometry(rect);
}
//mainwindow.cpp
QGraphicsScene *scene = new QGraphicsScene;
ui.graphicsView->setScene(scene);
QGraphicsLinearLayout *layout = new QGraphicsLinearLayout;
CompositeItem * ci = new CompositeItem(...);
layout->addItem(ci);
QGraphicsWidget *container = new QGraphicsWidget;
container->setLayout(layout);
scene->addItem(container);
Here i do not see my compositeitem drawing on scene.
Did you solve your problem?
SixDegrees
19th May 2010, 08:45
What happens if you add your CompositeItem directly to the scene?
And what is it you expect to see? I don't see any paint() routine or other functions that will cause anything to be displayed.
onurozcelik
24th May 2010, 08:37
@GuL I don't solve my problem with this approach. But I write a custom composite item class that acts like a QGraphicsItemGroup, also can be added inside QGraphicsLayout.
@SixDegrees if I add my composite item directly to scene I see empty scene. There is no paint because I expect to see child items paints themselves.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.