Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Highlighting a QGraphicsItem in a QGraphicsScene

    General and Desktop
    3
    3
    11257
    Loading More Posts
    • 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
      maikelmeyers last edited by

      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 Reply Quote 0
      • B
        bootchk last edited by

        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 Reply Quote 0
        • R
          rokemoon last edited by

          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 Reply Quote 0
          • First post
            Last post