Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 油漆工绘制不一致_C++_Qt_Qpainter - Fatal编程技术网

C++ 油漆工绘制不一致

C++ 油漆工绘制不一致,c++,qt,qpainter,C++,Qt,Qpainter,我正在编写一个GUI,用Qt绘制图形。我的画师表现出一些不一致性:在编译后运行完全相同的二进制文件时,它只绘制了大约50%的图形。我确实调用了QPainter的begin(),并且我还确保传递给绘图函数(如Dropellipse())的参数已初始化,并且在调用该函数时具有有效值 以下是相关代码(请注意,参数painter已初始化,并且在此函数之前已调用begin()): void GraphWidget::paintEvent(QPaintEvent*event){ QWidget::paint

我正在编写一个GUI,用Qt绘制图形。我的画师表现出一些不一致性:在编译后运行完全相同的二进制文件时,它只绘制了大约50%的图形。我确实调用了QPainter的begin(),并且我还确保传递给绘图函数(如Dropellipse())的参数已初始化,并且在调用该函数时具有有效值

以下是相关代码(请注意,参数painter已初始化,并且在此函数之前已调用begin()):

void GraphWidget::paintEvent(QPaintEvent*event){
QWidget::paintEvent(事件);
此->油漆工=新的油漆工(此);
painter->setRenderHint(QPainter::抗锯齿);
//自己画图形
画家->翻译(xOffset,yOffset);
画师->比例(graphScale,graphScale);
画师;
}
void GraphWidget::paintGraph(){
如果(此->图形){
//迭代所有边并绘制它们
for(Agnode_t*node=agfstnode(图);node;
node=agnxtnode(图,节点)){
对于(Agedge_t*edge=agfstout(图形,节点);edge;
边=agnxtout(图形,边)){
边缘;
}
}
//遍历所有节点并绘制它们
for(Agnode_t*node=agfstnode(图);node;
node=agnxtnode(图,节点)){
牵引节点(节点);
}
}
}
void GraphWidget::drawNode(Agnode_t*节点){
...
//节点的高度和宽度,以像素为单位。
float scaleWidth=宽度*此->逻辑像素();
浮动刻度高度=高度*此->逻辑刻度();

std::cout发现问题。调用
painter->translate(xOffset,yOffset)
导致了画师问题,因为当我第一次打开窗口时,
xOffset
yOffset
未初始化,所以我猜它们取了随机值,图形被转换到了我看不到的某个随机点。我只是确保在构造函数中将偏移变量初始化为0,而这个f解决了问题。

请发布一个。除非有太多的代码,否则请显示完整的
GraphWidget::drawNode
GraphWidget::paintEvent
实现——或者至少是其中的所有相关部分。对不起,我刚刚添加了所有相关的绘图功能!希望这对您有更多帮助,谢谢。我正在使用Graphviz,如果您想知道Agnode_t和Agedge_t对象。它们来自Graphviz库。不确定这是问题的一部分,但是…您正在泄漏
QPainter
s左、右和中心。为什么要在堆上分配
QPainter
。摆脱
painter
成员,只使用
QPainter painter painter(此)
。然后,您还可以删除对
QPainter::begin/end/save/restore
的调用。感谢您的输入-我修复了这个问题,行为仍然相同(尽管现在代码更干净更好)。
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);
}