PDA

View Full Version : QProgressBar in QTreeWidget?



lvi
3rd September 2008, 15:17
I have a QTreeWidget that displays information. It only has top level items, each having 5 fields. Right now, these 5 columns are all text (QString). The last one represents some kind of progress, so I'd like this to be a progress bar (QProgressBar).

The documentation of QTreeWidget says:

void QTreeWidget::setItemWidget ( QTreeWidgetItem * item, int column, QWidget * widget )
Sets the given widget to be displayed in the cell specified by the given item and column.
Note that the given widget's autoFillBackground property must be set to true, otherwise the widget's background will be transparent, showing both the model data and the tree widget item.
This function should only be used to display static content in the place of a tree widget item. If you want to display custom dynamic content or implement a custom editor widget, use QTreeView and subclass QItemDelegate instead.
This function was introduced in Qt 4.1.
See also itemWidget() and Delegate Classes.

So, supposedly I can set any widget for any cell. But I'm unsure of what is meant with

This function should only be used to display static content in the place of a tree widget item.
A progress bar is not static, so it's implied I cannot use it. Or does "static" refer to the fact that it should remain the same widget one it's set?
Or should I use QAbstractItemModel with QTreeView instead? (I have tried this approach briefly, but with no success so far. I'd like to use the QTreeWidget, because it's easier to use, rather that putting many hours in the abstract model.)

spirit
3rd September 2008, 16:47
why you can't write your own delegate with progress bar?

lvi
3rd September 2008, 17:04
How do you mean?
I'm not very familiar with the MVC idea, and I was hoping to get by the easy way ;)
I tried to just use QTreeWidget::setItemWidget() and it seems to work just fine. I was just wondering if this is a good approach, or that I'm doing it all wrong...

spirit
3rd September 2008, 17:12
simple delegate example


QWidget *MyItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QProgressBar *bar = new QProgressBar(parent);
bar->setMaximum(100);
bar->setValue(50);
return bar;
}

void MyItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
{
editor->setGeometry(option.rect);
}


in widget:


...
QTableWidget *tw = new QTableWidget(10, 2, this);
tw->setItemDelegate(new MyItemDelegate);
...

jpn
3rd September 2008, 17:27
Please search the forums:

http://www.qtcentre.org/forum/f-qt-programming-2/t-how-to-show-progess-in-a-qtreeview-item--2330.html#10

lvi
3rd September 2008, 20:48
Please search the forums:

http://www.qtcentre.org/forum/f-qt-programming-2/t-how-to-show-progess-in-a-qtreeview-item--2330.html#10

Thank you for pointing me there. Your example in that thread is extremely useful!

I'm not sure whether I forgot to search or used the wrong terms, but I definitely need to sleep more ;)