PDA

View Full Version : CSS Related Problem



GabrielGray
30th June 2010, 21:50
So, i have a small login form, and i want to put it lying on a image made by me.
In order to do this, i created a frame, and customized it with stylesheets so the background-image is my image and put the form inside in a layout.

the problem is when i resize the window, the image stays always the same size, which blows up my layout.

How do i make the image stretch and shrink so it always fits the form?

Thanks

dflo
8th April 2011, 06:19
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!



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();
}


The shaped clock example is a great reference for other painter capabilities! http://doc.trolltech.com/widgets-shapedclock-shapedclock-cpp.html

Good luck!