Qt World Summit: Register Today!

QPainterPath painting reverse?

  • Hi.
    This is my setup: a QGraphicsView in the form, a function that fills a QPointF vector with simple points (a normally ascending X with different Y values for each X) and then a function that prints those numbers:
    @ QGraphicsScene *scene = new QGraphicsScene();
    QPainterPath linePath;
    for(int i=0; i<m_qvPoints.size(); i++)
    linePath.lineTo(m_qvPoints[i].x(), m_qvPoints[i].y());
    scene->addPath(linePath, QPen("blue"), QBrush("white", Qt::NoBrush));

    And this is how I fill the vector (it's inside a for loop):
    @m_qvPoints.append(QPointF(giro, m_Bankn));@

    But i encounter two unexpected/unwanted results:

    1. The graphic is "reversed". This means that it seems to be printing the elements from the end to the beginning (so when the graphic should be descendant, it appears ascendant). I've checked manually the vector where the points are and it seems to be OK. Weird thing is that i've tried printing them reversed like this:
      @for(int i=m_qvPoints.size(); i>0; i--)@

    But the graphic is drawn exactly the same! :O

    1. There's a straight line appearing from the end to the beginning which has nothing to do with the function itself :\

    Here's a couple of screen caps:



    Any ideas? Thanks!

    PS: By the way, how can I easily add some reference frame like coordinates to orient myself when looking at the graph?

  • I'm wondering if your figure is not 'upside-down' rather than backwards... The usual default coordinate system will have y increasing down the screen rather than up like you would probably be used to. If you are pumping data points directly into the QPainterPath then, you might be able to just invert y coordinates you pass to the painterPath functions. (There may be something you can do with the items transformation, i.e. a vertical reflection or something, but this way is probably the quickest).

    Also, the first thing that you probably want to do is call moveTo() on the painterPath to start the path at the first point of your dataset (and then start your loop at i = 1).


    @linePath.moveTo(m_qvPoints.at(0).x(), -m_qvPoints.at(0).y();

    for(int i=1; i<m_qvPoints.size(); i++)
    linePath.lineTo(m_qvPoints[i].x(), -m_qvPoints[i].y());

    As for 'drawing it backwards' it makes sense that you're ending up with the same figure because it's equivalent to tracing a line with your finger from left to right or right to left.... the traced path is the exact same. The only way you're going to change the way things are drawn is by changing the x/y coordinates that are being passed into the path -> like negating the y-coordinates in the snippet above.

Log in to reply