PDA

View Full Version : QGraphicsLayout::geometry()



isutruk
19th October 2009, 02:53
Hi all, I have a small issue about QGraphicsLayout. Hope, you can show me the workaround or ma mistake here.

Code snaps:

Document::Document()
: QGraphicsWidget(0,Qt::Window)
{
QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical);
layout->setContentsMargins(10,5,10,5);
setLayout(layout);
layout->addItem(new Paper(this));
}


Paper::Paper(Document *doc, QPrinter::PaperSize paperSize, QPrinter::Orientation orientation)
:
ContentFrame(Qt::Vertical)
{
setParentLayoutItem(doc->layout());
setParentItem(doc);
QSizeF sizeF = printerPaperSize(orientation, paperSize, QPrinter::DevicePixel, Suzidil::DPI);
setMinimumSize(sizeF);

setBrush(QBrush(QColor(255, 255, 255, 125)));
setContentsMargins(30, 50, 30, 40);
setName("paper");

Page *p = new Page(this);
addItem(p);
}


Page::Page(Paper *parent)
: ContentFrame(Qt::Vertical, ContentFrame::ResizeAll | ContentFrame::Interactive, parent)
{
setName("page");
setMinimumSize(100,100);
setBrush(QBrush(QColor(255, 255, 255, 125)));
}



ContentFrame::ContentFrame(Qt::Orientation orientation,
ResizeOptions options,
ContentFrame *parent)
:
QGraphicsLinearLayout(orientation),
QGraphicsRectItem(parent)
{
cf_resizeOptions = options;
if (!(options & NotResizable) && options & Interactive)
setAcceptHoverEvents(true);
setPen(QPen(Qt::NoPen));
setGraphicsItem(this);
setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
}

as u can see ContentFrame inherits both of QGraphicsLinearLayout and QGraphicsRectItem. And i want rectItem has the same geometry with the layout. So I've tried the function below.


void ContentFrame::setGeometry(const QRectF &rect)
{
QGraphicsLinearLayout::setGeometry(rect);
QRectF rect2 = geometry();
QGraphicsRectItem::setPos(rect2.topLeft());
QGraphicsRectItem::setRect(0,0,rect2.width(),rect2 .height());
}

topLeft is the pos of the layout in parent's coordinates. see (http://doc.trolltech.com/4.5/qgraphicslayoutitem.html#setGeometry)

I'm using A4 size for paper. (669.685 947.126)
Document's contentsMargin is (10,5,10,5)
Paper's contentsMargin is (30, 50, 30, 40)

Expected result is:
"page" geometry 30 50 609.685 847.126 // left top width height
"page" rect 0 0 609.685 847.126
"page" pos 30 50

"paper" geometry 10 5 669.685 947.126
"paper" rect 0 0 669.685 947.126
"paper" pos 10 5

Current result is:
"page" geometry 40 55 609.685 857.126
"page" rect 0 0 609.685 857.126
"page" pos 40 55

"paper" geometry 10 5 669.685 947.126
"paper" rect 0 0 669.685 947.126
"paper" pos 10 5

As I can see, Qt's layoutengine sends a rectangle to my page which is not in paper's (parent of page) coord-sys, but document's.

How can I achieve this problem, any idea? thanks.

isutruk
19th October 2009, 16:43
I found my mistake, changed setGeometry function like this:

void ContentFrame::setGeometry(const QRectF &rect)
{
QGraphicsLinearLayout::setGeometry(rect);
QGraphicsRectItem::setRect(geometry());
}

and when I put an item inside the page, for example at pos (0,0) It is corner of Document, but It looks I should consider topLeft of ContentFrame when putting an item. Solved. Thanks.