QWidget background-image fit

妖精的绣舞 提交于 2019-12-10 19:42:32

问题


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


来源:https://stackoverflow.com/questions/14703899/qwidget-background-image-fit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!