Nominate our 2022 Qt Champions!

For a given view, when do the scrollbar settings (min, max, size of scrollbar) get set?

  • I have a situation in which I will need to take manual control of the scrollbar; minimum, maximum, location of slider etc.

    Currently, when I change a model's contents, the scrollbar automatically updates. Does anyone happen to know when this happens? Is it triggered by a "model reset" or an "end insert rows" or some such? My options, I think, are to either find a way to tell the scrollbar that I will be updating it and it shouldn't do it by itself, or to wait until it has finished updating itself and then apply my changes. But to do that, I'll need to know when it will change. Does anyone know? I think there are some signals from the scrollbar I could watch, but that's getting very ugly then - that'd be just me watching it to see when it changed and blatting it back.

  • You need to look at specific view class,

    Model itself knows nothing about scrollbar or any other ui element.
    All it does it notifies about its own changes,

    I guess every view handles it differently.
    Even the same view in different modes may do it differently ( like in case when all rows have the same height vs each row has different height)
    Question has no answer the way it was asked.

  • Question has no answer the way it was asked.

    I was hoping for an answer like "when the model updates, the view receives the signal that the model has changed, and the view resets the scrollbar accordingly at that point."

    I suppose other possible answers could have been things like "the view recalculates the scrollbar values every time it redraws". As you say, it depends on how the view code was written. I was hoping someone would know.

  • Lifetime Qt Champion


    IIRC, changes to the model will trigger an updateGeometry call which will update the widget and what's inside.

  • By my comment above I was trying to say that it might be better to tell what is the problem
    which inspired you to look for solution.

    Even though we live in C++ world the simplest solution might not be an overriding behavior.
    Sometimes just disabling view update and updating it later may be sufficient.
    According to cases I faced changing model in some ways (for example resetting) forces views to forget everything: current selection, visible range etc.
    I could try to re-write view functionality, but
    the simplest way was to save state I wanted to keep and restore it after change in such cases.

    So unless you are satisfied by above answer which technically is correct you might want to re-phrase.

  • Okey dokey. The problem is that when the user scrolls towards the bottom, I'm going to remove the top thousand items in the model and put a new thousand in at the bottom. I need to make the scrollbar always look like we're scrolling through a few million items, even though there are only ever a few thousand. As such, I can't have the scrollbar updating itself based on the actual contents of the model (and thus what the view will actually be sowing).

  • First of all I have never done what you wanted, so my advises might not work well

    1. You might want to override :

    void QAbstractScrollArea::setupViewport ( QWidget * viewport ) [protected slot]
    This slot is called by QAbstractScrollArea after setViewport(viewport) has been called. Reimplement this function in a subclass of QAbstractScrollArea to initialize the new viewport before it is used.

    1. You can actually either hide scrollbar provided by QAbstractScrollArea and display your own instead or subclass scrollbar and set it to itemView.

Log in to reply