View Full Version : QGraphicsview and mouse press event

21st January 2010, 04:55
Hi all,

I have a problem which need help.

I have a graphicsview which contains a graphicsscene. I want to do something on a mouse click on the graphicsview object.
How can I do this?

mainwindow .h

QGraphicsEllipseItem *item;
QGraphicsView *view;
QGraphicsScene *scene;


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
item = new QGraphicsEllipseItem;
item->setRect(50, 100, 5, 5); //for setting position and size

scene = new QGraphicsScene;
scene->setSceneRect(0, 0, 600, 600);

view = new QGraphicsView(scene);

21st January 2010, 05:37
reimplement the QGraphicsScen
void QGraphicsScene::keyPressEvent ( QKeyEvent * keyEvent ) [virtual protected]

21st January 2010, 06:59
reimplement the QGraphicsScen
void QGraphicsScene::keyPressEvent ( QKeyEvent * keyEvent ) [virtual protected]

I guess you meant QGraphicsScene::mousePressEvent

May be if you tell what you want to do on click, we can help you better

22nd January 2010, 00:00
actually I wanted to have something like when I click the mouse on the graphicsview, I will display a circle on that spot and remove any other previously over items items on the scene (except for the background).

which mousepressevent should I edit? the graphicsview, graphicsscene or the graphicellipse?

I have another problem. How can I prevent tiling of the Qpixmap in graphivew if my pixmap is smaller than the scene rect?

22nd January 2010, 02:47
I manage to get the mouse press event to work. However I still have the tiled background issue.

I loaded map onto the graphicsview by using setbackgroundbrush. but I notice that it got tiled if the graphicsScene scene rect is bigger than the map.

How can I prevent this? I want to just white/ block out the areas that is not covered by the map.

Furthermore, I have this scrollbar which will change the size of the map (via matrix transform). I would like the white/black out area to not show after the map is large enough to cover the graphicsScene.

22nd January 2010, 05:20
Dont set the brush. Draw the background yourself using QGraphicsScene::drawBackground or QGraphicsView::drawBackground

26th January 2010, 06:04
thx. by the way, is there any way to remove the background brush after involving setBackgroundBrush?