0

I have a subclass of a qgraphicsitem to create a group of items that goes in another subclass that goes to a subclassed scene.

When casting by subclass I can get the items in the good scene coords but item.pos() = (0,0) or the wrong position using item->setPos();

1) Here I get the item coords but the wrong position when adding. 2) Here I get the item coords = (0,0) but the items are in the good position.

void addExtChordseistris()
{
QPoint p = QCursor::pos();
for(QGraphicsView *view: views()){
    QWidget *viewport = view->viewport();
    QRect vr = viewport->rect();
    QPoint vp = viewport->mapFromGlobal(p);
    if(vr.contains(vp)){
        QPointF sp = view->mapToScene(vp);
        chord *itemdos = new chord();
        addItem(itemdos);
        itemdos->addchorddos(sp);
        itemdos->setPos(sp); // -> using this or not
    }
    }
}

This is what gives me pos.() = (0,0).

void debugSceneItemscuatro()
{
    QList<QGraphicsItem *> allitems = items();
        foreach(auto item, allitems) {
            if(item->type() == chord::Type){
                qDebug() << item->pos();
            }
        }
}

This is how the item were added:

#include "chord.h"
#include "note.h"

chord::chord()
{
    Pressed = false;
    setFlag(ItemIsMovable);
//    QList<QGraphicsItem> coso;
}

QRectF chord::boundingRect() const
{
    // outer most edges
    return QRectF(0,0,100,100);
}

void chord::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QRectF rect = boundingRect();

    if(Pressed)
    {
        QPen pen(Qt::red, 3);
        painter->setPen(pen);
//        painter->drawEllipse(rect);
    }
    else
    {
        QPen pen(Qt::black, 3);
        painter->setPen(pen);
//        painter->drawRect(rect);
    }
}

//[1] Add chord
void chord::addchord(QPointF sp)
{
//    scene()->addLine(sp.x(), sp.y(), sp.x()+10, sp.y()+10);
    auto *line = scene()->addLine(sp.x(), sp.y(), sp.x() + 10, sp.y() + 10);
    line->setParentItem(this);
        QList<int> midics = {10, 30, 40};
      for(int i = 0; i < midics.length(); i++)
          {
        QGraphicsSimpleTextItem *item = new QGraphicsSimpleTextItem("n", this);
        item->setFont(QFont("omheads", 20));
        item->setPos(sp.x(), sp.y()+midics[i]);
//        scene()->addItem(item);
        coso.append(item);
      }
}
//[1] Add chord
void chord::addchorddos(QPointF sp)
{
//    scene()->addLine(sp.x(), sp.y(), sp.x()+10, sp.y()+10);

    auto *line = scene()->addLine(sp.x(), sp.y(), sp.x() + 10, sp.y() + 10);
    line->setParentItem(this);
        QList<int> midics = {0, 10, 30, 40};
      for(int i = 0; i < midics.length(); i++)
          {
        note *item = new note();
        item->setParentItem(this);
        QPointF ssp = {sp.x(), sp.y()+midics[i]};
        item->addnote(ssp);
//        item->setPos(sp.x(), sp.y()+midics[i]);
//        scene()->addItem(item);
//        coso.append(item);
      }
}
//void chord::getobjects(QGraphicsItem item)
//{
//    return coso;
//}
QList<QGraphicsItem*> chord::retrievedata() const
{
     return coso;
}

void chord::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    Pressed = true;
    update();
    QGraphicsItem::mousePressEvent(event);
}

void chord::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    Pressed = false;
    update();
    QGraphicsItem::mouseReleaseEvent(event);
}

And the other subclass,..

void note::addnote(QPointF ssp) {

    QGraphicsSimpleTextItem *item = new QGraphicsSimpleTextItem("n", this);
    item->setFont(QFont("omheads", 20));
    item->setPos(ssp.x(), ssp.y());

}

Thanks,...maybe I'm doing a wrong approach, thanks for any Idea, solution...! :-)

davintxi
  • 19
  • 3
  • Hi evillanesc,...I come from your solution here,... I'm having the problem that it assign the coords QPointF(0,0) to the item added to the scene if I don't apply item->setPos(xplace, yplace), even when the item is correctly placed. Thanks! https://stackoverflow.com/questions/55215093/qgraphicsscene-troubles-to-get-scenepos-inside-a-function – davintxi Apr 01 '19 at 12:24
  • Well it seems that your current code has certain peculiarities that makes my old solution not work so that's why I've asked you for an MCVE, so if you want help provide it – eyllanesc Apr 01 '19 at 12:28
  • I did edit the body of the question and added the "debug" funtcion,...I don't know if it is enough,...I'll do a simplified example if not and upload it,...Thanks! – davintxi Apr 01 '19 at 13:26
  • I'm not sure yet but I think the problem would be that if I use your code as it is it puts the items in the correct position but assign QPointF(0,0) as the item coords if u look for item->pos(), and then, if u add setPos() to the input code it assign coords to the item, but doesn't put it in the mouse coords, it moves them a bit. Working on it yet,...we will see,... – davintxi Apr 03 '19 at 13:03
  • SOLVED!!! I had to add setPos(0,0) instead of the coords, for the childs, and then everything works,...at least until now! Thanks! – davintxi Apr 03 '19 at 17:12

0 Answers0