Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QScroller::LeftMouseButtonGesture on multiple scroll areas



  • I have multiple stacked pages in a stacked widget. Several of those pages have QScrollAreas on them.

    I would like each of the scroll areas to allow the user to scroll left/right in the area with a touch or leftclick gesture. Below I try setting the LeftMouseButtonGesture on each of the scrollAreas, depending on the page that is at the top of the stack (visible).

    But i'm getting some very strange behavior that is hard to describe. When I first start my application, the LeftMouseButtonGesture works to scroll horizontally on both pages, but after I switch back and fourth between the two pages, it always stops working on the second of the two while continuing to work on the first.

    Is there a better way to achieve this, or something I should be doing differently? I've tried both of the following ways:

    Method 1 - on each page change of the stacked widget:

    void MainWindow::on_stackedWidget_currentChanged(int pageNum)
    {
    	switch(pageNum)
    	{
    	case Pages::pg_1:
    		QScroller::grabGesture(ui->scrollArea_Page1, QScroller::LeftMouseButtonGesture);
    		break;
    
    	case Pages::pg_2
    		QScroller::grabGesture(ui->scrollArea_Page2 QScroller::LeftMouseButtonGesture);
    		break;
    
    	//More page cases will go here.
    	
    	}
    }
    

    Method 2 - Set all gestures in my MainWindow Init.

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
    	ui(new Ui::MainWindow),
    {
        ui->setupUi(this);
    
        QScroller::grabGesture(ui->scrollArea_Page1, QScroller::LeftMouseButtonGesture);
        QScroller::grabGesture(ui->scrollArea_Page2 QScroller::LeftMouseButtonGesture);
    }
    

    Both methods seem to produce the same result where one of the two pages stops scrolling via the gesture (both are always scroll-able via the scroll bar at the bottom).



  • @DaveK-0 I have the same problem.


  • Lifetime Qt Champion

    Hi
    It seems to be related to being inside a QStackedWidget.
    It works as expected if the QScrollAreas are just next to each other.
    However, if you put them in stacked. It stop working.

    The docs states
    "Note: To avoid unwanted side-effects, mouse events are consumed while the gesture is triggered. Since the initial mouse press event is not consumed, the gesture sends a fake mouse release event "
    and i think its related to this. but i dont know how/why.


Log in to reply