PDA

View Full Version : Synchronizing scrollbars on two QListWidget



atlantis210
21st May 2014, 09:35
Hi everyone,

I'm new to this forum, so I hope I don't make any mistake described in forum's rules :)

Here's my problem. I have two QListWidget with scrollbars:
I'd like to synchronize the two scrollbars when scrolling down or up.

I searched on the internet and on Qt's help but I didn't find any signal or function helping me.

Do I have to create my own scrollbar class (thing I don't particularly want to do) or is there anything else I could do?

Thanks in advance

atlantis210
21st May 2014, 16:27
I'd like to add that those scrollbars are from the QListWidget. It's not a QScrollBar I added

d_stranz
21st May 2014, 18:20
Retrieve the two vertical scrollbars from each list widget (QAbstractScrollArea::verticalScrollBar()). Connect slots to the QAbstractSlider::valueChanged() signals for each one, and in those slots, call the QAbstractSlider::setValue() slot for the other one.

You have to be careful about this - if you don't set a flag or do something else to block it, you will end up with an infinite recursion loop, since calling setValue() results in the valueChanged() signal, which will then trigger your slot, where you once again change the value and get another signal... It might be easiest to check to see if the value has changed, and decide to call the other scrollbar's slot only if it has changed.

All of this assumes that your two list widgets have the same content, so that calling setValue on one refers to the same position in the other.

If all you want to do is to ensure that when an item is selected in one view, it is scrolled to in the other, then you can use the QListWidget::scrollToItem() slot in response to an itemClicked() or currentItemChanged() signal from the other view.

atlantis210
22nd May 2014, 08:37
Thanks a lot, it works!

I post the solution for one scrollbar if others have the same problem:



_currentSliderValue = 0;
QScrollBar *lFctScrollBar = _fctWidget->verticalScrollBar();
QScrollBar *lValueScrollBar = _valFctWidget->verticalScrollBar();
connect(lFctScrollBar, &QAbstractSlider::valueChanged,
[=](int aSliderPosition){if(aSliderPosition != _currentSliderValue)
{
lValueScrollBar->setValue(aSliderPosition);
_currentSliderValue = aSliderPosition;
}
});

Do the same connect for the second.

Thanks d_stranz!