0

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.

habibhassani
  • 486
  • 1
  • 6
  • 15
  • 1
    If you want to know the child widget at a specific position, use [`childAt()`](https://doc.qt.io/qt-5/qwidget.html#childAt-1). Be aware, though, that if you want to track mouse event for a specific widget, then you should either subclass that widget or use an event filter. – musicamante Oct 27 '22 at 17:26
  • Thank you for your answer. I want to know if the mouse is not hovering over the frame, and how to stop the mouseMoveEvent from executing at mousePressEvent, or for none of them to happen in the first place when I click outside the frame. – habibhassani Oct 27 '22 at 23:50
  • 1
    Are you trying to create a "touch to scroll" interface? If that's the case, then you should be using a QScrollArea and [QScroller](https://doc.qt.io/qt-5/qscroller.html). – musicamante Oct 28 '22 at 01:01

0 Answers0