items() and itemsAt() do not return correct item position



  • Hi everyone. I'm coping with a strange problem. I have a simple window with a manually added QGraphicsView and QGraphicsScene. The problem is represented by the fact that the following code, instead of returning the exact coordinate of the QPoint added to the scene, always returns 0,0. I even tried a QLine and it is detected, but always in the same position, that is, 0,0, and this makes no sense. I tried to move the various objects, removing them to check if the function provided the same thing, but it works: when no object is on the scene, it does not return anything.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include <QDebug>
    #include <QGraphicsItem>
    #include <QGraphicsView>
    
    MainWindow::MainWindow(QWidget *parent) :
    	QMainWindow(parent),
    	ui(new Ui::MainWindow)
    {
    	ui->setupUi(this);
    	QGraphicsView* graphicsView = new QGraphicsView (this);
    	QGraphicsScene *scene = new QGraphicsScene(this);
    
    	scene->setSceneRect(0, 0, 200, 200);
    	graphicsView->setScene(scene);
    	graphicsView->setFixedSize(200, 200);
    	graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    	graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    
    	scene->addEllipse(180, 76, 0.5, 0.5, QPen(Qt::red), QBrush(Qt::SolidLine));
    
    	qDebug() << scene->itemAt(180, 76, graphicsView->transform());
    	QList<QGraphicsItem*> itemList = scene->items();
    	for (int i = 0; i < itemList.size(); i++)
    	{
    		qDebug() << itemList.at(i)->type() << itemList.at(i)->pos();
    	}
    }
    
    MainWindow::~MainWindow()
    {
    	delete ui;
    }
    

    This code returns the following:

    QGraphicsItem(0x1ce9370, pos=0,0)
    4 QPointF(0,0)
    

    When no object is on the scene it returns:

    QGraphicsItem(0)
    

    Michael



  • I am not sure why this happens as you don't seem to assign the item to any parent. However, still worth mentioning: QGraphicsItem::pos() will return its position relative to the parent item. You can translate those to absolute scene coordinates by using QGraphicsItem::mapToScene(). For convenience, you can also just use QGraphicsItem::scenePos() to directly retrieve the items absolute position in the scene.

    I am not sure what QGraphicsView::transform() does in your case, you might want to drop that.



  • @Joel-Bodenmann
    I tried as you said:

    qDebug() << scene->itemAt(180, 76, QTransform())->scenePos();
    QList<QGraphicsItem*> itemList = scene->items();
    for (int i = 0; i < itemList.size(); i++)
    {
      qDebug() << itemList.at(i)->type() << itemList.at(i)->scenePos();
    }
    

    The result is still 0,0.



  • @alogim said:

    addEllipse

    It does return correct position. Your expectations are not correct.
    From documentation:

    "QGraphicsEllipseItem * QGraphicsScene::addEllipse ( const QRectF & rect, const QPen & pen = QPen(), const QBrush & brush = QBrush() )
    Creates and adds an ellipse item to the scene, and returns the item pointer. The geometry of the ellipse is defined by rect, and its pen and brush are initialized to pen and brush.

    Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0)."

    If you wanted position to be equal to center etc, center rectangle around 0 point then change item position to point of interest.



  • @alex_malyu said:

    Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0)."

    If you wanted position to be equal to center etc, center rectangle around 0 point then change item position to point of interest.

    So, what have I to do in order to get item coordinates mapped to the scene/graphicsView?



  • I do not understand last question. I do not see any problem with mapping which rely on the item bounding rectangle.

    You can have an infinite number of visually the same graphics items if you use different combination of position and bounding box parameters.

    For example if you create QGraphicsEllipseItem using following rectangle/position pairs you will get the same result on the screen?
    pair 1:
    QRectF rect1( 0, 0, 1., 2 );
    QPointF pos1(10.,20.);
    pair 2:
    QRectF rect1( 10,20, 1., 2 );
    QPointF pos1(0.,0.);

    Which way to define depends on your needs.



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