简体   繁体   中英

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 . Similar to Windows Media Player CD import, there should be a progress bar for every item in the list.

Now there seems to be a way to do this by creating a regular progress bar, using QPixmap::grabWidget() to save its appearance in a QPixmap and then adding this QPixmap as Icon to the QListWidgetItem via QListWidgetItem::setIcon() . However, this seems to be horribly wacky.

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

Each item in a QListWidget can be represented by a QWidget of your choice, rather than the default rendering (text). You can set this by calling QListWidget::setItemWidget() . In this case, I'd recommend using QProgressBar as the 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 the place of a list widget item. If you want to display custom dynamic content or implement a custom editor widget, use QListView and subclass QItemDelegate instead.

You could do it by converting your list widget into a model/view/delegate combo. Then you can set a delegate on the list view that overrides the paint functions and draws the progress bar wherever you want it. I don't know how easy it would be to get an actual QProgressBar widget into the drawing area, however.

Alternately, you could consider making your own list-widget like container that knows about the progress bars.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM