当前位置: 首页 > 新闻动态 > 软件编程

QT实现图片轮播

作者:用户投稿 浏览: 发布日期:2026-01-12
[导读]:这篇文章主要介绍了QT实现图片轮播,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了QT实现图片轮播的具体代码,供大家参考,具体内容如下

UI设计

一个Qlabel控件,一个pushButton 键

废话不多说直接怼代码

.h文件

#ifndef IMAGES_H
#define IMAGES_H
 
#include <QtWidgets/QMainWindow>
#include "ui_images.h"
#include <Qlabel>
#include <qpushbutton.h>
#include <qpixmap.h>
#include <qstring.h>
#include <qtimer.h>
 
class images : public QMainWindow
{
 Q_OBJECT
 
public:
 images(QWidget *parent=0);
 ~images();
 
private:
 Ui::imagesClass ui;
 QTimer *qTimer;
 int imgNumber;
private slots:
 //显示图片
 void showPictureSlot();
 
};
 
#endif // IMAGES_H

.cpp文件

#include "images.h"
#pragma execution_character_set("utf-8")
 
images::images(QWidget *parent)
 : QMainWindow(parent), imgNumber(0)
{
 ui.setupUi(this);
 //修改标题
 this->setWindowTitle("QLabel的显示图片程序:");
 
 //给label设置新的文本
 ui.picture_label->setText("未显示图片");
 //将label框的内容位于中间.
 ui.picture_label->setAlignment(Qt::AlignCenter | Qt::AlignHCenter);
 
 
 //设置label框自动填充
 //ui.picture_label->setScaledContents(true);
 
 //连接信号 与 槽
 connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(showPictureSlot()));
 
 
 ui.picture_label->setScaledContents(true);
 
 qTimer = new QTimer();
 
 connect(this->qTimer, SIGNAL(timeout()), this, SLOT(showPictureSlot()));
 
 qTimer->start(3000);
}
 
images::~images()
{
 delete qTimer;
}
//显示图片
void images::showPictureSlot(){
 ++imgNumber;
    //图片路径(绝对路径拼接)
 QString path = ":/File/Resources/" + QString::number(imgNumber) + ".png";
 QPixmap pixmap(path);
 
 pixmap.scaled(ui.picture_label->size(), Qt::KeepAspectRatio);
 
 ui.picture_label->setPixmap(pixmap);
 
 if (3 == imgNumber)
 {
 imgNumber = 0;
 }
}

这样简单的图片轮播在Qlabel上就 实现了.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

免责声明:转载请注明出处:http://shjed.com/news/773495.html

扫一扫高效沟通

多一份参考总有益处

免费领取网站策划SEO优化策划方案

请填写下方表单,我们会尽快与您联系
感谢您的咨询,我们会尽快给您回复!