QT實現(xiàn)圖片輪播
更新時間:2020年06月08日 08:29:55 作者:男銀的驕傲
這篇文章主要介紹了QT實現(xiàn)圖片輪播,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了QT實現(xiàn)圖片輪播的具體代碼,供大家參考,具體內(nèi)容如下
UI設(shè)計

一個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);
//修改標(biāo)題
this->setWindowTitle("QLabel的顯示圖片程序:");
//給label設(shè)置新的文本
ui.picture_label->setText("未顯示圖片");
//將label框的內(nèi)容位于中間.
ui.picture_label->setAlignment(Qt::AlignCenter | Qt::AlignHCenter);
//設(shè)置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上就 實現(xiàn)了.
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
為什么要學(xué)習(xí)C語言 C語言優(yōu)勢分析
不止一個學(xué)生問到我:“老師,為什么我們的應(yīng)用程序設(shè)計要學(xué)C語言而不是別的?C語言不是已經(jīng)過時了嗎?如果現(xiàn)在要寫一個Windows程序,用VB或Dephi開發(fā)多快呀,用C行嗎?退一萬步,為什么選擇C而不是C++呢?”2013-07-07
解析C語言中結(jié)構(gòu)體struct的對齊問題
這篇文章主要介紹了C語言中結(jié)構(gòu)體struct的對齊問題,作者深入到內(nèi)存分配方面來進(jìn)行解析,需要的朋友可以參考下2016-04-04
C語言修煉之路悟徹數(shù)組真妙理?巧用下標(biāo)破萬敵下篇
在C語言和C++等語言中,數(shù)組元素全為指針變量的數(shù)組稱為指針數(shù)組,指針數(shù)組中的元素都必須具有相同的存儲類型、指向相同數(shù)據(jù)類型的指針變量。指針數(shù)組比較適合用來指向若干個字符串,使字符串處理更加方便、靈活2022-02-02

