0
votes

I've got two widgets, their scrolls are controlled separately. ScrollBarPolicy for each widget — ScrollBarAsNeeded.

Also there is a checkbox which should synchronise the scrolling (horizontal) of both widgets. I would like to scroll one of them and the second should be scrolled too. How to implement that?

Also I would like to realise a case when I scroll 1st widget, the second would be scrolled too, but it should be impossible to scroll 2nd widget directly.

Two widgets — diagrams

2
What do you mean by "but I would scroll second at all."? - Gurushant
Woops. Sorry. I mean that it's impossible to scroll 2nd widget directly. - Arseniy Spiridonov

2 Answers

0
votes

For case 1:
Assuming your widgets are custom widgets if not derive them and make custom widget and add a custom signal for widget1 like ScrollEvent and connect the signal to a custom slot such as ScrollUpdate in widget2. Install eventfiter for your widget1, and in eventfilter watch out for QScrollEvent, when the event occurs emit your custom signal. In the slot of widget2 scroll to the position of widget1.

For case 2:
Implement the same thing as case1. In addition to that use the following code

ui->your_widget2->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);  

This makes the scroll bar disappear, so user can not scroll directly.

0
votes

In the constructor of the second object implemented two methods with ready signals and slots of QScrollArea:

connect(parentScrollArea->horizontalScrollBar(),
        SIGNAL(valueChanged(int)),
        relatedDiagram->parentScrollArea->horizontalScrollBar(),
        SLOT(setValue(int)));
connect(relatedDiagram->parentScrollArea->horizontalScrollBar(),
        SIGNAL(valueChanged(int)),
        parentScrollArea->horizontalScrollBar(),
        SLOT(setValue(int)));

I decided not to disable a direct scroll of the second object.

In the method of synchronisation I added for case of synchronisation:

connect(commonArea->horizontalScrollBar(),
        SIGNAL(valueChanged(int)),
        resultsArea->horizontalScrollBar(),
        SLOT(setValue(int)));
connect(resultsArea->horizontalScrollBar(),
        SIGNAL(valueChanged(int)),
        commonArea->horizontalScrollBar(),
        SLOT(setValue(int)));

And dissynchronization:

 disconnect(commonArea->horizontalScrollBar(),
               SIGNAL(valueChanged(int)),
               resultsArea->horizontalScrollBar(),
               SLOT(setValue(int)));
 disconnect(resultsArea->horizontalScrollBar(),
               SIGNAL(valueChanged(int)),
               commonArea->horizontalScrollBar(),
               SLOT(setValue(int)));