目前,我用一条线连接两个QGraphicsPixmapItem
,并能够移动它们,但是当我添加第三个QGraphicsPixmapItem
并尝试将其连接到其中一个时,它会破坏前一条连接线。
以下是我的当前代码:
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include <QGraphicsLineItem>
#include <QPixmap>
class CustomElipse : public QGraphicsPixmapItem
{
public:
CustomElipse (const QPixmap& pixmap) : QGraphicsPixmapItem(pixmap) {
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemSendsScenePositionChanges);
}
void addLine(QGraphicsLineItem *line, bool isPoint1) {
this->line = line;
isP1 = isPoint1;
}
QVariant itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionChange && scene()) {
// value is the new position.
QPointF newPos = value.toPointF();
moveLineToCenter(newPos);
}
return QGraphicsItem::itemChange(change, value);
}
void moveLineToCenter(QPointF newPos) {
int xOffset = pixmap().rect().x() + pixmap().width()/2;
int yOffset = pixmap().rect().y() + pixmap().height()/2;
QPointF newCenterPos = QPointF(newPos.x() + xOffset, newPos.y() + yOffset);
// Move the required point of the line to the center of the elipse
QPointF p1 = isP1 ? newCenterPos : line->line().p1();
QPointF p2 = isP1 ? line->line().p2() : newCenterPos;
line->setLine(QLineF(p1, p2));
}
private:
QGraphicsLineItem *line;
bool isP1;
};
int main(int argc,char*argv[])
{
QApplication a(argc, argv);
QGraphicsScene scene;
CustomElipse *elipse1 = new CustomElipse(QPixmap(":green.png"));
scene.addItem(elipse1);
CustomElipse *elipse2 = new CustomElipse(QPixmap(":green.png"));
scene.addItem(elipse2);
CustomElipse *elipse3 = new CustomElipse(QPixmap(":green.png"));
scene.addItem(elipse3);
QGraphicsLineItem *line = scene.addLine(QLineF(40, 40, 80, 80));
elipse1->addLine(line, true);
elipse2->addLine(line, false);
elipse3->addLine(line,false);
QGraphicsView view(&scene);
view.show();
return a.exec();
}
我试着创建一条新的线,并像前一条一样连接它。我知道它覆盖了前一行,但保存它也不是一个好主意,因为一个项目可能指向100个其他项目。
QGraphicsLineItem *line1 = scene.addLine(QLineF(40, 40, 80, 80));
elipse3->addLine(line,true);
elipse2->addLine(line,false);
1条答案
按热度按时间mcvgt66p1#
您的
CustomElipse
一次只能连接一行,因此添加第二行将使其与任何其他连接到它的行分离。您需要考虑的是允许您的项目处理附加到多个行的情况,因此可以使用行列表而不是
QGraphicsLineItem *line;
。bool isP1;
也是如此。这导致您的代码需要进行必要的更改,以便将行移动到其中心方法,因此它将处理附加到它的所有行,这需要一个遍历行列表的简单循环。
结果如下: