-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvertisement.cpp
More file actions
73 lines (63 loc) · 2.18 KB
/
Copy pathadvertisement.cpp
File metadata and controls
73 lines (63 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "advertisement.h"
Advertisement::Advertisement(QLabel *label) : mylabel(label)
{
// 存放图片的母文件路径,替换为实际路径
QString photoDirectory = "/home/pthoto";
QDir directory(photoDirectory);
// 检查目录是否存在
if (directory.exists()) {
QStringList fileList = directory.entryList(QDir::Files);
foreach (QString fileName, fileList) {
QString filePath = directory.absoluteFilePath(fileName);
// 检查文件是否存在和是否可读取
QFileInfo fileInfo(filePath);
if (fileInfo.exists() && fileInfo.isFile() && fileInfo.isReadable()) {
// 获取文件扩展名并转换为小写,以便不区分大小写比较
QString extension = fileInfo.suffix().toLower();
// 检查文件扩展名,只添加符合条件的文件路径
if (extension == "jpg" || extension == "bmp" || extension == "png") {
pthotoList.append(filePath);
qDebug() << "已添加文件:" << filePath; // 打印完整的文件路径
}
}
}
} else {
qDebug() << "目录不存在:" << photoDirectory;
}
}
void Advertisement::startAdvertising(int interval)
{
if (pthotoList.isEmpty()) {
qDebug() << "广告目录为空";
return;
}
myTimer = new QTimer(this);
connect(myTimer, &QTimer::timeout, this, &Advertisement::showNextImage);
myTimer->start(interval);
}
void Advertisement::stopAdvertising()
{
if (myTimer) {
myTimer->stop();
myTimer->deleteLater();
}
}
void Advertisement::showNextImage()
{
if (cnt == pthotoList.size()||cnt<0) {
cnt = 0; // 重置计数器
}
QPixmap mymap;
if (mymap.load(pthotoList.at(cnt)))
{
mymap = mymap.scaled(mylabel->size(), Qt::KeepAspectRatio);
mylabel->setScaledContents(true);
mylabel->setPixmap(mymap);
}
else
{
// 处理加载图片失败的情况
qDebug() << "Failed to load image: " << pthotoList.at(cnt);
}
cnt++;
}