PDA

View Full Version : QGraphicsWidgets on QGraphicsGridLayout



onurozcelik
1st April 2010, 13:11
Hi

I subclass QGraphicsWidget and override boundingRect and paint methods.

Here is what I did shortly


CustomItem :: CustomItem()
{
pen = QPen(QBrush(Qt::gray),5,Qt::SolidLine,Qt::RoundCap ,Qt::BevelJoin);
}
CustomItem :: ~CustomItem()
{
}

QRectF CustomItem::boundingRect() const
{
return QRectF(0,0,102,5);
}

void CustomItem::paint(QPainter *painter,...)
{
painter->setPen(pen);
painter->drawLine(0,0,100,0);
}


And I added my CustomItem(s) to QGraphicsGridLayout


For this I used


QGraphicsScene *scene = new QGraphicsScene;
QGraphicsGridLayout *grid = new QGraphicsGridLayout;

CustomItem *custom1 = new CustomItem;
CustomItem *custom2 = new CustomItem;

grid->addItem(custom1,0,1);
grid->addItem(custom2,0,2);

QGraphicsWidget *container = new QGraphicsWidget;
container->setLayout(grid);

scene->addItem(container);


When I do this
Two custom items had painted on each other(I guess because of paint draws them on same coordinates)
But I want them to paint one after another, I assume grid layout does it for me but it does not

How can I overcome this?

Note: I use QGraphicsWidget class instead of QGraphicsItem or QGraphicsLineItem because I think I may easily
create my user interface without supplying coordinates for each item.

axeljaeger
3rd April 2010, 16:00
I think subclassing boundingRect is not enough. See the documentation for QLayoutItem.

onurozcelik
3rd April 2010, 17:14
Thanks for your reply

I solved the problem
İf you want to add custom item to QGraphicsGridLayout you should give a fixed row or column size with
setFixedColumnWidth/setFixedRowHeight

axeljaeger
3rd April 2010, 17:17
That sounds like a short term solution and not the ultimate solution that works also when you have two items in a rows / column that want to have a different size.

onurozcelik
4th April 2010, 12:38
That sounds like a short term solution and not the ultimate solution that works also when you have two items in a rows / column that want to have a different size.

You may be right. In a situation you mention in your reply we may use QGraphicsItemGroup or something like that.