QGraphicsScene addRect 不正确的渲染

QGraphicsScene addRect incorrect render

本文关键字:不正确 addRect QGraphicsScene      更新时间:2023-10-16

我在程序中使用带有附加QGraphicsScene的QGraphicsView小部件。这部分工作得很好:

    std::vector<unsigned int> nodesCoordsOnScreen;
    for(int i=0; i<nodesCoords.size(); i++)
    {
        unsigned int tempCoord = spaceH+nodesCoords.at(i)*perOneMmH;
        graphVW->scene()->addEllipse(tempCoord, -4, 8, 8, standartPen);
        QGraphicsTextItem* tempText = graphVW->scene()->addText(QString::number(i+1));
        tempText->setPos(tempCoord+8, 0);
        graphVW->scene()->addEllipse(tempCoord+8, 5, 15, 15, standartPen);
        nodesCoordsOnScreen.push_back(tempCoord);
    }

这有麻烦:

    for(int i=0; i<bars->rowCount(); i++)
    {
        unsigned int begin = barsCoords.at(i).first;
        unsigned int end = barsCoords.at(i).second;
        unsigned int begCrd = nodesCoordsOnScreen.at(begin-1);
        unsigned int endCrd = nodesCoordsOnScreen.at(end-1);
        graphVW->scene()->addRect(begCrd+4, (barsAreas.at(i)/2)*perOneMmV, endCrd-begCrd,
                              -(barsAreas.at(i))*perOneMmV, standartPen);
    }

麻烦很奇怪:当第二部分渲染时 - 它渲染不正确,但如果我移动程序窗口或最小化它并返回正常状态 - 它呈现正确。

只需要在绘图后使用 QtGraphicsScene::update()。