Break a Chain of Events when I click outside the target QFrame

25 Views Asked by At

So I want to grab the QFrame and scroll it, so I'm using mouse-move-event to do it, but the problem I have is the event triggers even when I click outside the Frame that I set Mouse Tracking on. So I thought of stopping at least the mouse-Move-Event early at mouse-press-event, but the problem is the focusWidget method always returns the scroll area, so I can't filter out the other widgets, and the ignore method doesn't stop the chain of events execution.

Code Demo:

import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, 
QVBoxLayout, QFrame, QScrollArea)  

class Window(QMainWindow):
    def __init__(self):
        super(Window, self).__init__() 

    """supposed to stop the events' chain when it's not the scrolling frame"""
    def mousePressEvent(self, event): 
        if QApplication.focusWidget()!=scroll:
            print("ignore")
            event.ignore() 
    def mouseMoveEvent(self, event): 
        print("move")
        
app = QApplication(sys.argv)

layout = QVBoxLayout()

b_widget=QFrame()
b_widget.setStyleSheet("background-color: blue")       
b_widget.setMinimumSize(100, 50)         
r_widget=QFrame()
r_widget.setStyleSheet("background-color: red")       
r_widget.setMinimumSize(100, 50)            
g_widget=QFrame()
g_widget.setStyleSheet("background-color: green") 
g_widget.setMinimumSize(1000, 1000)

"""activating mouse tracking on the scrolling frame"""
g_widget.setMouseTracking(True)

scroll=QScrollArea()
scroll.setWidget(g_widget) 


layout.addWidget(r_widget)
layout.addWidget(b_widget) 
layout.addWidget(scroll) 

widget = QWidget()
widget.setLayout(layout) 

main=Window()
main.setCentralWidget(widget) 
    
main.show() 
app.exec()

Thank you for your help.

0

There are 0 best solutions below