PDA

View Full Version : Custom QGraphicsItem drawn in position different from the one given



LightJeep
26th March 2016, 23:27
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.
11813

Heres the code:

MainWindow class


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);

Graph* graph = new Graph();
Node* A = new Node();
Node* B = new Node();
graph->add(100,10, A);
graph->add(200,20, B);
graph->link(A, B);
scene->addItem(graph);

//TESTING COORDINATES
scene->addEllipse(100,10,1,1,QPen(Qt::red, 30));
scene->addEllipse(200,20,1,1,QPen(Qt::blue, 30));
}


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.


Graph::Graph()
{

}

void Graph::add(qreal x, qreal y, Node *node)
{
addToGroup(node);
nodes.push_back(node);
node->setPos(x,y);
}

void Graph::link(Node* A, Node* B)
{
A->linkNode(B);
Edge* edge = new Edge(A,B);
addToGroup(edge);
edges.push_back(edge);
}


Node class, inherits from QGraphicsItem


Node::Node()
{
setRadius(15.0);
}

QRectF Node::boundingRect() const
{
return rect;
}

void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setPen(QPen(Qt::black, 4));
painter->setBrush(Qt::red);
painter->drawEllipse(pos(), rect.width(), rect.height());
}

void Node::setRadius(qreal radius)
{
rect.setSize(QSizeF(2.0*radius, 2.0*radius));
}

void Node::linkNode(Node* node)
{
this->adjacency_list.push_back(node);
node->getAdjacencyList()->push_back(this);
}

QList<Node*>* Node::getAdjacencyList()
{
return &adjacency_list;
}


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! :D

d_stranz
28th March 2016, 17:11
What could be causing the Nodes to be drawn at different positions from the ones given in the QGraphicsScene

By default, the QGraphicsScene will be centered in the QGraphicsView. So if your view is larger than the scene it contains, everything in the scene will appear to be shifted towards the center.

You can verify this by adding a top-level QGraphicsRectItem to your scene (i.e. at the same level as your circles, with no brush and a black pen), with origin at (0,0) and size the same as your scene rect. You will almost certainly see that your circles are correctly positioned with respect to this rect.