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:
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include "square.h"
#include "background.h"
int main(int argc, char *argv[])
{
// create a scene
Background * background = new Background();
background->fillBackgroundWithSquares(scene);
// add a view
view->show();
return a.exec();
}
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include "square.h"
#include "background.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// create a scene
QGraphicsScene * scene = new QGraphicsScene(0,0,200,250);
Background * background = new Background();
background->fillBackgroundWithSquares(scene);
// add a view
QGraphicsView * view = new QGraphicsView(scene);
view->show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
background.h:
#ifndef BACKGROUND_H
#define BACKGROUND_H
#include <QGraphicsScene>
#include <square.h>
class Background
{
public:
Background();
};
#endif // BACKGROUND_H
#ifndef BACKGROUND_H
#define BACKGROUND_H
#include <QGraphicsScene>
#include <square.h>
class Background
{
public:
Background();
void fillBackgroundWithSquares(QGraphicsScene *scene);
};
#endif // BACKGROUND_H
To copy to clipboard, switch view to plain text mode
background.cpp:
#include "background.h"
Background::Background()
{
}
{
// create an item to put into the scene
Square *squares[20][25];
// add squares to the scene
for (int i = 0; i < 20; i++)
for (int j = 0; j < 25; j++) {
squares[i][j] = new Square(i*10,j*10);
scene->addItem(squares[i][j]);
}
}
#include "background.h"
Background::Background()
{
}
void Background::fillBackgroundWithSquares(QGraphicsScene *scene)
{
// create an item to put into the scene
Square *squares[20][25];
// add squares to the scene
for (int i = 0; i < 20; i++)
for (int j = 0; j < 25; j++) {
squares[i][j] = new Square(i*10,j*10);
scene->addItem(squares[i][j]);
}
}
To copy to clipboard, switch view to plain text mode
square.h:
#ifndef SQUARE_H
#define SQUARE_H
#include <QGraphicsRectItem>
#include <QGraphicsView>
#include <QDragEnterEvent>
{
public:
Square(int x, int y);
private:
protected:
void dragEnterEvent(QGraphicsSceneDragDropEvent * event);
};
#endif // SQUARE_H
#ifndef SQUARE_H
#define SQUARE_H
#include <QGraphicsRectItem>
#include <QGraphicsView>
#include <QDragEnterEvent>
class Square : public QGraphicsRectItem
{
public:
Square(int x, int y);
private:
QPen pen;
protected:
void mousePressEvent(QGraphicsSceneMouseEvent * event);
void dragEnterEvent(QGraphicsSceneDragDropEvent * event);
void mouseMoveEvent(QGraphicsSceneMouseEvent * event);
};
#endif // SQUARE_H
To copy to clipboard, switch view to plain text mode
square.cpp:
#include "square.h"
Square::Square(int x, int y)
{
// draw a square
setRect(x,y,10,10);
pen.setBrush(Qt::NoBrush);
setPen(pen);
setBrush(Qt::cyan);
setAcceptDrops(true);
setAcceptHoverEvents(true);
}
// 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.
{
setBrush(Qt::black);
}
void Square::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
setBrush(Qt::black);
}
{
setBrush(Qt::black);
}
#include "square.h"
Square::Square(int x, int y)
{
// draw a square
setRect(x,y,10,10);
pen.setBrush(Qt::NoBrush);
setPen(pen);
setBrush(Qt::cyan);
setAcceptDrops(true);
setAcceptHoverEvents(true);
}
// 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.
void Square::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
setBrush(Qt::black);
}
void Square::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
setBrush(Qt::black);
}
void Square::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
setBrush(Qt::black);
}
To copy to clipboard, switch view to plain text mode
Bookmarks