Hi all,

So I want to create a text item with bounding box and a few rectangular ports representing inputs/outputs to my text item. Then I also need the ability to connecting the port from one node to another.

I subclass QGraphicsTextItem so that it also contains some QGraphicsRectItem to be able to create nodes as in this picture
Nodes.PNG

I added
Qt Code:
  1. Node *item = new Node(m_selectedJFIName.c_str(), 2);
  2. addItem(item);
To copy to clipboard, switch view to plain text mode 

in my QGraphicsScene. There are 2 problems
1) when I drag the Node within the scene, there seems to an issue with refresh, that is, the rectangles didn't move to the new position nicely, there are some blur lines until the scene refresh
2) I followed the Diagram scene example to implement mouseReleaseEvent event to draw a QGraphicsLineItem from a rectangle of node # 1 to a rectangle in node # 2, but the call of
QList<QGraphicsItem *> startItems = items(m_line->line().p1());

within the scene mouseReleaseEvent didn't contain the rectangle from noe # 1, same thing with the
QList<QGraphicsItem *> endItems = items(m_line->line().p2()); call

I guess it's due to the fact that I never call addItem on the rectangles? Please help as I have tried various things, but nothing seems to work thus far. thanks.

Qt Code:
  1. class Node : public QGraphicsTextItem
  2. {
  3. public:
  4. Node(std::string text, int nports=2, QGraphicsItem *parent = 0);
  5. virtual ~Node();
  6.  
  7. QRectF boundingRect() const;
  8. QPainterPath shape() const;
  9.  
  10. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
  11.  
  12. private:
  13. QList<QGraphicsRectItem *> m_inports;
  14. QGraphicsRectItem *m_outport;
  15. };
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. Node::Node(std::string text, int nports, QGraphicsItem *parent)
  2. : QGraphicsTextItem(text.c_str(), parent)
  3. {
  4. setFlag(QGraphicsItem::ItemIsMovable, true);
  5. setFlag(QGraphicsItem::ItemIsSelectable, true);
  6. int tick = boundingRect().width()/(nports+1);
  7. for (int i=0; i<nports; ++i) {
  8. QGraphicsRectItem *iport = new QGraphicsRectItem(QRectF((i+1)*tick, -10, 10, 10));
  9. iport->setZValue(1);
  10. m_inports.push_back(iport);
  11. }
  12. m_outport = new QGraphicsRectItem(QRectF(40, 27, 10, 10));
  13. m_outport->setZValue(1);
  14. }
  15.  
  16. Node::~Node()
  17. {
  18. while (!m_inports.isEmpty())
  19. delete m_inports.takeFirst();
  20. if (m_outport) {
  21. delete m_outport;
  22. m_outport = 0;
  23. }
  24. if (m_text) {
  25. delete m_text;
  26. m_text = 0;
  27. }
  28. }
  29.  
  30. QRectF Node::boundingRect() const
  31. {
  32. const QRectF t = QGraphicsTextItem::boundingRect();
  33. QRectF t2;
  34. t2.setHeight(t.height()+10);
  35. t2.setWidth(t.width()+10);
  36. return t2;
  37. }
  38.  
  39. QPainterPath Node::shape() const
  40. {
  41. const QRectF t = QGraphicsTextItem::boundingRect();
  42. QRectF t2;
  43. t2.setHeight(t.height()+10);
  44. t2.setWidth(t.width()+10);
  45. path.addEllipse(t2);
  46. return path;
  47. }
  48.  
  49. void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  50. {
  51. painter->setPen(QPen(Qt::black, 1));
  52. painter->drawRoundedRect(0, 0, boundingRect().width(),
  53. boundingRect().height(), 5, 5);
  54. QGraphicsTextItem::paint(painter, option, widget);
  55. int count = m_inports.count();
  56. for (int i=0; i<count; ++i) {
  57. m_inports.at(i)->paint(painter, option, widget);
  58. }
  59. m_outport->paint(painter, option, widget);
  60. }
To copy to clipboard, switch view to plain text mode