mercredi 30 mars 2022

Qt - QGraphicsScene MouseEvent is offset

I am trying to test a MouseEvent for a QGraphicsScene, but for some reason, the area at which the program registers the click is offset. Here is my code:

//var declaration
editorScene = new QGraphicsScene(this);
    ui->spriteGraphicsEditor->setScene(editorScene);

...

//registers a click on the graphics editor
void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton){
        // Detect if the click is in the view.
        QPoint remapped = ui->spriteGraphicsEditor->mapFromParent( event->pos() );
        if ( ui->spriteGraphicsEditor->rect().contains( remapped ) )
        {
             QPointF mousePoint = ui->spriteGraphicsEditor->mapToScene( remapped );
             qDebug() << mousePoint;
        }
    }
}

And here is a screenshot of the program(it is still a rough work-in-progress) The blue box shows the approximate area you can click on

It appears that the gray bar on the top is causing the error, as if the area is being defined before the GraphicsScene is being put into place. Why might this happen?

Aucun commentaire:

Enregistrer un commentaire