View Full Version : Custom QWidget with more than 2 controls including buttons inside QListView

15th May 2011, 23:52
I want to put a QWidget inside QListView, tried with setItemWidget and not worked, but now I'm using QListView in a new project and need to do this.

It's a download list.

How can I do this ?

16th May 2011, 00:28
QListView (http://doc.trolltech.com/latest/qlistview.html#details) is a different thing from QListWidget (http://doc.qt.nokia.com/latest/qlistwidget.html) which i assume you want to use because this one has setItemWidget (http://doc.qt.nokia.com/latest/qlistwidget.html#setItemWidget).

16th May 2011, 01:15
I've used the Widget before, but not worked, now I've started another project using QListView and want to do this using it. I'm trying to do this with delegates.

How I can calculate the sizes ?

I'm using this code but I don't think it's the best way:

void DownloadItemDelegate::paint(
QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index) const
painter->drawText(option.rect.x() + 2, option.rect.y() + option.fontMetrics.height(), index.data(Qt::DisplayRole).toString());

QStyleOptionProgressBarV2 sop;
sop.state = QStyle::State_Enabled;
sop.rect = option.rect.adjusted(2, option.fontMetrics.boundingRect(index.data(Qt::Dis playRole).toString()).height() + option.fontMetrics.height() / 2, -2, 0);
sop.fontMetrics = option.fontMetrics;
sop.minimum = 0;
sop.maximum = 100;
sop.progress = 50;
sop.orientation = Qt::Horizontal;
sop.textVisible = false;

QApplication::style()->drawControl(QStyle::CE_ProgressBar, &sop, painter);

QSize DownloadItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
if (option.rect.width() == 0 || option.rect.height() == 0) return QSize();

QSize size = option.rect.size();
size.setHeight(option.fontMetrics.boundingRect(ind ex.data(Qt::DisplayRole).toString()).height());
size.setHeight(size.height() + 20);

qDebug() << size.width() << "x" << size.height();
return size;