PDA

View Full Version : QItemDelegate painting stretched pixmap



aguayro
4th July 2012, 22:45
hi, i'm trying to make a custom delegate for a QTableView, the image is displayed, but is ugly because it's streched to the cell size, there is anyway to avoid that? just show the image with normal scale inside the cell.
this is my paint method:


void rating_delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {

int rate = index.data(Qt::DisplayRole).toInt();
if(rate > 0) {
QPixmap *star = new QPixmap(QString("Skin/%1/Sys/rate%2.png").arg(CurrentSkin).arg(rate));
painter->drawPixmap(option.rect, *star, star->rect());
}
}


And the results:
7949

ChrisW67
5th July 2012, 03:10
hi, i'm trying to make a custom delegate for a QTableView, the image is displayed, but is ugly because it's streched to the cell size, there is anyway to avoid that? just show the image with normal scale inside the cell.


As the QPainter::drawPixmap() documentation describes:

Draws the rectangular portion source of the given pixmap into the given target in the paint device.
Note: The pixmap is scaled to fit the rectangle, if both the pixmap and rectangle size disagree.

So, determine a rectangle the same size as the pixmap that fits within the available painting rectangle and use that as the first argument. If the available space is smaller than the pixmap then determine the largest rectangle of same aspect ratio that will fit.

BTW: The QPixmap at line 5 does not need to be on the heap and is a memory leak as it stands.

aguayro
5th July 2012, 13:33
Thanks fot he answer.
Now i can draw the bitmap on screen at real scale, but if i resize the column, the pixmap doesn't hide, its stay drawed with same size, any idea?