使用Qt的开发一个的Windows下的屏保部分代码。
Login::Login(QWidget *parent) : QWidget(parent)
{
layout();
addImage();
updateImage();
QTimer *timer = new QTimer(this);
timer->start(5000);
//将定时器超时信号与槽(功能函数)联系起来
connect(timer, &QTimer::timeout, [&]() {updateImage();});
this->setAutoFillBackground(true);
}
void Login::updateImage()
{
QPalette palette;
QPixmap pixmap;
static bool be = true;
auto static list = listPixmap.begin();
if (list == listPixmap.begin())
be = true;
else if (list == --listPixmap.end())
be = false;
if (be)
pixmap = *list++;
else
pixmap = *list--;
// 设置背景图片自动缩放充满全屏
palette.setBrush(this->backgroundRole(),
QBrush(pixmap.scaled(this->size(),
Qt::IgnoreAspectRatio,
Qt::SmoothTransformation)));
this->setPalette(palette);
}
void Login::addImage()
{
//QList<QPixmap> listPixmap;
QPixmap pixmap0(":/new/icon/src/叶子.jpg"), pixmap1(":/new/icon/src/山.jpg"), pixmap2(":/new/icon/src/沙滩.jpg"),pixmap3(":/new/icon/src/login.jpg");
listPixmap << pixmap0 << pixmap1 << pixmap2 << pixmap3;
}