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));
}
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));
}
To copy to clipboard, switch view to plain text mode
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);
setContentsMargins(30, 50, 30, 40);
setName("paper");
Page *p = new Page(this);
addItem(p);
}
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);
}
To copy to clipboard, switch view to plain text mode
Page::Page(Paper *parent)
: ContentFrame(Qt::Vertical, ContentFrame::ResizeAll | ContentFrame::Interactive, parent)
{
setName("page");
setMinimumSize(100,100);
}
Page::Page(Paper *parent)
: ContentFrame(Qt::Vertical, ContentFrame::ResizeAll | ContentFrame::Interactive, parent)
{
setName("page");
setMinimumSize(100,100);
setBrush(QBrush(QColor(255, 255, 255, 125)));
}
To copy to clipboard, switch view to plain text mode
ContentFrame::ContentFrame(Qt::Orientation orientation,
ResizeOptions options,
ContentFrame *parent)
:
QGraphicsLinearLayout(orientation),
{
cf_resizeOptions = options;
if (!(options & NotResizable) && options & Interactive)
setAcceptHoverEvents(true);
setGraphicsItem(this);
}
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));
}
To copy to clipboard, switch view to plain text mode
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);
}
void ContentFrame::setGeometry(const QRectF &rect)
{
QGraphicsLinearLayout::setGeometry(rect);
QRectF rect2 = geometry();
QGraphicsRectItem::setPos(rect2.topLeft());
QGraphicsRectItem::setRect(0,0,rect2.width(),rect2.height());
}
To copy to clipboard, switch view to plain text mode
topLeft is the pos of the layout in parent's coordinates. see
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.
Bookmarks