1

I need to add a "hover effect" to some QPixmaps added to a QGraphicsScene. I would like to "highlight" my QPixmap by filling it with a halfway transparent white color when the user hovers over it. If at all possible I want to avoid using the setPixmap(QPixmap) method to exchange my pixmap with a premade "hover image". This is what I've got so far:

import com.trolltech.qt.gui.QGraphicsPixmapItem;
import com.trolltech.qt.gui.QGraphicsSceneHoverEvent;
import com.trolltech.qt.gui.QPixmap;

public class SelectablePixmapItem extends QGraphicsPixmapItem {

    private QPixmap pixmap;

    public SelectablePixmapItem(QPixmap pixmap) {
        super(pixmap);
        setAcceptHoverEvents(true);
        setItemPixmap(pixmap);
    }

    private void setItemPixmap(QPixmap pixmap) {
        this.pixmap = pixmap;
    }

    @Override
    public void hoverEnterEvent(QGraphicsSceneHoverEvent e) {
    }

    @Override
    public void hoverLeaveEvent(QGraphicsSceneHoverEvent e) {
    }
}

Update: it does capture the events by the way :)

Kai
  • 38,985
  • 14
  • 88
  • 103
Benjamin
  • 541
  • 1
  • 9
  • 17
  • Does your version of Qt Jambi have [QGraphicsEffects](http://doc.qt.nokia.com/latest/qgraphicseffect.html)? – Mat Dec 04 '11 at 14:24
  • Yes. It does......... sorry for the many dots. Word minimum :) – Benjamin Dec 04 '11 at 14:35
  • Why don't you just draw a transparent rectangle (e.g. `QColor(255, 255, 255, 128)`) over the image? It will probably be faster than QGraphicsEffects. – D K Dec 04 '11 at 14:35
  • How would I so so? I have tried doing that using the fill method of QPixmap but filling it with that color will replace the pixels, not add them as a extra layer. – Benjamin Dec 04 '11 at 14:38

1 Answers1

1

If you know the coordinates of the pixmap, you can do:

graphicsscene.addRect(pixmap.rect(),
                      new QPen(),
                      new QBrush(new QColor(255, 255, 255, 128)));

to create a transparent white rectangle on top of the pixmap.

(Sorry if my Java is bad, I am adapting what I know from PyQt style).

D K
  • 5,530
  • 7
  • 31
  • 45