now it runs:
Thx for your patience anda_skoa
Here ist the full code:
dialog.h
#include <QDialog>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsItem>
#include <QDebug>
#include "customitem.h"
namespace Ui {
class Dialog;
}
{
Q_OBJECT
public:
explicit Dialog
(QWidget *parent
= 0);
~Dialog();
private:
Ui::Dialog *ui;
customItem *ellipse1;
customItem *ellipse2;
};
#include <QDialog>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsItem>
#include <QDebug>
#include "customitem.h"
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
QGraphicsScene *scene;
customItem *ellipse1;
customItem *ellipse2;
QGraphicsLineItem *line;
};
To copy to clipboard, switch view to plain text mode
dialog.cpp
ui(new Ui::Dialog)
{
ui->setupUi(this);
ui->graphicsView->setScene(scene);
QPen outlinePen
(Qt
::black);
outlinePen.setWidth(2);
ellipse1 = new customItem();
ellipse2 = new customItem();
ellipse1->scenePos().y() + ellipse1->mCenter.y(),
ellipse2->scenePos().x() + ellipse2->mCenter.x(),
ellipse2->scenePos().y() + ellipse2->mCenter.y());
ellipse1->setLine(line,false);
ellipse2->setLine(line, true);
scene->addItem(ellipse1);
scene->addItem(ellipse2);
scene->addItem(line);
}
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
QBrush grayBrush(Qt::gray);
QPen outlinePen(Qt::black);
outlinePen.setWidth(2);
ellipse1 = new customItem();
ellipse2 = new customItem();
line = new QGraphicsLineItem(ellipse1->scenePos().x() + ellipse1->mCenter.x(),
ellipse1->scenePos().y() + ellipse1->mCenter.y(),
ellipse2->scenePos().x() + ellipse2->mCenter.x(),
ellipse2->scenePos().y() + ellipse2->mCenter.y());
ellipse1->setLine(line,false);
ellipse2->setLine(line, true);
scene->addItem(ellipse1);
scene->addItem(ellipse2);
scene->addItem(line);
}
To copy to clipboard, switch view to plain text mode
cusomItem.h
#include <QPen>
#include <QGraphicsItem>
#include <QPainter>
#include <QDebug>
{
public:
customItem();
qreal getXPosEllipse();
qreal getYPosEllipse();
public:
bool Pressed;
bool startOrEnd;
};
#include <QPen>
#include <QGraphicsItem>
#include <QPainter>
#include <QDebug>
class customItem : public QGraphicsItem
{
public:
customItem();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void setLine(QGraphicsLineItem *newLine, bool side);
qreal getXPosEllipse();
qreal getYPosEllipse();
public:
bool Pressed;
bool startOrEnd;
QGraphicsLineItem *customLine;
QPointF mCenter;
};
To copy to clipboard, switch view to plain text mode
customItem.cpp
#include "customitem.h"
customItem::customItem()
{
Pressed = false;
setFlag(ItemIsMovable);
}
QRectF customItem
::boundingRect() const {
// outer most edges
}
{
QRectF ellipse
= boundingRect
();
mCenter = ellipse.center();
//qDebug() << "mCenter: " << mCenter;
if(Pressed)
{
painter->setPen(pen);
painter->drawEllipse(ellipse);
}
else
{
painter->setPen(pen);
painter->drawEllipse(ellipse);
}
}
qreal customItem::getXPosEllipse()
{
return this->scenePos().x();
}
qreal customItem::getYPosEllipse()
{
return this->scenePos().y();
}
{
Pressed = true;
update();
}
{
Pressed = false;
if(startOrEnd == false)
{
customLine->setLine( this->scenePos().x() + mCenter.x(),this->scenePos().y()+ mCenter.y(),
customLine->line().x2(), customLine->line().y2() );
}
if(startOrEnd == true)
{
customLine->setLine( customLine->line().x1(), customLine->line().y1(),
this->scenePos().x() + mCenter.x(),this->scenePos().y()+ mCenter.y());
}
update();
}
{
qDebug() << "-------------------------------------------";
qDebug() << "&customLine:\t" << &customLine << "\t&newLine:\t" << &newLine;
qDebug() << "customLine:\t" << customLine << "\tnewLine:\t" << newLine;
customLine = newLine;
startOrEnd = side;
qDebug() << "customLine:\t" << customLine << "\tnewLine:\t" << newLine;
}
#include "customitem.h"
customItem::customItem()
{
Pressed = false;
setFlag(ItemIsMovable);
}
QRectF customItem::boundingRect() const
{
// outer most edges
return QRectF(0,0,100,100);
}
void customItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QRectF ellipse = boundingRect();
mCenter = ellipse.center();
//qDebug() << "mCenter: " << mCenter;
if(Pressed)
{
QPen pen(Qt::red, 3);
painter->setPen(pen);
painter->drawEllipse(ellipse);
}
else
{
QPen pen(Qt::black, 3);
painter->setPen(pen);
painter->drawEllipse(ellipse);
}
}
qreal customItem::getXPosEllipse()
{
return this->scenePos().x();
}
qreal customItem::getYPosEllipse()
{
return this->scenePos().y();
}
void customItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
Pressed = true;
update();
QGraphicsItem::mousePressEvent(event);
}
void customItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
Pressed = false;
if(startOrEnd == false)
{
customLine->setLine( this->scenePos().x() + mCenter.x(),this->scenePos().y()+ mCenter.y(),
customLine->line().x2(), customLine->line().y2() );
}
if(startOrEnd == true)
{
customLine->setLine( customLine->line().x1(), customLine->line().y1(),
this->scenePos().x() + mCenter.x(),this->scenePos().y()+ mCenter.y());
}
update();
QGraphicsItem::mouseReleaseEvent(event);
}
void customItem::setLine(QGraphicsLineItem *newLine, bool side)
{
qDebug() << "-------------------------------------------";
qDebug() << "&customLine:\t" << &customLine << "\t&newLine:\t" << &newLine;
qDebug() << "customLine:\t" << customLine << "\tnewLine:\t" << newLine;
customLine = newLine;
startOrEnd = side;
qDebug() << "customLine:\t" << customLine << "\tnewLine:\t" << newLine;
}
To copy to clipboard, switch view to plain text mode
Bookmarks