PDA

View Full Version : Combine QGraphicsPathItem



mvbhavsar
11th February 2015, 07:24
Hello,


I have 2 QGraphicsPathItem having there own QPainterPath set within it.
Now I want 2nd QGraphicsPathItem to get connected to 1st QGraphicsPathItem using connectPath or any other function so that resultant QGraphicsPathItem is 1st QGraphicsPathItem.

I don't want to do this within single QGraphicsPathItem since I will not be able to modify individual elements at run time.

How to implement this?


Thanks

Manish

wysota
11th February 2015, 08:47
Subclass the item, teach it to accept pointers to other instances of that class and use itemChange() method to move those items when your item moves.

mvbhavsar
11th February 2015, 09:56
I have following code but still it is not working.



#include "cubeitem.h"

CubeItem::CubeItem(QGraphicsItem *parent):QGraphicsPathItem(parent),
m_Point1(10,10),
m_Point2(100,100),
m_Point3(50,150)
{
startPoint = m_Point1;
setFlags(ItemIsSelectable|ItemIsMovable);
setShapePath();
}

CubeItem::~CubeItem()
{

}

void CubeItem::setLastItem(QGraphicsPathItem *pi)
{
startPoint = pi->shape().currentPosition();
QPainterPath path1 = pi->shape();
QPainterPath path2;
path2.moveTo(path1.currentPosition());
path2.addPath(shape());
path1.connectPath(path2);
pi->setPath(path1);
setPath(path2);
update();
}

void CubeItem::setPoint1(QPointF arg)
{
m_Point1 = arg;
setShapePath();
update();
}
void CubeItem::setPoint2(QPointF arg)
{
m_Point2 = arg;
setShapePath();
update();
}

void CubeItem::setPoint3(QPointF arg)
{
m_Point3 = arg;
setShapePath();
update();
}

void CubeItem::setShapePath()
{
QPainterPath path;
// path.moveTo(startPoint);
path.cubicTo(m_Point1, m_Point2, m_Point3);
setPath(path);
setBrush(QBrush(Qt::yellow));
QPen pen(Qt::gray);
pen.setWidth(0);
setPen(pen);
}



Also, in mainwindow class, I have following code written on button clicked.



void MainWindow::on_tbCube_clicked()
{
cbi = new CubeItem;
scene->addItem(cbi);
if (scene->selectedItems().size()>0){
QGraphicsPathItem *pi = qgraphicsitem_cast<QGraphicsPathItem*>(scene->selectedItems().last());
if (pi){
cbi->setLastItem(pi);
}
}
}

wysota
11th February 2015, 11:10
In what way is this code supposed to "be working"?

mvbhavsar
11th February 2015, 12:08
Made the changes as below. Now what is happening is that if I move the second item after selecting, it does not retain first item's last point. And hence both looks detached.




#include "cubeitem.h"

CubeItem::CubeItem(QGraphicsPathItem *pi, QGraphicsItem *parent):QGraphicsPathItem(parent),QObject(),
m_Point1(10,10),
m_Point2(100,100),
m_Point3(50,150),
m_pi(pi)
{
if (pi){
startPoint = pi->shape().currentPosition();
startPoint = mapFromItem(pi,startPoint);
}
else{
startPoint = m_Point1;
}
setFlags(ItemIsSelectable|ItemIsMovable|ItemSendsG eometryChanges);
setData(0,"CubeItem");
setShapePath();
qDebug() << startPoint;
}

CubeItem::~CubeItem()
{

}

void CubeItem::setPoint1(QPointF arg)
{
m_Point1 = arg;
setShapePath();
update();
}
void CubeItem::setPoint2(QPointF arg)
{
m_Point2 = arg;
setShapePath();
update();
}

void CubeItem::setPoint3(QPointF arg)
{
m_Point3 = arg;
setShapePath();
update();
}

void CubeItem::setShapePath()
{
QPainterPath path;
path.moveTo(startPoint);
qDebug() << m_Point1 << "=="<< m_Point2 << "=="<< m_Point3;
path.cubicTo(m_Point1, m_Point2, m_Point3);
setPath(path);
QPen pen(Qt::gray);
pen.setWidth(0);
setPen(pen);
}



QVariant CubeItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change==ItemPositionChange){
if (m_pi){
setPoint1(startPoint);
}
}
return value;
}