PDA

View Full Version : How to resize a layout item?



lni
21st February 2009, 20:31
Hi,

I am trying to force a specific size on a layout item as shown (400x400), but it doesn't work. Is there a way for it?

Thanks



#include <QtGui>

class LayoutItem : public QGraphicsWidget {
public:
LayoutItem(QGraphicsItem *parent = 0) : QGraphicsWidget( parent ) {}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget = 0) {

qDebug() << "LayoutItem::paint, size = " << size();
QRectF frame(QPointF(0,0), geometry().size());
painter->drawRect( frame );
}

};

class Window : public QGraphicsWidget {
public:
Window(QGraphicsWidget *parent = 0) {

QGraphicsGridLayout *grid = new QGraphicsGridLayout( this );
LayoutItem* item = new LayoutItem;
grid->addItem(item, 0, 0, 1, 1);
item->resize( 400, 400 );
}
};

int main(int argc, char **argv)
{
QApplication app(argc, argv);

QGraphicsScene scene;

Window *window = new Window;
scene.addItem(window);
QGraphicsView view(&scene);
view.resize(500, 500);
view.show();

return app.exec();
}

talk2amulya
21st February 2009, 21:26
try resizing the item before you add it to the layout

lni
21st February 2009, 22:12
try resizing the item before you add it to the layout

It doesn't work either...

Lykurg
22nd February 2009, 08:53
Hi,

you have to use option->rect and implement the pure virtual function boundingRect()!


class LayoutItem : public QGraphicsWidget {
public:
LayoutItem(QGraphicsItem *parent = 0) : QGraphicsWidget( parent ) {}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget = 0) {

qDebug() << "LayoutItem::paint, size = " << option->rect;
painter->drawRect( option->rect );
}
QRectF boundingRect() const
{
return QRectF(0,0,400.0,400.0);
}

};

class Window : public QGraphicsWidget {
public:
Window(QGraphicsWidget *parent = 0)
{
QGraphicsGridLayout *grid = new QGraphicsGridLayout( this );
LayoutItem* item = new LayoutItem;
grid->addItem(item, 0, 0, 1, 1);
}
};