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. Drawing QGraphicsItem inside QGraphicsPolygon
Forum Update on Monday, May 27th 2025

Drawing QGraphicsItem inside QGraphicsPolygon

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 4.6k Views
  • 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.
  • A Offline
    A Offline
    AndreAhmed
    wrote on last edited by
    #1

    I have created a class called Node which inherits from GraphicsItem. I created also another class which is called BLock and It inherits from QGraphicsPolygon.

    How can I draw Nodes at different Positions inside each Block ?
    @
    Node.cpp
    void Node::paint(QPainter *painter,
    const QStyleOptionGraphicsItem option,
    QWidget * /
    widget */)
    {
    QPen pen(myOutlineColor);
    if (option->state & QStyle::State_Selected) {
    pen.setStyle(Qt::DotLine);
    pen.setWidth(2);
    }
    painter->setPen(pen);
    painter->setBrush(myBackgroundColor);
    QRectF rect = outlineRect();
    painter->drawRoundRect(rect, roundness(rect.width()),
    roundness(rect.height()));
    painter->setPen(myTextColor);
    painter->drawText(rect, Qt::AlignCenter, myText);
    }
    @
    Block.cpp

    @Block::Block(QGraphicsItem *parent, QGraphicsScene *scene, int numNodes)
    : QGraphicsPolygonItem(parent, scene)
    {

    mPolygon << QPointF(-100, -100) << QPointF(100, -100)
              << QPointF(100, 100) << QPointF(-100, 100)
              << QPointF(-100, -100);
    setPolygon(mPolygon);
    setFlag(QGraphicsItem::ItemIsMovable, true);
    setFlag(QGraphicsItem::ItemIsSelectable, true);
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
    

    }

    QPixmap Block::image() const
    {
    QPixmap pixmap(250, 250);
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    painter.setPen(QPen(Qt::black, 8));
    painter.translate(125, 125);
    painter.drawPolyline(mPolygon);

    return pixmap;
    

    }@

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jake007
      wrote on last edited by
      #2

      Not sure if you can.
      But you can parent them together, so you will have positioning relative to it and also they'll move together etc ( Qt will take care of this details :) ).
      And you can set z-index value to child, so that it's rendered above, the parent.


      Code is poetry

      1 Reply Last reply
      0
      • A Offline
        A Offline
        AndreAhmed
        wrote on last edited by
        #3

        Thanks for your reply.
        How do I parent them together?

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jake007
          wrote on last edited by
          #4

          In constructor you have QGraphicsParent *parent pointer.
          When you create your child item, you only pass a pointer to it.

          Example:
          @QGraphicsItem *parent = new QGraphicsItem();
          QGraphicsItem *child = new QGraphicsItem(parent);@

          Child's will be automatically added to QGraphicsScene with parent.


          Code is poetry

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AndreAhmed
            wrote on last edited by
            #5

            Hi,

            I did that solution, let me know about it, however when the parent moves, children don't move together.

            I created a list of Nodes (childs) in the Block(parent)

            @
            Block::Block(int numNodes)

            {

            mPolygon << QPointF(-100, -100) << QPointF(100, -100)
                     << QPointF(100, 100) << QPointF(-100, 100)
                     << QPointF(-100, -100);
            setPolygon(mPolygon);
            setFlag(QGraphicsItem::ItemIsMovable, true);
            setFlag(QGraphicsItem::ItemIsSelectable, true);
            setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
            
            for (int i=0; i<numNodes; i++)
            {
                mNodes.append(new Node());
            }
            

            }
            @
            then in drawing, I drew each node separately here

            @
            Block* block = new Block(4);
            block->setPos(QPoint(80 + (100 * (seqNumber % 5)),
            80 + (50 * ((seqNumber / 5) % 7))));
            scene->addItem(block);

            QList<Node*> Nodes;
            int offset = 5;
            Nodes = block->getNodes();
            foreach(Node *n, Nodes)
            {
            n->setPos(QPoint(block->x()+90, block->y()+10+offset ));
            offset+=20;
            scene->addItem(n);
            }
            
            ++seqNumber;
            
            scene->clearSelection();
            node->setSelected(true);
            

            @

            but the problem is both nodes and the blocks are not coherent, they don't move together.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              Jake007
              wrote on last edited by
              #6

              Everything looks ok.
              I noticed that you deleted inherited constructor in class Block.
              Did you the the same in class Node? If you did, that's the problem.

              Try using qDebug() in parent to print number of children that it has, so that you see, if they are properly added. (this->children().count();).


              Code is poetry

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mr-Merlin
                wrote on last edited by
                #7

                Unless I'm missing something in glancing at your code, it looks like you've created the nodes in the constructor of the block, but the nodes still aren't parented.

                Try this: -

                mNodes.append(new Node(this));

                Note the 'this' pointer being added as a parent to the Node and make sure that the parameter gets passed through to the base class (QGraphicsItem) in the Node constructor.

                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