June 2, 2011

deanc deanc
Lab Rat
18 posts

[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:

  1. input1 = new QGraphicsEllipseItem(this);
  2. output2 = new QGraphicsEllipseItem(this);
  3. output1 = new QGraphicsEllipseItem(this);
  4. //
  5. // Set the connector size
  6. QRect sizeRect(0, 0, 15, 15);
  7. input1->setRect(sizeRect);
  8. output1->setRect(sizeRect);
  9. output2->setRect(sizeRect);
  10. //
  11. // Put the connectors into position
  12. input1->translate(-57, 10);
  13. output1->translate(43, 10);
  14. output2->translate(43, 30);

The paint function is:

  1. void BeamSplitterView::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
  2. {
  3.     drawNodeBase(painter, option);
  4.     input1->paint(painter, option, widget);
  5.     output1->paint(painter, option, widget);
  6.     output2->paint(painter, option, widget);
  7. }

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.

Thanks.

7 replies

June 2, 2011

rokemoon rokemoon
Lab Rat
197 posts

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

June 2, 2011

rokemoon rokemoon
Lab Rat
197 posts

Sorry for my english
Please comment this three lines

  1.     //input1->paint(painter, option, widget);
  2.     //output1->paint(painter, option, widget);
  3.     //output2->paint(painter, option, widget);

and say what you get

June 2, 2011

deanc deanc
Lab Rat
18 posts

That eliminates the ellipse in the middle. Thanks!!

June 2, 2011

rokemoon rokemoon
Lab Rat
197 posts

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.

June 2, 2011

deanc deanc
Lab Rat
18 posts

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?

June 2, 2011

rokemoon rokemoon
Lab Rat
197 posts

Maybe this will help you

  1.  painter->setPen(QPen(Qt::black, 1));
  2.  painter->setBrush(QBrush(Qt::Dense5Pattern));
  3.  painter->drawEllipse(0, 0, 15, 15);

and you can just set x, y to draw elipse where you need
  1. painter->drawEllipse(x, y, 15, 15);

Edit the post, and add at the beginning of title [Solved].

June 2, 2011

deanc deanc
Lab Rat
18 posts

Thanks again!

 
  ‹‹ Shortcut of Qt application.exe      All files from folder ››

You must log in to post a reply. Not a member yet? Register here!