[Interest] QStyledItemDelegate's sizeHint() height being ignored by QTableWidget?

Bob Hood bhood2 at comcast.net
Mon Jan 4 03:30:39 CET 2016


I'm using a QTableWidget (Qt 5.4.2) to display QImages in each cell of the 
table.  I'm using a QStyledItemDelegate for the table, and it overrides 
paint() and sizeHint().  Each QImage has been already been scaled from the 
original to 160x120, resulting in 160x108 due to maintaining aspect.  I'm 
using the examples/widgets/itemviews/stardelegate/ example as my guide on this 
one.

In my set up code, I set the delegate and the rows and columns:

     ui->table_Thumbnails->setItemDelegate(new ThumbDelegate);
     ...
     ui->table_Thumbnails->setColumnCount(cols);
     ui->table_Thumbnails->setRowCount(rows);

and then I set the horizontal and vertical labels:

ui->table_Thumbnails->setHorizontalHeaderLabels(header_labels);
     ...
ui->table_Thumbnails->setVerticalHeaderLabels(header_labels);

then I populate each table cell with the QTableWidgetItems that have their 
data() set to the scaled QImage ("scale" defaults to 160x120):

     QTableWidgetItem *item = new QTableWidgetItem(QTableWidgetItem::UserType);
     ...
     item->setData(Qt::DisplayRole, image.scaled(scale, keep_aspect ? 
Qt::KeepAspectRatio : Qt::IgnoreAspectRatio));
     ui->table_Thumbnails->setItem(row, col, item);

In my ThumbDelegate, I simply return the size of the QImage as the sizeHint():

     if(index.data().canConvert<QImage>())
     {
         QImage image = qvariant_cast<QImage>(index.data());
         return image.size();     // 160x108 due to aspect
     }

and then in my paint(), I just paint the QImage into the cell's rectangle:

     if(index.data().canConvert<QImage>())
     {
         QImage image = qvariant_cast<QImage>(index.data());

         painter->save();

         painter->setRenderHint(QPainter::Antialiasing, true);
         painter->setPen(Qt::NoPen);
         painter->setBrush(option.palette.foreground());
         painter->drawImage(option.rect, image);

         painter->restore();
     }

This is resulting in cells that appear to be the correct width, but whose 
heights are nowhere near the 108 pixels of the QImage. So, of course, my 
images are squashed up into the available space to a point where they are not 
discernible:



Am I going to have to use a QTableView with a model in order to get the height 
I need in each cell, or might this be a bug?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160103/e0a69236/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bgacahei.png
Type: image/png
Size: 6274 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160103/e0a69236/attachment.png>


More information about the Interest mailing list