用qt写的一个将必应主页图片设置为桌面背景的小程序,运行后会自动拉取图片并在桌面右上角显示图片信息,并会自动将图片保存到程序目录下的BingDesk_QT_img文件夹。运行文件夹里的appBingDesk_QT.exe即可,win10/11测试ok,简单好用!
WallpaperManager::WallpaperManager(QObject *parent)
: QObject(parent), networkManager(new QNetworkAccessManager(this))
{
// connect(networkManager, &QNetworkAccessManager::finished, this, &WallpaperManager::onNetworkReply);
setNotificationStr("BingDesk_QT initializing...");
m_getImgSuccess = 0;
m_timer_2s = new QTimer(this);
connect(m_timer_2s, &QTimer::timeout, this, &WallpaperManager::onTimeout_2s);
m_timer_2s->start(2000); // 定时器每2000毫秒触发一次
}
void WallpaperManager::fetchWallpaper()
{
// QNetworkRequest request(QUrl("https://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1"));
QNetworkRequest request(QUrl("http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=zh-CN"));
QSharedPointer<QNetworkReply> reply(networkManager->get(request));
qDebug() << QSslSocket::sslLibraryBuildVersionString();
QtFuture::connect(reply.get(), &QNetworkReply::finished).then([=]
{
if (reply->error() == QNetworkReply::NoError)
{
QByteArray response = reply->readAll();
QString xml(response);
QString imageUrl = "https://www.bing.com" xml.section("<url>", 1, 1).section("</url>", 0, 0);
QString imageCopyright=xml.section("<copyright>", 1, 1).section("</copyright>", 0, 0);
imageCopyright.replace(QString("/"), QString("-"));
imageCopyright.replace(QString("("), QString("-"));
imageCopyright.replace(QString(")"), QString("-"));
setNotificationStr(imageCopyright);
m_getImgSuccess=1;
QUrl url(imageUrl);
QNetworkRequest request(url);
QNetworkReply *imageReply = networkManager->get(request);
// qDebug()<<QSslSocket::supportsSsl();
connect(imageReply, &QNetworkReply::finished, [=]()
{
if (imageReply->error() == QNetworkReply::NoError) {
QByteArray imageData = imageReply->readAll();
QDate currentDate = QDate::currentDate();
QString fileName=QDir::currentPath() "/BingDesk_QT_img/" QString::number(currentDate.year()) "/";
QDir dir(fileName);
if(!dir.exists())
{
dir.mkpath(fileName);
}
fileName=fileName QString::number(currentDate.month()) "-" QString::number(currentDate.day()) "-" imageCopyright ".jpg";
// QFile file("bing_wallpaper.jpg");
QFile file(fileName);
if (file.open(QIODevice::WriteOnly)) {
file.write(imageData);
file.close();
setWallpaper(fileName);
}
}
imageReply->deleteLater(); });
}
reply->deleteLater(); });
}
void WallpaperManager::setWallpaper(QString imagePath)
{
// Convert the QString to a wide string
std::wstring wsImagePath = imagePath.toStdWString();
// Call the Windows API to set the wallpaper
SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, (void *)wsImagePath.c_str(), SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
}
//! [8]
QString WallpaperManager::getNotificationStr()
{
return m_notificationStr;
}
void WallpaperManager::setNotificationStr(QString str)
{
m_notificationStr = str;
emit statusChanged();
}
void WallpaperManager::onTimeout_2s()
{
if (!m_getImgSuccess)
{
// Clean previous state
replies.clear();
fetchWallpaper();
}
else
{
m_timer_2s->stop();
}
}
网友评论
我要评论