PDA

View Full Version : Carousel with QLabels



ZajtiM
18th September 2021, 22:22
I would like to create carousel with QLabels. Nothing special. Each QLabel will contain only text. I'm not using QML.

Just a quick qt designer example. QLabels will be added/created programmatically, not in designer.
13722

My first idea was to add all QLabels in QHBoxLayout and us hide()/show() to create carousel like view.

Any ideas or suggestions are welcome.

d_stranz
19th September 2021, 00:38
Any ideas or suggestions are welcome.

Use the Flow Layout from this example (https://doc.qt.io/qt-5/qtwidgets-layouts-flowlayout-example.html). Add your layout into a QWidget and put the QWidget into a QScrollArea. If you handle the size hint of the widget correctly, the scrolling should work automatically. If you don't like the scrollbar at the bottom, you can hide the scrollbars, make an h box with your own arrows at left and right (as you show in your screenshot) and the flow layout in the middle, and handle the scrolling manually via button clicks. Even better would be the ability to grab a label and move the whole thing left and right.

Hiding and showing widgets in an h box will not give a very nice visual experience. As you hide or show widgets, all of the others will appear to jump to new positions as the layout is recalculated. A better experience would be a smooth scrolling from side to side.