View Full Version : QGraphicsscene and QGraphicsview

4th October 2015, 22:40
Hey guys,
my Code:

#include "../header/analyse.h"
#include "ui_analyse.h"
#include <QDebug>

Analyse::Analyse(QWidget *parent) :
ui(new Ui::Analyse)


scene = new QGraphicsScene();


delete ui;

#ifndef ANALYSE_H
#define ANALYSE_H

#include <QWidget>
#include <QPushButton>
#include <QGraphicsLineItem>
#include <QStringList>
namespace Ui {
class Analyse;

class Analyse : public QWidget
QGraphicsScene *scene;
explicit Analyse(QWidget *parent = 0);
Ui::Analyse *ui;
#endif // ANALYSE_H

The user can add moveable Items to the scene. I want that you can only move Items through the right border. I got two problems:
The borders one the left, top and bottom site. If you try to move an item through this borders it stops.(colide)
If you move items to the right border of the scene scroll bars should appear. and the scene should extend.
A scene like this :
| ->->->->->->->->->->->->->->->->
I tried a lot of things and looked around. I would be happy if someone can help me.

5th October 2015, 02:55
First point - this is a memory leak. Create it with "this" as a parent so it is deleted when your Analyse widget instance goes out of scope.

scene = new QGraphicsScene();

It would be easier to accomplish what you want is you make your scene bigger than your graphics view, and set the view's sceneRect() to the size you need. As the user drags an item to the right, you can enlarge the scene rect as needed.

5th October 2015, 23:17
Hey thanks for this fast answer!
the size of my QGraphicsview get changed if you change the window size. How can i deal with this?
And if I use your settings you can still move an item through the border. Is there an easy way to fix this?

6th October 2015, 16:58
You will probably have to implement a handler for QGraphicsItem::itemChange(). The example code at the doc link does almost exactly what you need in the case where the item is about to move across a forbidden boundary. Also look at the Qt "elasticnodes" example.