Results 1 to 4 of 4

Thread: Synchronizing scrollbars on two QListWidget

  1. #1
    Join Date
    May 2014
    Posts
    7
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Synchronizing scrollbars on two QListWidget

    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
    Last edited by atlantis210; 21st May 2014 at 15:29.

  2. #2
    Join Date
    May 2014
    Posts
    7
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Synchronizing scrollbars on two QListWidget

    I'd like to add that those scrollbars are from the QListWidget. It's not a QScrollBar I added

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Synchronizing scrollbars on two QListWidget

    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.

  4. The following user says thank you to d_stranz for this useful post:

    atlantis210 (22nd May 2014)

  5. #4
    Join Date
    May 2014
    Posts
    7
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Synchronizing scrollbars on two QListWidget

    Thanks a lot, it works!

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

    Qt Code:
    1. _currentSliderValue = 0;
    2. QScrollBar *lFctScrollBar = _fctWidget->verticalScrollBar();
    3. QScrollBar *lValueScrollBar = _valFctWidget->verticalScrollBar();
    4. connect(lFctScrollBar, &QAbstractSlider::valueChanged,
    5. [=](int aSliderPosition){if(aSliderPosition != _currentSliderValue)
    6. {
    7. lValueScrollBar->setValue(aSliderPosition);
    8. _currentSliderValue = aSliderPosition;
    9. }
    10. });
    To copy to clipboard, switch view to plain text mode 
    Do the same connect for the second.

    Thanks d_stranz!

Similar Threads

  1. Synchronizing input and storage for settings
    By fruchtquark in forum Qt Programming
    Replies: 1
    Last Post: 29th November 2012, 22:20
  2. Replies: 7
    Last Post: 27th April 2012, 13:17
  3. synchronizing events
    By zaphod.b in forum Qt Programming
    Replies: 0
    Last Post: 14th July 2009, 16:29
  4. Synchronizing QFtp
    By arun_vj in forum Qt Programming
    Replies: 0
    Last Post: 5th November 2008, 12:31
  5. Synchronizing two GraphicsScenes
    By manojmka in forum Qt Programming
    Replies: 13
    Last Post: 3rd December 2007, 09:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.