Hello,
the following code draws 3 ellipses and one rectangle.
These 4 object are grouped in a QGraphicsItemGroup together.
What i want to do is to implement 2 states:
First state:
If the rectangles moves, the ellipses move too, like the code shows.
Second state:
This is where i do not find a solution.
I need a state where I make the ellipses moveable inside the rectangle , for example with a double click on the rectangle.
And if i click back outsite the rectangle, the ellipses fix back to the rectangle. (first state).
How i could do this?
Thx Andre
mainwindow.h
#include <QMainWindow>
#include "customitemellipse.h"
#include "customitemrect.h"
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#include <QMainWindow>
#include "customitemellipse.h"
#include "customitemrect.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QGraphicsScene *scene;
QGraphicsItemGroup *group;
};
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->graphicsView->setScene(scene);
group = scene->createItemGroup(scene->selectedItems());
CustomItemEllipse *ellipse1 = new CustomItemEllipse();
scene->addItem(ellipse1);
group->addToGroup(ellipse1);
CustomItemEllipse *ellipse2 = new CustomItemEllipse();
ellipse2
->setPos
(mapToParent
(QPoint(300,
0)));
scene->addItem(ellipse2);
group->addToGroup(ellipse2);
CustomItemEllipse *ellipse3 = new CustomItemEllipse();
ellipse3
->setPos
(mapToParent
(QPoint(0,
300)));
scene->addItem(ellipse3);
group->addToGroup(ellipse3);
CustomItemRect *rect = new CustomItemRect();
scene->addItem(rect);
group->addToGroup(rect);
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
group = scene->createItemGroup(scene->selectedItems());
group->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
CustomItemEllipse *ellipse1 = new CustomItemEllipse();
scene->addItem(ellipse1);
group->addToGroup(ellipse1);
CustomItemEllipse *ellipse2 = new CustomItemEllipse();
ellipse2->setPos(mapToParent(QPoint(300,0)));
scene->addItem(ellipse2);
group->addToGroup(ellipse2);
CustomItemEllipse *ellipse3 = new CustomItemEllipse();
ellipse3->setPos(mapToParent(QPoint(0,300)));
scene->addItem(ellipse3);
group->addToGroup(ellipse3);
CustomItemRect *rect = new CustomItemRect();
scene->addItem(rect);
group->addToGroup(rect);
}
To copy to clipboard, switch view to plain text mode
customitemellipse.h
#include <QGraphicsItem>
#include <QPainter>
{
public:
public:
CustomItemEllipse();
};
#include <QGraphicsItem>
#include <QPainter>
class CustomItemEllipse : public QGraphicsItem
{
public:
QRectF ellipse;
public:
CustomItemEllipse();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
};
To copy to clipboard, switch view to plain text mode
customitemellipse.cpp
#include "customitemellipse.h"
CustomItemEllipse::CustomItemEllipse()
{
ellipse = boundingRect();
setFlag(ItemIsMovable);
setZValue(10);
}
QRectF CustomItemEllipse
::boundingRect() const {
}
{
painter->setBrush(Qt::gray);
painter->setPen(pen);
painter->drawEllipse(ellipse);
}
#include "customitemellipse.h"
CustomItemEllipse::CustomItemEllipse()
{
ellipse = boundingRect();
setFlag(ItemIsMovable);
setZValue(10);
}
QRectF CustomItemEllipse::boundingRect() const
{
return QRectF(0,0,75,75);
}
void CustomItemEllipse::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setBrush(Qt::gray);
QPen pen(Qt::black, 3);
painter->setPen(pen);
painter->drawEllipse(ellipse);
}
To copy to clipboard, switch view to plain text mode
customitemrect.h
#include <QGraphicsItem>
#include <QPainter>
{
public:
public:
CustomItemRect();
};
#include <QGraphicsItem>
#include <QPainter>
class CustomItemRect : public QGraphicsItem
{
public:
QRectF rect;
public:
CustomItemRect();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
};
To copy to clipboard, switch view to plain text mode
customitemrect.cpp
#include "customitemrect.h"
CustomItemRect::CustomItemRect()
{
rect = boundingRect();
setFlag(ItemIsMovable);
setScale(5);
}
QRectF CustomItemRect
::boundingRect() const {
}
{
painter->setBrush(Qt::white);
QPen pen
(Qt
::black,
0.5);
painter->setPen(pen);
painter->drawRoundRect(rect);
}
#include "customitemrect.h"
CustomItemRect::CustomItemRect()
{
rect = boundingRect();
setFlag(ItemIsMovable);
setScale(5);
}
QRectF CustomItemRect::boundingRect() const
{
return QRectF(0,0,75,75);
}
void CustomItemRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setBrush(Qt::white);
QPen pen(Qt::black, 0.5);
painter->setPen(pen);
painter->drawRoundRect(rect);
}
To copy to clipboard, switch view to plain text mode
Bookmarks