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. items() and itemsAt() do not return correct item position

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

Scheduled Pinned Locked Moved General and Desktop
itematitemitemspositionqgraphicsviewqgraphicsscene
6 Posts 3 Posters 6.0k 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
    alogim
    wrote on 14 Sept 2015, 20:19 last edited by
    #1

    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

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Joel Bodenmann
      wrote on 14 Sept 2015, 20:42 last edited by
      #2

      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.

      Industrial process automation software: https://simulton.com
      Embedded Graphics & GUI library: https://ugfx.io

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alogim
        wrote on 14 Sept 2015, 20:56 last edited by
        #3

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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          alex_malyu
          wrote on 14 Sept 2015, 21:41 last edited by alex_malyu
          #4

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

          A 1 Reply Last reply 15 Sept 2015, 14:57
          0
          • A alex_malyu
            14 Sept 2015, 21:41

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

            A Offline
            A Offline
            alogim
            wrote on 15 Sept 2015, 14:57 last edited by
            #5

            @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?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              alex_malyu
              wrote on 15 Sept 2015, 22:50 last edited by alex_malyu
              #6

              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.

              1 Reply Last reply
              0

              6/6

              15 Sept 2015, 22:50

              • Login

              • Login or register to search.
              6 out of 6
              • First post
                6/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved