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.)
I am fairly new to Qt and am sure that I have made a simple mistake somewhere here. Any help is greatly appreciated.