Unsolved Detecting left mouse click
-
Hello,
How could I detect a left mouse click on the QGraphicsRectItems and know which QGraphicsRectItem was clicked? I am implementing multiple shortest path algorithms and I visualize them and I want that user could draw "walls". -
There are several posts on how to do this. Use the search :)
-
I added this line to mainwindow header file
protected: void mousePressEvent(QGraphicsSceneMouseEvent *mouseevent);
And now I am getting a warning - 'MainWindow::mousePressEvenet' hides overloaded virtual function. So, that means that I need to create a separate class for my QGraphicsScene that has QGraphicsRectItems?
-
Just do what's suggested there.
Change your line to:void mousePressEvent(QGraphicsSceneMouseEvent *mouseevent) override;
-
@Pl45m4 said in Detecting left mouse click:
void mousePressEvent(QGraphicsSceneMouseEvent *mouseevent) override;
Thank you for the reply:), but when I changed my line to yours, I received new error - error: non-virtual member function marked 'override' hides virtual member function
-
Hi,
A QMainWindow is not a QGraphicsScene. Take a good look at the method signatures of both these classes.
-
Thanks for the reply:), I will look into that, I thought that there is no difference where you create a
mousePressEvent, oh and by the way, I found another solution, I will loop through QGraphicsRectItem and if that rect isUnderMouse I will paint a wall there.
It's not perfect solution, but at least it works:)void MainWindow::keyPressEvent(QKeyEvent *event){ if (event->key() == Qt::Key_Space){ for (int y = 0; y < height; y++){ for (int x = 0; x < width; x++){ if (cells[y][x]->isUnderMouse()){ cells[y][x]->setBrush(Qt::red); vec.cells[y][x] = wall; } } } } }