1

I've got a Qt application implemented consisting of a main window with a custom QWidget called CanvasWidget that has a QGraphicsScene and view in which items are drawn.

The objects are visible and drawn correctly, however, when testing for functionality, itemAt always returns false. Here is an example:

Creation of point in separate widget:

drawGraphic is a signal connected to addGraphic, a slot (pointradius = 2)

    case(PointType):
        //draw point
        pnt = new QGraphicsEllipseItem();
        pnt->setRect(exp.head.value.point_value.x - pointradius, exp.head.value.point_value.y - pointradius, pointradius * 2, pointradius * 2);
        pnt->setBrush(Qt::black);
        drawGraphic(pnt);
        break;

CanvasWidget::addGraphic:

void CanvasWidget::addGraphic(QGraphicsItem * item){
    scene->addItem(item);
    scene->update();
    qDebug() << item->scenePos();
}

Test code:

void TestGUI::testPoint() {

  QVERIFY(repl && replEdit);
  QVERIFY(canvas && scene);

  // send a string to the repl widget
  QTest::keyClicks(replEdit, "(draw (point 0 0))");
  QTest::keyClick(replEdit, Qt::Key_Return, Qt::NoModifier);

  // check canvas
  QVERIFY2(scene->itemAt(QPointF(0, 0), QTransform()) != 0,
           "Expected a point in the scene. Not found.");
}

Output:

QDEBUG : TestGUI::testPoint() QPointF(0,0)
QDEBUG : TestGUI::testPoint() QPointF(0,0)
FAIL!  : TestGUI::testPoint() 'scene->itemAt(QPointF(0, 0), QTransform()) != 0' 
returned FALSE. (Expected a point in the scene. Not found.)

QPointF(0,0)

I am fairly new to Qt and am sure that I have made a simple mistake somewhere here. Any help is greatly appreciated.

hopoffZ
  • 11
  • 3
  • 1
    So your ellipse is covering the origin of the scene (0,0)? – ypnos Apr 16 '23 at 16:15
  • Yes. Dunno if it's helpful but I'll add a screenshot of the window output after adding the point. – hopoffZ Apr 16 '23 at 17:56
  • Neither your code nor your screenshot proves that 0,0 is fully covered by an item. Did you try explicitely setting up an item at that location? – ypnos Apr 16 '23 at 22:30

0 Answers0