Highlighting a QGraphicsItem in a QGraphicsScene



  • Hi,

    I want to highlight a GraphicsRectItem in a QGraphicsscene if the mouse is over this item. For this purpose I reimplemented paint() of the item, asking for "option->state & QStyle::State_MouseOver". This works but the item keeps highlighted while the mouse button is hold down, even if the mouse is not over the item anymore and stops beeing highlighted if the mouse button is released. Is there any possibilty that the State_MouseOver-Flag is unset if the mouse is not over the item, even if the mouse button is hold down?

    @
    void PolygonLineHandle::paint(QPainter *painter,
    const QStyleOptionGraphicsItem *option, QWidget *)
    {
    QBrush b=brush();

    if (option->state & QStyle::State_MouseOver)
    b.setColor(Qt::cyan);

    painter->setBrush(b);
    painter->drawRect(rect());
    }
    @



  • This is not a definitive answer, just ideas.

    Study "mouse grab". When a mouse button is pressed in a widget, it grabs the mouse. Same thing for QGI's. The idea is that further mouse events are dispatched to the widget that has grabbed the mouse, even if the mouse leaves the widget. The design reasoning is that only that widget knows what to do with the mouse events. I think the concept of mouse grab is somewhat controversial, but it is in Qt. So my guess is that mouse grab affects the state and changes the way it is updated: doesn't update mouseOver???

    Also, have you looked at hover events?

    I need the same effect, a "highlight" of an item different from the automatic highlighting that Qt does when you select (or focus>) a QGI. But I will have a state variable in my class that I will maintain to indicate it should be highlighted.



  • You can reimplement hoveEvents. Somethin like this:
    highlightItem.h
    @
    #ifndef HIGHLIGHTITEM_H
    #define HIGHLIGHTITEM_H

    #include <QGraphicsRectItem>
    #include <QPen>

    class HighlightItem : public QGraphicsRectItem
    {
    public:
    explicit HighlightItem(QGraphicsRectItem *parent = 0);

    protected:
    void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
    void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

    private:
    QPen m_pen;
    };

    #endif // HIGHLIGHTITEM_H
    @

    highlightItem.cpp
    @

    #include "highlightitem.h"

    #include <QPainter>

    HighlightItem::HighlightItem(QGraphicsRectItem *parent) :
    QGraphicsRectItem(parent)
    {
    setAcceptHoverEvents(true);
    }

    void HighlightItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
    {
    m_pen.setColor(Qt::cyan);
    update();
    }

    void HighlightItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
    {
    m_pen.setColor(Qt::black);
    update();
    }

    void HighlightItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
    painter->setPen(m_pen);

    painter->drawRect(rect());
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.