pyside6 在移动窗口后有时鼠标移入事件不触发
```python
import sys
from typing import Tuple
import uuid
from PySide6.QtWidgets import QMainWindow,QScrollArea,QComboBox,QLineEdit,QRadioButton,QApplication,QWidget,QLabel,QPushButton,QSizePolicy,QButtonGroup,QPlainTextEdit
from PySide6.QtSvgWidgets import QSvgWidget
from styles.style_manager import manager_style,AutoHBoxLayout,AutoVBoxLayout
from PySide6.QtCore import QParallelAnimationGroup,QSequentialAnimationGroup,Qt,QPropertyAnimation,Property,QEvent,QEasingCurve,QObject, Signal,QSize,QTimer,QRect,QPoint
from PySide6.QtGui import QCursor,QTextBlockFormat,QTextCursor,QColor,QFontMetrics,QIcon,QPixmap,QMouseEvent,QEnterEvent,QFont,QBitmap,QPainter,QPen,QRadialGradient,QPainterPath
from PySide6.QtWidgets import QScrollBar, QStyle, QStyleOptionSlider, QVBoxLayout
from PySide6.QtCore import Qt, QTimer, QEvent
from PySide6.QtGui import QEnterEvent
from PySide6.QtGui import QBrush
from PySide6.QtGui import QRegularExpressionValidator
from PySide6.QtCore import QRegularExpression
import PySide6
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QGraphicsDropShadowEffect
h=34
class Top_bar(QWidget):
def __init__(self,parent=None):
super().__init__(parent)
self.is_open_menu=False
self.setFixedSize(all_with,h)
box=QWidget(self)
box.setObjectName("box")
box.setFixedSize(all_with, h)
self.setStyleSheet(manager_style.top_bar_style())
self.enter = QColor(0, 0, 0)
self.leave= QColor(255, 255, 255)
self.update_ui()
def enterEvent(self, event:QEnterEvent):
self.setStyleSheet(f"""
#box{{
background-color: {self.enter.name()};
border:1px solid rgba(170, 170, 170, 1);
border-top-left-radius:6px;
border-top-right-radius:6px;
border-bottom:none;
}}
""")
def leaveEvent(self, event:QEnterEvent):
self.setStyleSheet(f"""
#box{{
background-color: {self.leave.name()};
border:1px solid rgba(170, 170, 170, 1);
border-top-left-radius:6px;
border-top-right-radius:6px;
border-bottom:none;
}}
""")
def update_ui(self):
self.setStyleSheet(f"""
#box{{
background-color: {self.leave.name()};
border:1px solid rgba(170, 170, 170, 1);
border-top-left-radius:6px;
border-top-right-radius:6px;
border-bottom:none;
}}
""")
all_with=350
#透明的容器
class MainWidget(QWidget):
def __init__(self):
super().__init__()
self.setFocusPolicy(Qt.ClickFocus)
#隐藏标题栏
self.setWindowFlags(Qt.FramelessWindowHint)
#设置透明背景属性
self.setAttribute(Qt.WA_TranslucentBackground)
#不接受鼠标事件
# self.setAttribute(Qt.WA_TransparentForMouseEvents)
layout=QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
layout.setAlignment(Qt.AlignLeft | Qt.AlignTop )
top_bar1=Top_bar()
layout.addWidget(top_bar1)
top_bar2=Top_bar()
layout.addWidget(top_bar2)
self.top_bar=Top_bar()
layout.addWidget(self.top_bar)
self.setFixedSize(all_with,h*3)
self.setStyleSheet(manager_style.main_style())
self.dragging = False
self.top_bar.installEventFilter(self)
def eventFilter(self, obj, event):
# print(obj, event)
if obj == self.top_bar:
event_type = event.type()
# 鼠标按下
if event_type == QEvent.MouseButtonPress:
if event.button() == Qt.LeftButton:
self.dragging = True
self.drag_position = event.globalPosition().toPoint() - self.frameGeometry().topLeft()
return True
# 鼠标移动
elif event_type == QEvent.MouseMove:
if self.dragging and (event.buttons() & Qt.LeftButton):
self.move(event.globalPosition().toPoint() - self.drag_position)
return True
# 鼠标释放
elif event_type == QEvent.MouseButtonRelease:
if event.button() == Qt.LeftButton:
self.dragging = False
return True
return super().eventFilter(obj, event)
if __name__=="__main__":
app=QApplication(sys.argv)
window=MainWidget()
window.show()
sys.exit(app.exec())
```