Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to access a QGraphicsItem's state in paint event via the option parameter?

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

Scheduled Pinned Locked Moved Solved Qt for Python
4 Posts 3 Posters 569 Views
  • 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.
  • NiagarerN Offline
    NiagarerN Offline
    Niagarer
    wrote on last edited by Niagarer
    #1

    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!

    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      NiagarerN 1 Reply Last reply
      4
      • NiagarerN 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!

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @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
        1
        • SGaistS SGaist

          Hi,

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

          NiagarerN Offline
          NiagarerN Offline
          Niagarer
          wrote on last edited by
          #4

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

          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