PDA

View Full Version : QGraphicsscene and QGraphicsview



ReasyEasyPeasy
4th October 2015, 21:40
Hey guys,
my Code:

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

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

ui->setupUi(this);

scene = new QGraphicsScene();
this->ui->graphicsView->setScene(scene);
this->ui->graphicsView->setRenderHint(QPainter::Antialiasing);

}

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

#ifndef ANALYSE_H
#define ANALYSE_H

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

class Analyse : public QWidget
{
Q_OBJECT
public:
QGraphicsScene *scene;
explicit Analyse(QWidget *parent = 0);
~Analyse();
private:
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.
Greets

d_stranz
5th October 2015, 01: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.

ReasyEasyPeasy
5th October 2015, 22: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?
Greets

d_stranz
6th October 2015, 15: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.