Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

34 rader
901 B

  1. #include "connection.h"
  2. #include <QPen>
  3. #include <QPainter>
  4. Connection::Connection(Item* from, Item::AnchorType fromType,
  5. Item* to, Item::AnchorType toType, QGraphicsItem* parent)
  6. : QGraphicsLineItem(parent),
  7. from_(from), to_(to), fromType_(fromType), toType_(toType)
  8. {
  9. setZValue(-1); // 在图元之下
  10. setPen(QPen(Qt::black, 2));
  11. setFlags(QGraphicsItem::ItemIsSelectable);
  12. from_->addConnection(this);
  13. to_->addConnection(this);
  14. updatePosition();
  15. }
  16. void Connection::updatePosition()
  17. {
  18. setLine(QLineF(from_->anchorPos(fromType_), to_->anchorPos(toType_)));
  19. }
  20. void Connection::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
  21. {
  22. QPen pen = this->pen();
  23. if (isSelected()) {
  24. pen.setColor(Qt::red); // 选中变红
  25. pen.setWidth(3);
  26. }
  27. painter->setPen(pen);
  28. painter->drawLine(line());
  29. }