1
import sys

from PySide6.QtWidgets import (
    QApplication,
    QWidget,
    QVBoxLayout,
    QGridLayout,
    QGroupBox
)

class EmailBox(QWidget):
    def __init__(self) -> None:
        super().__init__()
        self._emailbox_layout = QVBoxLayout()
        self.setLayout(self._emailbox_layout)

        self._emailbox_layout.addWidget(self._Draw())

    def _Draw(self):
        self._gb = QGroupBox()
        self._layout = QGridLayout()
        self._layout.addWidget(self._gb)

        if (the cursor is hovering over the groupbox) :
            self._hovered
        else:
            self._not_hovered: ...

        return self._gb

    
    def _hovered(self):
        print(1)

    def _not_hovered(self):
        print(2)

def main():
    app: QApplication = QApplication(sys.argv)
    ex = EmailBox()
    ex.show()
    sys.exit(app.exec())

if __name__ == "__main__":
    main()

What I want is to implement into the code here above a check that checks whether the cursor is hovered over a group box, here the self._gb group box, and then do some logic defined by the _hovered() function. If the cursor leaves the group box, or is simply not hovered over it, then implement the logic of _not_hovered(). This is order to change some QPushButtons that would be put into the group box. Uncovered, there are no buttons, then when it is hovered, it add three buttons to the group box to the right. I know how to implement this, I just don't know the proper way to implement a way to see if the group box is hovered or not...

relu
  • 11
  • 2
  • 1
    What you want to do cannot be implemented right in the `__init__`, and for two obvious reasons: 1. at that point, the main widget is not even shown; 2. it cannot take into account mouse movements that happen *after* it's being shown. You could subclass QGroupBox with a custom signal, and override `enterEvent()` and `leaveEvent()` so that it emits that signal accordingly, then connect that signal to the function in the main class which will do what required whether the box is *entered* or *left* by the mouse cursor. – musicamante Aug 19 '23 at 21:47

0 Answers0