Scrolling over a series of buttons

  • Hello,

    I'd like to build an interface with a series of button aligned horizontally. The series will be wider than the window or screen and therefore it should be scrolled over. I would like the scrolling to occur not per pixels, but per button (so the scroll snaps to the buttons edge so to speak).

    How can I achieve that?

    I thought of using a QScrollArea, but it can have only one child, so buttons should be added horizontally to a QFrame for instance, and the QFrame would be set as child to QScrollArea. But then, how to ensure the per-button scrolling rather than per-pixel?

    Or is there a better way to do this? What do you reckon?


  • I think you will need to use ensureWidgetVisible(): perhaps subclass and override scrollContentsBy() to check to see if they have scrolled enough to snap to the next widget?

  • Thanks Chris,

    First of all I am not really confident with overriding scrollContentsBy() because I am not sure I understand what it really does.
    Second, if I understand what ensureWidgetVisible() does¹, then how can I tell which widget should be passed as the argument?
    Logically, I'd need to find out what buttons of the scroll area's child are partly visible, make out how much of their surface is visible. Then I should move programmatically the child so that buttons with a visibility of 50% or more are visible.
    Otherwise, if all the buttons are of equal size, I should be able to defined a unit of movement for the scroll area somehow.

    ¹ to move the widget of the scroll area so that the child of the widget specified as an argument and a specified margin around it, is visible.

  • Yeah, I'm being dumb, that wouldn't work. I think you really just want to use a QTableView, which will let you set a scroll policy that scrolls in entire-widget increments.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.