PDA

View Full Version : Find the Scene dimention from inside a QGraphicsItem



0backbone0
26th August 2015, 14:37
Hi,

I am trying to create a custom GraphicsItem that will set its width autocratically to the Scene width.

sceneBoundingRect() sounds like what I need, but my Program crashes all the time.



#include "customparentitem.h"
#include <QDebug>

customparentitem::customparentitem()
{
setFlag(ItemIsMovable);
}

QRectF customparentitem::boundingRect() const
{
qDebug() << this->sceneBoundingRect();
return QRectF(0,0,200,30);
}

void customparentitem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QRectF temprec=boundingRect();
painter->drawRoundedRect(temprec.x(),temprec.y(),temprec.wi dth(),temprec.height(),8,8);
}



The returned BoundingRect is just temporary until I get the desired output with qDebug().



#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
scene(new QGraphicsScene),
customitem(new customparentitem)
{
ui->setupUi(this);

ui->graphicsView->setViewportUpdateMode(QGraphicsView::FullViewportU pdate);
ui->graphicsView->setRenderHint(QPainter::Antialiasing);
scene->setSceneRect(ui->graphicsView->rect());

scene->addItem(customitem);

ui->graphicsView->setScene(scene);
}

MainWindow::~MainWindow()
{
delete ui;
}


This compiles just fine, but as said earlier crashes. Any ideas why?

anda_skoa
26th August 2015, 18:44
Maybe you did not set the scene rect.
In which case it will be determined by getting the union of all item bounding rects, which in your case would likely lead to a infinte cascading recursion.

Cheers,
_

0backbone0
26th August 2015, 19:09
I want the Scenerect to be the same size as the graphicsView its sitting in.
Set the Scenerect this way:

scene->setSceneRect(ui->graphicsView->rect());

I will test out if it still crashes if I set the Scenerect manualy.

Edit: Just tested it, no change. The program compiles, starts and crashes immediately. :(

d_stranz
26th August 2015, 20:57
The main window and all sub-widgets within it do not have any size until after the showEvent. So the setup you are trying to do in the main window constructor isn't going to result in anything useful. Implement showEvent() for your main window class and do the size determination there.

You'll probably also want to implement resizeEvent() too if you want the graphics item to change size as the view changes.

But then you could simply do all of this with a call to QGraphicsView::fitInView() with your top-level item as the argument.

0backbone0
26th August 2015, 23:10
Just found out that SceneBoundingRect just uses the BoundingRect of the Item and map it to Scene Coordinates.
I tried to use this to view a Boundigrect (even use it to calculate one) that doesn’t exist jet,witch obviously can't work.

I think the simplest workaround is to put a Slot to Mainwindow that updates the BoundingRect of the Graphicsitem.
Like this:

connect(scene,SIGNAL(sceneRectChanged(QRectF)),thi s,SLOT(setitemwidth(QRectF));

And QGraphicsView::fitInView() just updates the Scene, not the Items in it. Thus it isn't much use to me in this situation.