How to draw a progress bar inside a list widget in Qt

I want to have a list of items that need to be processed in a QListWidget . As with importing Windows Media Player CDs, there should be a progress bar for each item in the list.

Now there is a way to do this by creating a regular progress bar, using QPixmap::grabWidget() to save its appearance in QPixmap , and then adding this QPixmap as an icon in QListWidgetItem via QListWidgetItem::setIcon() . However, this seems terribly stupid.

Do you know a more elegant way to achieve a progress bar inside a list widget?

+4
source share
2 answers

Each element in a QListWidget can be represented by a QWidget of your choice, and not by default (text). You can set this by calling QListWidget::setItemWidget() . In this case, I recommend using QProgressBar as a rendering widget - you should get the desired result.

From the documentation of QListWidget::setItemWidget() :

This function should only be used to display static content in place of a list widget element. If you want to display custom dynamic content or implement your own editor widget, use QListView and a subclass of QItemDelegate instead.

+5
source

You can do this by converting the list widget to the model / view / delegate command. You can then set the delegate in the list view, which overrides the drawing functions and draws a progress bar, wherever you want. I don't know how easy it would be to get the actual QProgressBar widget in the drawing area.

Alternatively, you might consider creating your own list widget, such as a container that knows about the execution steps.

+1
source

All Articles