That QRect is actually based on the sizeHint you return.
The framework first calls sizeHint for the delegate and then paints it.
Following that frame of thought, then making wrapping text work in a QTreeView and QTableView is different then in the QListView.
I haven't tested this yet but I think you would do something like this:
{
int width;
if (qobject_cast<QColumnView *>(parent()) != 0) {
QColumnView *v = qobject_cast<QColumnView *>(parent());
width = v->columnWidths().at(index.column());
} else if (qobject_cast<QHeaderView *>(parent()) != 0) {
//i'm going to leave this out for now because I don't know how to get the width of a QHeaderView cell
width = 100; //dumb value
} else if (qobject_cast<QTableView *>(parent()) != 0) {
QTableView *v
= qobject_cast<QTableView
*>
(parent
());
width = v->columnWidth(index.column());
} else if (qobject_cast<QTreeView *>(parent()) !=0) {
QTreeView *v
= qobject_cast<QTreeView
*>
(parent
());
width = v->columnWidth(index.column());
} else if (qobject_cast<QListView *>(parent()) != 0) {
QListView *v
= qobject_cast<QListView
*>
(parent
());
width = p->viewport()->size().width();
} else {
//specify default value if we are using a non-standard view
width = 400;
}
// ... calculate text wrapping and cell/row height based on width.
// ... future feature: specify max # of text lines and then elide the text if it's too long. (see QFontMetrics::elidedText() method)
}
QSize PluginDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
{
int width;
if (qobject_cast<QColumnView *>(parent()) != 0) {
QColumnView *v = qobject_cast<QColumnView *>(parent());
width = v->columnWidths().at(index.column());
} else if (qobject_cast<QHeaderView *>(parent()) != 0) {
//i'm going to leave this out for now because I don't know how to get the width of a QHeaderView cell
width = 100; //dumb value
} else if (qobject_cast<QTableView *>(parent()) != 0) {
QTableView *v = qobject_cast<QTableView *>(parent());
width = v->columnWidth(index.column());
} else if (qobject_cast<QTreeView *>(parent()) !=0) {
QTreeView *v = qobject_cast<QTreeView *>(parent());
width = v->columnWidth(index.column());
} else if (qobject_cast<QListView *>(parent()) != 0) {
QListView *v = qobject_cast<QListView*>(parent());
width = p->viewport()->size().width();
} else {
//specify default value if we are using a non-standard view
width = 400;
}
// ... calculate text wrapping and cell/row height based on width.
// ... future feature: specify max # of text lines and then elide the text if it's too long. (see QFontMetrics::elidedText() method)
}
To copy to clipboard, switch view to plain text mode
I'm sure there is a way to optimize that whole thing too, of course.
Now if you know for sure that your delegate will only ever be used in a QListView then don't do the above.
Bookmarks