Please nominate your Qt Champions for 2021!

[Solved] Nested QGraphicsItems drawing positions

  • I'm new to Qt and really loving it so far. But I'm stuck on getting nested graphics items to draw properly. Here's what I've done so far:

    I've derived a class from QGraphicsItem, call it ComponentView. This class has 3 nested QGraphicsEllipseItems. The appearance should be a rounded rectangle for the main item, with the ellipses arranged along the edges. I set it up like this in the constructor:
    @input1 = new QGraphicsEllipseItem(this);
    output2 = new QGraphicsEllipseItem(this);
    output1 = new QGraphicsEllipseItem(this);
    // Set the connector size
    QRect sizeRect(0, 0, 15, 15);
    // Put the connectors into position
    input1->translate(-57, 10);
    output1->translate(43, 10);
    output2->translate(43, 30);@

    The paint function is:
    @void BeamSplitterView::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
    drawNodeBase(painter, option);
    input1->paint(painter, option, widget);
    output1->paint(painter, option, widget);
    output2->paint(painter, option, widget);

    where drawNodeBase() just draws a rounded rectangle and a text label. The problem is that FOUR ellipses are being drawn; the three on the edge plus one in the middle of the parent (at 0,0). I actually suspect that all three of the ellipse objects are being drawn in the middle, as well as in their proper positions. I don't want the ellipse in the center and don't understand why it's being drawn.


  • Do have you this rectangle in middle, if you paint without drawNodeBase()?

  • Sorry for my english
    Please comment this three lines
    @ //input1->paint(painter, option, widget);
    //output1->paint(painter, option, widget);
    //output2->paint(painter, option, widget);@
    and say what you get

  • That eliminates the ellipse in the middle. Thanks!!

  • You are welcome. Also you can just draw elipse by QPainter->drawElipse(...), maybe it will be more effective. And please mark post as [Solved], thanks.

  • I may have to switch to the drawEllipse(...) approach because I haven't figured out how to get the ellipses to draw properly -- they only draw as empty outlines, not solid filled.

    How do you mark a thread as solved?

  • Maybe this will help you
    painter->setPen(QPen(Qt::black, 1));
    painter->drawEllipse(0, 0, 15, 15);
    and you can just set x, y to draw elipse where you need @painter->drawEllipse(x, y, 15, 15);@
    Edit the post, and add at the beginning of title [Solved].

  • Thanks again!

Log in to reply