0

I have a 150x450 QScrollArea with a VBoxLayout in it. I have to dynamically (while a video is being played, frame by frame) add an unspecified amount of QLabels, ranging from none to hundreds.

When I start adding, QLabels start appearing exactly in the middle. Then when another appears, they shift so the middle is exactly between them. So on and so on.

How can I make them appear from the very top and just go down? Without shifting positions and wiggling?

Petersaber
  • 851
  • 1
  • 12
  • 29
  • Use stretch: http://doc.qt.io/qt-5.4/qboxlayout.html#addStretch – Amartel Jun 12 '15 at 10:17
  • Maybe `QVBoxLayout::addStretch(1)` at the bottom of layout would help? It will "push" all content to the top. – vahancho Jun 12 '15 at 10:17
  • @vahancho that starts it off at the bottom, and they still violently shake every time one is added. But that's a push in the right direction, I'll dig into that. – Petersaber Jun 12 '15 at 10:20
  • @Petersaber, you need to place a stretch at the bottom of your layout: `layout->addWidget(w1); layout->addWidget(w2); [..] layout->addWidget(w100); layout->addStretch(1);`. From the other hand, if you want to incrementally add new items why don't you consider using `QTableView` or `QTableWidget`? – vahancho Jun 12 '15 at 10:29
  • @vahancho i really forgot to mention. I am adding these QLabels "live", while a video is playing, from different threads... so that solution is impossible. – Petersaber Jun 12 '15 at 10:35
  • @Petersaber, why it is impossible? You cannot add new rows in the table at run-time? Please also note that having hundreds of labels in the application will cost you much memory and performance. – vahancho Jun 12 '15 at 10:44
  • @vahancho that's because in your solution, the labels would align topside AFTER the video is done playing, and not one after another. I'd have to add the stretch after every frame in a layout created via Designer and not code line, and for some reason it refuses to scroll (new labels eventually overlap each other) . I really don't want to move the function from one cpp to another just for the komunikatyLayout (created from code, not Designer) to be detected by QtCreator (it produces an error otherwise) – Petersaber Jun 12 '15 at 10:55
  • @vahancho you weren't kidding about the performance impact. I'll have to rethink the whole thing. Maybe have no more than 15 labels that get cleared once the box fills up, removing the need for a scrollbar. Or maybe there's another, less performance-intensive way to display hundreds of errors..... I'm still new to Qt – Petersaber Jun 12 '15 at 11:21
  • I agree with the latter idea of @vahancho. I'd strongly suggest using a `QTableView` or here even a `QListview`. They already have such layout behaviour and you should be able to style those to your liking without much problems – Bowdzone Jun 12 '15 at 11:26
  • @vahancho using the listView was a wonderful idea. Thank you! Feel free to post it as an answer – Petersaber Jun 12 '15 at 12:05

1 Answers1

1

Having hundreds of labels in the application and layouting them in your scroll area will cost you much memory and performance. From the other hand Qt has the number of dedicated classes to handle multiple items in a scroll area such as: QTableWidget, QListWidget, QTableView etc. All these classes designed to handle rows of items and have all related functionality. Using them will free you from taking care about layouts, scrolling and so on.

vahancho
  • 20,808
  • 3
  • 47
  • 55