Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Highlighting a QGraphicsItem in a QGraphicsScene
Forum Updated to NodeBB v4.3 + New Features

Highlighting a QGraphicsItem in a QGraphicsScene

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 11.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maikelmeyers
    wrote on 20 Sept 2011, 14:34 last edited by
    #1

    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());
    }
    @

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bootchk
      wrote on 14 Nov 2011, 16:10 last edited by
      #2

      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.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rokemoon
        wrote on 15 Nov 2011, 04:52 last edited by
        #3

        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());
        }
        @

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved