Reputation: 67
I'm programming a GUI that draws graphs with Qt. My painter is showing some inconsistency: It only paints the graph about 50% of the times that I run the exact same binary after compilation. I do call QPainter's begin(), and I also made sure that the parameters I'm passing to the drawing functions such as drawEllipse() are initialized and have valid values when I call the function.
Below is the relevant code (note that the parameter painter has been initialized and begin() has been called prior to this function):
void GraphWidget::paintEvent(QPaintEvent *event) {
QWidget::paintEvent(event);
this->painter = new QPainter(this);
painter->setRenderHint(QPainter::Antialiasing);
// draw graph itself
painter->translate(xOffset, yOffset);
painter->scale(graphScale, graphScale);
paintGraph(painter);
}
void GraphWidget::paintGraph() {
if (this->graph) {
// Iterate thought all edges and draw them
for (Agnode_t *node = agfstnode(graph); node;
node = agnxtnode(graph, node)) {
for (Agedge_t *edge = agfstout(graph, node); edge;
edge = agnxtout(graph, edge)) {
drawEdge(edge);
}
}
// Iterate through all nodes and draw them
for (Agnode_t *node = agfstnode(graph); node;
node = agnxtnode(graph, node)) {
drawNode(node);
}
}
}
void GraphWidget::drawNode(Agnode_t *node) {
...
//Height and width of node, in pixels.
float scaleWidth = width * this->logicalDpiX();
float scaleHeight = height * this->logicalDpiY();
std::cout << "Drawing individual node. x = " << x << ". scaleWidth = " << scaleWidth << ". y = " << y << ". ScaleHeight = " << scaleHeight << "\n";
//Actual node painting takes place here.
painter->drawEllipse(x - scaleWidth / 2, y - scaleHeight / 2, scaleWidth, scaleHeight);
...
}
void GraphWidget::drawEdge(Agedge_t *edge) {
// retrieve the position attribute and parse it
float lastx, lasty, x, y;
getNodePos(agtail(edge), lastx, lasty);
auto spline_list = ED_spl(edge)->list;
for (int i = 0; i < spline_list->size; i++) {
x = spline_list->list[i].x;
y = spline_list->list[i].y;
painter->drawLine(lastx, lasty, x, y);
lastx = x;
lasty = y;
}
getNodePos(aghead(edge), x, y);
painter->drawLine(lastx, lasty, x, y);
}
Upvotes: 0
Views: 538
Reputation: 67
Found the problem. Calling painter->translate(xOffset, yOffset)
was causing the painter issues because when I first opened the window, xOffset
and yOffset
were uninitialized so my guess is they took random values and the graph was being translated to some random spot where I couldn't see it. I just made sure to initialize the offset variables to 0 in the constructor and this fixed the problem.
Upvotes: 0