I don't know if you can use CSS (presumably using setStyleSheet()) to cause a background image to resize as needed, but you can certainly do it by writing your own paintEvent() (and resizeEvent() too, for efficiency) in a subclass of QFrame:
Keep your original pixmap and your scaled_pixmap as members of your new class!
if (scaled_pixmap)
delete scaled_pixmap;
float width_f = float(width()) / float(pixmap->width());
float height_f = float(height()) / float(pixmap->height());
float zoom_f = (width_f < height_f ? width_f : height_f);
scaled_pixmap = pixmap->scaled(zoom_f, zoom_f, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
if (!scaled_pixmap) {
resizeEvent(&event);
}
painter.save();
painter.
fillRect(event
->rect
(),
QColor("black"));
// the zooming in resizeEvent() ensures that only one of these is non-zero
h_offset = (width() - scaled_pixmap->width()) / 2; // if frame is wider than image
v_offset = (height() - scaled_pixmap->height()) / 2; // if frame is taller
painter.drawPixmap(h_offset, v_offset, scaled_pixmap);
painter.restore();
}
void resizeEvent (QResizeEvent *) {
if (scaled_pixmap)
delete scaled_pixmap;
float width_f = float(width()) / float(pixmap->width());
float height_f = float(height()) / float(pixmap->height());
float zoom_f = (width_f < height_f ? width_f : height_f);
scaled_pixmap = pixmap->scaled(zoom_f, zoom_f, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
void paintEvent (QPaintEvent *event) {
if (!scaled_pixmap) {
QResizeEvent event; // ignored anyway, so just fake it
resizeEvent(&event);
}
painter = QPainter(this);
painter.save();
painter.fillRect(event->rect(), QColor("black"));
// the zooming in resizeEvent() ensures that only one of these is non-zero
h_offset = (width() - scaled_pixmap->width()) / 2; // if frame is wider than image
v_offset = (height() - scaled_pixmap->height()) / 2; // if frame is taller
painter.drawPixmap(h_offset, v_offset, scaled_pixmap);
painter.restore();
}
To copy to clipboard, switch view to plain text mode
The shaped clock example is a great reference for other painter capabilities! http://doc.trolltech.com/widgets-sha...clock-cpp.html
Good luck!
Bookmarks