Qt Forum

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

    Update: Forum Guidelines & Code of Conduct

    Solved How to access a QGraphicsItem's state in paint event via the option parameter?

    Qt for Python
    3
    4
    225
    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.
    • Niagarer
      Niagarer last edited by Niagarer

      Hi!
      I have a working C++ code snippet where I check for a QGraphicsItem placed in a QGraphicsScene whether the mouse is hovering over it:

      void ParameterPin::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
      {
          Q_UNUSED(widget);
      
          QPen pen = QPen(QBrush(QColor(150, 150, 150)), 2);
          QBrush brush = QBrush(_color);
      
          if(option->state & QStyle::State_MouseOver) {
              brush.setColor(_color.lighter());
          }
      
          // Point
          painter->setPen(pen);
          painter->setBrush(brush);
      
          painter->drawEllipse(0, 0, 15, 15);
      }
      
      

      The option parameter is of QStyleOptionGraphicsItem class which according to the docs inherits from QStyleOption. QStyleOption has a state variable which is a state enum representing different states including the MouseOver state.

      For some reason, the same code shown above doesn't work with PySide2. The PySide2 documentation is uncomplete there and I can't find a way to accomplish the same. Any suggestions?

      An approach like this:

                  if option.state and QStyle.State_MouseOver:
                      color = color.lighter()
      

      always returns true.
      Thanks for answers!

      JonB 1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        & and "and" are not the same operation. You need the former which is a "bitwise and".

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        Niagarer 1 Reply Last reply Reply Quote 4
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          Hi,

          & and "and" are not the same operation. You need the former which is a "bitwise and".

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          Niagarer 1 Reply Last reply Reply Quote 4
          • JonB
            JonB @Niagarer last edited by

            @Niagarer
            As @SGaist has said. Just so you know when reading C++ code, it is && in C++ which equates to Python and. Single & C++ operator is same as Python's &.

            1 Reply Last reply Reply Quote 1
            • Niagarer
              Niagarer @SGaist last edited by

              @SGaist oh my goodness, totally, thank you so much!

              1 Reply Last reply Reply Quote 0
              • First post
                Last post