You don't need to use drawForeground. You only need to reimplement the paint() method of your item class (which you probably already did) and mouse press event to modify the item when it's clicked.
You don't need to use drawForeground. You only need to reimplement the paint() method of your item class (which you probably already did) and mouse press event to modify the item when it's clicked.
I'm afraid I don't really get it. This is my GraphicsItem code:
Qt Code:
#include <QtGui> #include <QPainter> #include <iostream> #include "Graphics.hpp" Graphics::Graphics(): color(qrand() % 256, qrand() % 256, qrand() % 256) { setCursor(Qt::OpenHandCursor); } { } { Q_UNUSED(option); Q_UNUSED(widget); painter->setPen(Qt::NoPen); painter->setBrush(Qt::green); painter->drawRect(0, 0, 10, 20); } { }To copy to clipboard, switch view to plain text mode
I have read that there is no such thing as a "repaint"-method. So know I'm wondering how to call a kind of repaint from in mousePressEvent??
As I said in your other thread, you have the update() method, that will just call paint !![]()
if you want to change the color of the item upon selection then restore the color again upon de-selection:
Set ItemIsSelectable flag in Graphics::Graphics()
then in Graphics::paint() change the color of the brush if the item is selectedQt Code:
setFlag(ItemIsSelectable);To copy to clipboard, switch view to plain text mode
Qt Code:
if (isSelected()) painter->setBrush(Qt::black); else painter->setBrush(Qt::green);To copy to clipboard, switch view to plain text mode
if you want to change the color of the item upon selection forever:
no flags, just reimplement mousePressEvent()
then your Graphics::paint() should set the brush color:Qt Code:
{ if (brushcolor == Qt::green) brushcolor = Qt::black; update(); }To copy to clipboard, switch view to plain text mode
and in your constructor:Qt Code:
painter->setBrush(brushcolor);To copy to clipboard, switch view to plain text mode
also, don't forget to define brushcolor as member of Graphics class:
i tried both, and they worked fine with me. except the second method, two items are selected at a time. i think you have something strange in your bounding rect or i did something wrong.Qt Code:
private: ... Qt::GlobalColor brushcolor;To copy to clipboard, switch view to plain text mode
Regards,
-Mustafa
Wirloff (10th April 2007)
Thanx, I have got I workingI understand everything now, except one thing: what does the next sentence do:
Qt Code:
To copy to clipboard, switch view to plain text mode
Thanx again to all of you
This sentence calls the default implementation of this method.
Really useful, you don't have to handle all the possible events : just reimplement the one you are interested in, and let the default code handle the others ;-)
Everything works very fine but now I have another (new) problem:
When clicked on a rectangle, the main program should be able to know where there has been clicked. Is it possible to send a signal to the Scene from within eg: Item's mouseEvent???
Well, items are not QObject, so you can't use signals / slots, unless you made a subclass which inherit QObject
You can always have access to the scene pointer with scene() (only if you stored your item in the scene).
QGraphicsMouseEvent has useful methods to obtain position : pos() -> item coordinates, scenePos() -> scene coordinates.
If an item ignores an event, it will propagate to the scene and can be handled there. I don't know if it is exactly what you want... Maybe it'll be easier to call some method of the scene in item's mouse event handler instead?
Bookmarks