Hi guys, iam stuck in one case. I have scene (using QGraphicsScene) and i fill that scene with squares (using QGraphicsRectItem). I want squares to change their color to black if my mouse button is pressed and if my mouse coursor is over them so i can move my mouse over the whole scene and make every square to change their color to black whenever cursor enters an area of a square and mouse button is pressed. Can you please give me any idea how to make that happen ? I was trying to solve that using mousePressEvent, mouseMoveEvent, dragEnterEvent etc. and I think that this is a proper way to do that but i have no idea how to push that through. To put more light on my case i have added sample of my code. Thanks for help.

main.cpp:
Qt Code:
  1. #include <QApplication>
  2. #include <QGraphicsScene>
  3. #include <QGraphicsView>
  4. #include "square.h"
  5. #include "background.h"
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9. QApplication a(argc, argv);
  10.  
  11. // create a scene
  12. QGraphicsScene * scene = new QGraphicsScene(0,0,200,250);
  13.  
  14.  
  15. Background * background = new Background();
  16. background->fillBackgroundWithSquares(scene);
  17.  
  18.  
  19.  
  20. // add a view
  21. QGraphicsView * view = new QGraphicsView(scene);
  22.  
  23. view->show();
  24.  
  25.  
  26. return a.exec();
  27. }
To copy to clipboard, switch view to plain text mode 

background.h:
Qt Code:
  1. #ifndef BACKGROUND_H
  2. #define BACKGROUND_H
  3. #include <QGraphicsScene>
  4. #include <square.h>
  5. class Background
  6. {
  7. public:
  8. Background();
  9. void fillBackgroundWithSquares(QGraphicsScene *scene);
  10.  
  11. };
  12.  
  13. #endif // BACKGROUND_H
To copy to clipboard, switch view to plain text mode 

background.cpp:
Qt Code:
  1. #include "background.h"
  2.  
  3. Background::Background()
  4. {
  5.  
  6. }
  7.  
  8. void Background::fillBackgroundWithSquares(QGraphicsScene *scene)
  9. {
  10. // create an item to put into the scene
  11. Square *squares[20][25];
  12.  
  13.  
  14. // add squares to the scene
  15. for (int i = 0; i < 20; i++)
  16. for (int j = 0; j < 25; j++) {
  17. squares[i][j] = new Square(i*10,j*10);
  18. scene->addItem(squares[i][j]);
  19. }
  20.  
  21. }
To copy to clipboard, switch view to plain text mode 
square.h:

Qt Code:
  1. #ifndef SQUARE_H
  2. #define SQUARE_H
  3. #include <QGraphicsRectItem>
  4. #include <QGraphicsView>
  5. #include <QDragEnterEvent>
  6.  
  7.  
  8. class Square : public QGraphicsRectItem
  9. {
  10. public:
  11. Square(int x, int y);
  12.  
  13. private:
  14. QPen pen;
  15.  
  16. protected:
  17. void mousePressEvent(QGraphicsSceneMouseEvent * event);
  18. void dragEnterEvent(QGraphicsSceneDragDropEvent * event);
  19. void mouseMoveEvent(QGraphicsSceneMouseEvent * event);
  20.  
  21. };
  22.  
  23. #endif // SQUARE_H
To copy to clipboard, switch view to plain text mode 

square.cpp:
Qt Code:
  1. #include "square.h"
  2.  
  3. Square::Square(int x, int y)
  4. {
  5. // draw a square
  6. setRect(x,y,10,10);
  7. pen.setBrush(Qt::NoBrush);
  8.  
  9. setPen(pen);
  10.  
  11. setBrush(Qt::cyan);
  12. setAcceptDrops(true);
  13. setAcceptHoverEvents(true);
  14. }
  15.  
  16. // following functions are just attempt to make every square to be painted on black if I press my mouse button and move cursor over squares but none of the following work as I want.
  17.  
  18. void Square::mousePressEvent(QGraphicsSceneMouseEvent *event)
  19. {
  20. setBrush(Qt::black);
  21. }
  22.  
  23. void Square::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
  24.  
  25. {
  26. setBrush(Qt::black);
  27. }
  28.  
  29. void Square::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
  30. {
  31. setBrush(Qt::black);
  32. }
To copy to clipboard, switch view to plain text mode