77 lines
2.2 KiB
C++
77 lines
2.2 KiB
C++
|
#include "BackgroundWidget.h"
|
||
|
|
||
|
#include "MainWindow.h"
|
||
|
|
||
|
#include <QApplication>
|
||
|
#include <QDir>
|
||
|
#include <QHBoxLayout>
|
||
|
#include <QResizeEvent>
|
||
|
#include <QPaintEvent>
|
||
|
#include <QPainter>
|
||
|
|
||
|
BackgroundWidget::BackgroundWidget(QWidget *parent) : MovieWidget(parent) {
|
||
|
InitImages();
|
||
|
|
||
|
//m_display = new QLabel(this);
|
||
|
//QHBoxLayout* layout = new QHBoxLayout(this); // 创建水平布局管理器
|
||
|
//layout->addWidget(m_display); // 将标签添加到布局中
|
||
|
//layout->setContentsMargins(0, 0, 0, 0);
|
||
|
//m_display->setAlignment(Qt::AlignCenter);
|
||
|
}
|
||
|
|
||
|
void BackgroundWidget::OnTimeout() {
|
||
|
if (m_pixmapPaths.isEmpty()) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (m_pixmaps.count() < m_pixmapPaths.count()) {
|
||
|
QPixmap pixmap(m_pixmapPaths[m_current]); // 获取文件的绝对路径
|
||
|
const QPixmap& px = m_pixmaps.emplace_back(std::move(pixmap));
|
||
|
|
||
|
if (width() != px.width()) {
|
||
|
MainWindow::Get().setGeometry(0, 30, px.width(), height());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
m_current = (m_current + 1) % m_pixmaps.size();
|
||
|
update();
|
||
|
//m_display->setPixmap(m_pixmaps[m_current]);
|
||
|
//label.setPixmap(pixmap.scaled(label.size(), Qt::KeepAspectRatio));
|
||
|
}
|
||
|
|
||
|
void BackgroundWidget::InitImages() {
|
||
|
const QString imageDir = QApplication::applicationDirPath() + "/ProjectDisplay/background";
|
||
|
|
||
|
QStringList jpgFiles;
|
||
|
|
||
|
QDir dir(imageDir);
|
||
|
QStringList filters;
|
||
|
filters << "*.jpg"; // 添加 JPG 的文件扩展名
|
||
|
QFileInfoList fileInfoList = dir.entryInfoList(filters, QDir::Files); // 使用过滤器获取文件列表
|
||
|
|
||
|
foreach(const QFileInfo & fileInfo, fileInfoList) {
|
||
|
m_pixmapPaths.append(fileInfo.absoluteFilePath());
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
void BackgroundWidget::resizeEvent(QResizeEvent* event) {
|
||
|
MovieWidget::resizeEvent(event);
|
||
|
|
||
|
//m_display->setPixmap(label.pixmap()->scaled(label.size(), Qt::KeepAspectRatio)); // 图片大小适应标签大
|
||
|
}
|
||
|
|
||
|
void BackgroundWidget::paintEvent(QPaintEvent* evnet) {
|
||
|
if (m_pixmaps.isEmpty()) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
int imageHeight = m_pixmaps[m_current].height();
|
||
|
int y = fabs(height() - imageHeight) * 0.5;
|
||
|
|
||
|
QPainter painter(this);
|
||
|
painter.drawPixmap(0, y, m_pixmaps[m_current]);
|
||
|
}
|