QT实现banner轮播图,要求按Z序大小堆叠,中间为选中项并最大有阴影, 展示5张图片, 3秒滚动一次, 不使用QML
时间: 2023-09-13 11:06:24 浏览: 189
以下是一个不使用QML的简单实现方案:
```c++
#include <QApplication>
#include <QLabel>
#include <QHBoxLayout>
#include <QTimer>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
//创建容器widget和布局
QWidget *container = new QWidget;
QHBoxLayout *layout = new QHBoxLayout;
container->setLayout(layout);
container->setStyleSheet("background-color: black");
//创建5个label用于展示图片
QLabel *label1 = new QLabel(container);
QLabel *label2 = new QLabel(container);
QLabel *label3 = new QLabel(container);
QLabel *label4 = new QLabel(container);
QLabel *label5 = new QLabel(container);
label1->setPixmap(QPixmap(":/banner1.jpg"));
label2->setPixmap(QPixmap(":/banner2.jpg"));
label3->setPixmap(QPixmap(":/banner3.jpg"));
label4->setPixmap(QPixmap(":/banner4.jpg"));
label5->setPixmap(QPixmap(":/banner5.jpg"));
//将label添加到布局中
layout->addWidget(label1);
layout->addWidget(label2);
layout->addWidget(label3);
layout->addWidget(label4);
layout->addWidget(label5);
//设置label的对齐方式和阴影效果
for (auto label : container->findChildren<QLabel*>()) {
label->setAlignment(Qt::AlignCenter);
label->setScaledContents(true);
label->setGraphicsEffect(new QGraphicsDropShadowEffect);
}
//设置初始选中项和Z值
int currentIndex = 0;
label1->raise();
label1->setGraphicsEffect(nullptr);
label2->setGraphicsEffect(nullptr);
label3->setGraphicsEffect(nullptr);
label4->setGraphicsEffect(nullptr);
label5->setGraphicsEffect(nullptr);
//定时器控制自动滚动
QTimer timer;
QObject::connect(&timer, &QTimer::timeout, [&]() {
currentIndex = (currentIndex + 1) % 5;
switch (currentIndex) {
case 0:
label1->raise();
label1->setGraphicsEffect(nullptr);
label2->setGraphicsEffect(new QGraphicsDropShadowEffect);
label3->setGraphicsEffect(new QGraphicsDropShadowEffect);
label4->setGraphicsEffect(new QGraphicsDropShadowEffect);
label5->setGraphicsEffect(new QGraphicsDropShadowEffect);
break;
case 1:
label2->raise();
label1->setGraphicsEffect(new QGraphicsDropShadowEffect);
label2->setGraphicsEffect(nullptr);
label3->setGraphicsEffect(new QGraphicsDropShadowEffect);
label4->setGraphicsEffect(new QGraphicsDropShadowEffect);
label5->setGraphicsEffect(new QGraphicsDropShadowEffect);
break;
case 2:
label3->raise();
label1->setGraphicsEffect(new QGraphicsDropShadowEffect);
label2->setGraphicsEffect(new QGraphicsDropShadowEffect);
label3->setGraphicsEffect(nullptr);
label4->setGraphicsEffect(new QGraphicsDropShadowEffect);
label5->setGraphicsEffect(new QGraphicsDropShadowEffect);
break;
case 3:
label4->raise();
label1->setGraphicsEffect(new QGraphicsDropShadowEffect);
label2->setGraphicsEffect(new QGraphicsDropShadowEffect);
label3->setGraphicsEffect(new QGraphicsDropShadowEffect);
label4->setGraphicsEffect(nullptr);
label5->setGraphicsEffect(new QGraphicsDropShadowEffect);
break;
case 4:
label5->raise();
label1->setGraphicsEffect(new QGraphicsDropShadowEffect);
label2->setGraphicsEffect(new QGraphicsDropShadowEffect);
label3->setGraphicsEffect(new QGraphicsDropShadowEffect);
label4->setGraphicsEffect(new QGraphicsDropShadowEffect);
label5->setGraphicsEffect(nullptr);
break;
}
});
timer.start(3000);
//显示窗口
container->show();
return app.exec();
}
```
这个实现方案使用了QWidget作为容器,QHBoxLayout作为布局。每个图片使用一个QLabel来展示,通过设置QGraphicsEffect来实现阴影效果。定时器控制了自动滚动,通过修改选中项和Z值来切换图片和阴影效果。
阅读全文