Important: Please read the Qt Code of Conduct -

QGraphicsscene:: How to choose the recessive object ?

  • Hi, guys!
    I have a question. I use Qgrapchicscene to overlay an aircraft into a map and QLineF to paint the aircrafts route. The airplane is only a triangle, and the route is a QLineF.
    The problem is that the aircraft (triangle) is in the background.
    I don't like this and i want to ask how can i make the triangle recessive so the route do not overlays the triangle. For a better understood please take a look of the screenshot!


    And the second Question is how can i erase the route if the user changes the airplane?

    Thank you in advance!

  • Moderators

    You can add line objects using addLine.
    The returned object inherits QGraphicsItem so you can set the Z-order of it using setZValue.

  • beautiful!! thank you!
    And do you know how can erase all the lines when i want to do that ?

    And do you have any suggestions, how can i change the color indepence of the speed like the ?

  • Moderators

    For this a QGraphicsPathItem might be a better choice.

    A simple example

    //make a path consisting of some points
    QPainterPath path;
    ... //and so on (of course a loop would be better here)
    //create a gradient with wanted colors
    QLinearGradient gradient;
    gradient.setColorAt(0.0, Qt::red);
    gradient.setColorAt(0.5, Qt::green);
    gradient.setColorAt(1.0, Qt::blue);
    //make a wide pen
    QPen pen;
    //use the path and the pen to add a path item to the scene
    QGraphicsPathItem* pathItem = myScene->addPath(path, pen);
    //at some point remove the item from the scene and delete it
    delete pathItem;

    If the gradient is not good enough a solution you can just add individual lines with different pen colors, e.g.

    QList<QGraphicsLineItem*> lines;
    lines << myScene->addLine(myPlaneLineFragment(0), QPen(colorForSpeed(0)));
          << myScene->addLine(myPlaneLineFragment(1), QPen(colorForSpeed(1)));
          ... //and so on (of course a loop would be better here)

    and remove and delete them as in previous example.

  • i am little bit confused, so i post my way:

    this is the code in one class

    QVector<QLineF> aircraftsPath;
    QLineF temp;
    if(temp.length()<30) aircraftsPath.push_back(temp); //avoid the error lines from bad signals

    this is the code in mainwindow Class:

    if(pointOfAirplane->aircraftsPath.size()>0) {
       for(int i=0;i<pointOfAirplane->aircraftsPath.size();i++) {
           QLineF *temp= &pointOfAirplane->aircraftsPath[i];
           QPen *pen = new QPen(Qt::red, 3, Qt::DashDotLine, Qt::RoundCap, Qt::RoundJoin);
           pen->setColor(Qt::red); // Here i try to find a way to have different colors

  • Moderators

    You are leaking the QPen instance (you use new and never delete it). You don't need those pointers or the first if (since you loop anyway).

    So you could do something more or less like this:
    Assuming you've got some speed stored somewhere, e.g. when you add a path point you could add a speed color too:

    struct PathSegment {
       QLineF line;
       QColor speedColor;
    QVector<PathSegment> aircraftsPath;

    Then it's just a matter of adding it to the scene

    QVector<QGraphincsLineIntem*> lineItems;
    foreach(const PathSegment& segment, pointOfAirplane->aircraftsPath) {
       QPen pen(segment.speedColor, 3.0, Qt::DashDotLine, Qt::RoundCap, Qt::RoundJoin);
       lineItems << scene->addLine(segment.line, pen);

    When you need to clear the lines just iterate through the lineItems vector and delete them like I showed you in the previous post.

Log in to reply