QPainter 绘制不一致
QPainter not drawing consistently
我正在编写一个用Qt绘制图形的GUI。我的画家表现出一些不一致:它只绘制了大约 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)
会导致画家出现问题,因为当我第一次打开窗户时,xOffset
和yOffset
未初始化,所以我的猜测是它们采用随机值,并且图形正在被转换为我看不到它的某个随机位置。我只是确保在构造函数中将偏移变量初始化为 0,这解决了问题。
相关文章:
- 大于65535的C++数组[size]引发不一致的溢出
- 在 C++(和 C)中进行类型转换时明显不一致
- 填充上编译器生成的复制构造函数之间的不一致
- 犰狳的 print() 方法和 cout 在从 Rcpp 调用时顺序不一致
- CreateDIBSection为同一图像返回不一致的位图位值
- 在 Qml 中从 QSqlTableModel 中删除单行时视图不一致
- 模板参数推导不一致
- 声明中不一致的no是否违反ODR?
- 如何删除分支因子不一致的树,最大为 30,40
- 从 C++ 函数与 Python 函数返回的不一致值用于偏斜正态分布
- 从 C 字符串构造 std::string 与从另一个 std::string 构造 std::string 不一致
- 这种比较是否不一致(或者存在其他问题)?
- 以下可变参数模板行为是否不一致?
- 如何修复我的链表读数不一致的问题?
- 在C++17中,为什么类模板和函数模板的指针类型推导明显不一致
- void 函数中的指针参数返回不一致的值
- 如何查找导致结果不一致的代码
- 跨平台 mySQL 与字符集不一致
- C++:不一致的 std::p ow( 类型 ) 定义
- QPainter 绘制不一致