Results 1 to 3 of 3

Thread: QGraphicsTextItem subclass & QGraphicsScene

  1. #1
    Join Date
    Jan 2009
    Posts
    47
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QGraphicsTextItem subclass & QGraphicsScene

    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QGraphicsTextItem subclass & QGraphicsScene

    Quote Originally Posted by ttvo View Post
    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
    It's because the items you create in Node aren't children of Node. And blur lines occurs when the bounding rect is not correct.

  3. #3
    Join Date
    Jan 2009
    Posts
    47
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsTextItem subclass & QGraphicsScene

    I made the QGraphicsRectItem to be children of the text item and modified the paint method according, i now can draw and move the node fine. Thanks, Lykurg.

    Then I tried to follow the Diagram Scene example to draw a QGraphicsLineItem between ports, not nodes, I passed the start and end nodes to the arrow. But the arrow's paint method somehow ends up drawing the arrow connecting the topleft of the 2 nodes, not at the ports.

    1) Any suggestions on how to get the right position and to draw the arrow connecting the ports, NOT nodes
    2) I also need to have code ignoring clicks between two nodes

    Thanks in advance

Similar Threads

  1. Replies: 1
    Last Post: 11th June 2009, 05:49
  2. Replies: 0
    Last Post: 5th March 2009, 06:54
  3. in QGraphicsScene matrix of other QGraphicsScene
    By Noxxik in forum Qt Programming
    Replies: 5
    Last Post: 15th February 2009, 17:27
  4. QSqlQueryModel write subclass
    By skuda in forum Qt Programming
    Replies: 6
    Last Post: 29th October 2007, 16:18
  5. Problem on set QGraphicsTextItem write protect.
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2007, 20:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.