Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Nested QGraphicsItems drawing positions
Forum Updated to NodeBB v4.3 + New Features

[Solved] Nested QGraphicsItems drawing positions

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 5.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    deanc
    wrote on last edited by
    #1

    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);
    input1->setRect(sizeRect);
    output1->setRect(sizeRect);
    output2->setRect(sizeRect);
    //
    // 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.

    Thanks.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rokemoon
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rokemoon
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • D Offline
          D Offline
          deanc
          wrote on last edited by
          #4

          That eliminates the ellipse in the middle. Thanks!!

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rokemoon
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              deanc
              wrote on last edited by
              #6

              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?

              1 Reply Last reply
              0
              • R Offline
                R Offline
                rokemoon
                wrote on last edited by
                #7

                Maybe this will help you
                @
                painter->setPen(QPen(Qt::black, 1));
                painter->setBrush(QBrush(Qt::Dense5Pattern));
                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].

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  deanc
                  wrote on last edited by
                  #8

                  Thanks again!

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved