Drawing QGraphicsItem inside QGraphicsPolygon



  • 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;
    

    }@



  • 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.



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



  • 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.



  • 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.



  • 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();).



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.