문제

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 :)

도움이 되었습니까?

해결책

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).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top