Получение подклассных координат qgraphicsitem при приведении типа - PullRequest
0 голосов
/ 01 апреля 2019

У меня есть подкласс элемента qgraphics для создания группы элементов, которая переходит в другой подкласс, который переходит в подклассовую сцену.

При приведении к подклассу я могу получить элементы в хороших координатах сцены, но item.pos () = (0,0) или неправильную позицию, используя item-> setPos ();

1) Здесь я получаю координаты элемента, но при добавлении неверная позиция.2) Здесь я получаю координаты элемента = (0,0), но элементы находятся в хорошем положении.

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
    }
    }
}

Это то, что дает мне поз. () = (0,0).

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

Вот как был добавлен элемент:

#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);
}

И другой подкласс, ..

void note :: addnote (QPointF ssp) {

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

}

Спасибо, ... возможно, я делаю неправильный подход, спасибо за любую идею, решение ...!: -)

...