PDA

View Full Version : QGraphicsScene size



coder89
12th May 2011, 11:14
Hello.
I'm trying to create simple drawing program.
I'd like to create view of a sheet of paper on which user could draw and which could be zoomed. The similar effect like in MS Word page view.
I've tried to add QGraphicsView as a main window widget and than add QGraphicsScene with specified Rect. But unfortunately QGraphicsScene always is expanded to the QGraphicsView size. If I draw sth bigger than QGraphicsScene specified area size it always is drawn whole - I want to draw only those part of item which contains in scene area.

Is it possible to get such effect?

PS. QGraphicsView have to be expanded to whole window area because when user is zooming, scene area should growth. So QGraphicsScene should have different size than QGraphicsView.

high_flyer
13th May 2011, 09:06
But unfortunately QGraphicsScene always is expanded to the QGraphicsView size.
You mean that if you resize your view the "white" background "stretches" with it I guess.
But it doesn't mean the scene is the size of the view.

The scene is a logical entity, it has no graphical representation of its own.
I urge you to read the QGraphicScene and QGraphicsView docs thoroughly, I am sure it will answer most of your problems.
Ask here again if not.

coder89
13th May 2011, 13:56
Thank you for your interest!

I've ridden docs and I know about that. Let's say I have QGraphicsScene with 200x200px size. Window size (and QGraphicsView size) is about 1000x1000. Then I draw QGraphicsTextItem with string with at least 1000 chars. and draw it at (0,0) point. Unfortunatelly it is always! drawn on whole window (1000x1000) and it is not cropped to 200x200px. Moreover if I enable dragging of this text item I am able to move them on whole window - not only on 200x200px scene.

Let me show you my code. Maybe I'm doing sth wrong and I can't see it.

Methods of my QGraphicsView class:


Scene::Scene(QWidget *parent) :
QGraphicsView(parent),
m_canvas(0)
{
setupGUI();
}

Scene::Scene(Canvas * canvas, QWidget *parent) :
QGraphicsView(canvas, parent),
m_canvas(canvas)
{
setupGUI();
}

Scene::~Scene()
{
closeCanvas();
}

void Scene::setupGUI()
{
this->setAutoFillBackground(false);
this->viewport()->setAutoFillBackground(false);
}

void Scene::newCanvas(const QRectF & dimension)
{
closeCanvas();
m_canvas = new Canvas(dimension, this);
this->setScene(m_canvas);
}

void Scene::closeCanvas()
{
if (m_canvas)
{
delete m_canvas;
m_canvas = 0;
}
}

void Scene::wheelEvent(QWheelEvent* event)
{
double scaleFactor = 1.05;
if (event->modifiers() & Qt::ControlModifier)
scaleFactor += 0.3;
if(event->delta() < 0)
scaleFactor = 1.0 / scaleFactor;
scale(scaleFactor, scaleFactor);
centerOn( mapToScene(event->pos()) );
}


Metho9ds of QGraphicsScene class:


Canvas::Canvas(const QRectF & dimension, QObject * parent) :
QGraphicsScene(dimension, parent)
{
QGraphicsRectItem * background = new QGraphicsRectItem(dimension,0,this);
QGraphicsDropShadowEffect * effect = new QGraphicsDropShadowEffect();
effect->setBlurRadius(20);
effect->setOffset(5);
background->setGraphicsEffect(effect);
background->setBrush(Qt::white);
QGraphicsTextItem * text = new QGraphicsTextItem("hudskjgifv j",0,this);
text->setFlag(QGraphicsItem::ItemIsMovable, true);
}

void Canvas::drawBackground(QPainter * painter, const QRectF & rect)
{
QGraphicsScene::drawBackground(painter, rect.intersect(this->sceneRect()));
}

void Canvas::drawForeground(QPainter * painter, const QRectF & rect)
{
QGraphicsScene::drawForeground(painter, rect.intersect(this->sceneRect()));
}


And at last here is the code where I create my window:


KXmlGuiWindow * window = new KXmlGuiWindow();
Scene * m_scene = new Scene(window);
m_scene->newCanvas(QRectF(0, 0, 200.0, 200.0));
window->setCentralWidget(m_scene);
window->show();

coder89
15th May 2011, 11:02
Could anyone help me to solve this problem?
Thank you in advance :)

Santosh Reddy
26th June 2011, 10:03
If you are still looking for a solution, It may not be it is not possible to do directly, there is an alternate way (and I guess, QGraphicsScene is designed to be used this way)

Size of the QGraphicsScene is by default unlimited, so set the required size, using QGraphicsScene::setSceneRect(), and QGraphicsView, will ensure to show all the size (it can show more than the set size, if the containing widget is larger). Setting the scene rectangle will not restrict the items being drawn outside the scene rectangle, this is because item draw them self and scene does not draw them, so items have to get the scene rectangle first and them draw themselves with in the scene rectangle.

In your case, as you posted of using large text which may be drawn beyond the scene rectangle, you should first get the scene rectangle, and set restriction to draw wrapped test with in this rectangle.