Results 1 to 2 of 2

Thread: Custom QGraphicsItem drawn in position different from the one given

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2016
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Custom QGraphicsItem drawn in position different from the one given

    Hello everyone, I'm working in a program to create graphs and I'm currently working in the Node class which inherits from QGraphicsItem. I am placing these Nodes in a QGraphicsScene and if I set the position of the Node in QPoint(100, 10) the item gets drawn in a different position, what's weird is that if I set a position farther from the origin, the bigger is the offset of where it's drawn.

    I have set the position of the first Node at QPoint(100,10) and the second Node is at QPoint(200,20), to visualize the offset caused by the problem I have drawn circles at the exact same position of each Node where they should be drawn.
    example.jpg

    Heres the code:

    MainWindow class
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. scene = new QGraphicsScene(this);
    7. ui->graphicsView->setScene(scene);
    8.  
    9. Graph* graph = new Graph();
    10. Node* A = new Node();
    11. Node* B = new Node();
    12. graph->add(100,10, A);
    13. graph->add(200,20, B);
    14. graph->link(A, B);
    15. scene->addItem(graph);
    16.  
    17. //TESTING COORDINATES
    18. scene->addEllipse(100,10,1,1,QPen(Qt::red, 30));
    19. scene->addEllipse(200,20,1,1,QPen(Qt::blue, 30));
    20. }
    To copy to clipboard, switch view to plain text mode 

    Graph class, inherits from QGraphicsItemGroup
    With this class I am putting all the nodes in a group hoping that it will benefit me later if I have the need to move the whole graph once created.
    Qt Code:
    1. Graph::Graph()
    2. {
    3.  
    4. }
    5.  
    6. void Graph::add(qreal x, qreal y, Node *node)
    7. {
    8. addToGroup(node);
    9. nodes.push_back(node);
    10. node->setPos(x,y);
    11. }
    12.  
    13. void Graph::link(Node* A, Node* B)
    14. {
    15. A->linkNode(B);
    16. Edge* edge = new Edge(A,B);
    17. addToGroup(edge);
    18. edges.push_back(edge);
    19. }
    To copy to clipboard, switch view to plain text mode 

    Node class, inherits from QGraphicsItem
    Qt Code:
    1. Node::Node()
    2. {
    3. setRadius(15.0);
    4. }
    5.  
    6. QRectF Node::boundingRect() const
    7. {
    8. return rect;
    9. }
    10.  
    11. void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    12. {
    13. painter->setPen(QPen(Qt::black, 4));
    14. painter->setBrush(Qt::red);
    15. painter->drawEllipse(pos(), rect.width(), rect.height());
    16. }
    17.  
    18. void Node::setRadius(qreal radius)
    19. {
    20. rect.setSize(QSizeF(2.0*radius, 2.0*radius));
    21. }
    22.  
    23. void Node::linkNode(Node* node)
    24. {
    25. this->adjacency_list.push_back(node);
    26. node->getAdjacencyList()->push_back(this);
    27. }
    28.  
    29. QList<Node*>* Node::getAdjacencyList()
    30. {
    31. return &adjacency_list;
    32. }
    To copy to clipboard, switch view to plain text mode 

    What could be causing the Nodes to be drawn at different positions from the ones given in the QGraphicsScene, what am I doing wrong? Thanks for the help in advance!
    Last edited by LightJeep; 27th March 2016 at 00:00.

Similar Threads

  1. Position of QGraphicsItem on Scene.
    By Andre008 in forum Qt Programming
    Replies: 13
    Last Post: 12th February 2016, 13:37
  2. QGraphicsItem and it's position at QGraphicsView
    By silverado in forum Qt Programming
    Replies: 1
    Last Post: 20th February 2012, 15:28
  3. Fixed Position of QGraphicsItem
    By moh.gup@gmail.com in forum Qt Programming
    Replies: 10
    Last Post: 21st January 2012, 16:27
  4. QGraphicsItem is not drawn again?
    By zgulser in forum Qt Programming
    Replies: 12
    Last Post: 19th October 2009, 16:26
  5. QGraphicsItem position
    By zgulser in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2009, 09:33

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
  •  
Qt is a trademark of The Qt Company.