QPainter 绘制不一致
QPainter not drawing consistently
我正在编写一个使用 Qt 绘制图形的 GUI。我的 painter 显示出一些不一致:它只在我 运行 编译后完全相同的二进制文件的大约 50% 的时间内绘制图表。我确实调用了 QPainter 的 begin(),而且我还确保传递给绘图函数(例如 drawEllipse())的参数在我调用该函数时已初始化并具有有效值。
相关代码如下(注意参数painter已经初始化,begin()在该函数之前已经调用):
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);
}
找到问题了。调用 painter->translate(xOffset, yOffset)
导致了画家问题,因为当我第一次打开 window 时,xOffset
和 yOffset
未初始化,所以我猜测它们采用随机值并且正在翻译图形到一些我看不到的随机地点。我只是确保在构造函数中将偏移量变量初始化为 0,这解决了问题。
我正在编写一个使用 Qt 绘制图形的 GUI。我的 painter 显示出一些不一致:它只在我 运行 编译后完全相同的二进制文件的大约 50% 的时间内绘制图表。我确实调用了 QPainter 的 begin(),而且我还确保传递给绘图函数(例如 drawEllipse())的参数在我调用该函数时已初始化并具有有效值。
相关代码如下(注意参数painter已经初始化,begin()在该函数之前已经调用):
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);
}
找到问题了。调用 painter->translate(xOffset, yOffset)
导致了画家问题,因为当我第一次打开 window 时,xOffset
和 yOffset
未初始化,所以我猜测它们采用随机值并且正在翻译图形到一些我看不到的随机地点。我只是确保在构造函数中将偏移量变量初始化为 0,这解决了问题。