QWidget background-image fit

2019-07-07 09:34发布

问题:

I'm working with Qt 4.7 , I set a QWidget's background-image CSS an image from my qrc.

The problem is the image is High res , and only the upper left part of it is showing , I can't get it to scale down to fit. In CSS3 I saw a "background-size : contain" property but I fear it doesn't work in Qt 4.7.

Couldn't find a way to make the image fit the window. Any ideas ? I don't mind doing it programmatically .

Thanks

Solved: http://www.developer.nokia.com/Community/Wiki/Archived:Load,_Resize_image_and_set_background_image_in_Qt_application/widget

回答1:

You can re implement paintEvent :

void Widget::paintEvent(QPaintEvent *e)
{
    QPainter painter(this);
    painter.drawPixmap(0, 0, QPixmap(":/new/prefix1/picture001.png").scaled(size()));
    QWidget::paintEvent(e);
}


回答2:

If the QFrame is the same aspect ratio as the image you can use CSS on the QFrame like this:

QFrame
{
   border-image: url(:/images/myimage.png) 0 0 0 0 stretch stretch;
   border-width: 0px;
}