Qt学习1(实现静态图片查看+本地音乐播放+本地MP4播放)

    科技2022-07-10  143

    前言

    通过QLabel显示图片,通过QMediaPlayer实现本地音视频播放。

    截图

    图片查看窗口实现

    实现功能:选择图片文件夹,读取图片;上一张/下一张切换。 imagewidget.h

    #ifndef IMAGEWIDGET_H #define IMAGEWIDGET_H #include <QWidget> #include<QPushButton> #include<QLabel> #include<QFileDialog> class ImageWidget : public QWidget { Q_OBJECT public: explicit ImageWidget(QWidget *parent = nullptr); QStringList getImageList(); //返回图片列表 void labelLoadImage(QString imageUrl); //imageLabel显示图片 void chooseImageFile(); //选择图片文件夹 void nextImage();//下一张图片 void prevImage();//上一张图片 private: //图片界面控件 QPushButton *image_Choose_Btn; //选择图片文件夹按钮 QPushButton *image_Next_Btn; //下一张图片按钮 QPushButton *image_prev_Btn; //上一张图片按钮 QLabel *image_Label; //显示图片label QStringList imageList; //图片列表 qint64 location; //当前图片在列表位置 signals: }; #endif // IMAGEWIDGET_H

    imagewidget.cpp

    #include "imagewidget.h" ImageWidget::ImageWidget(QWidget *parent) : QWidget(parent) { //图片窗口控件初始化 this->image_Label = new QLabel(this); this->image_Label->setFixedSize(400,370); this->image_Label->move(0,0); this->image_Choose_Btn = new QPushButton("choose",this); this->image_Next_Btn = new QPushButton("next",this); this->image_prev_Btn = new QPushButton("prev",this); this->image_Choose_Btn->move(500,100); this->image_prev_Btn->move(500,200); this->image_Next_Btn->move(500,300); connect(this->image_Choose_Btn,&QPushButton::clicked,this,&ImageWidget::chooseImageFile); connect(this->image_prev_Btn,&QPushButton::clicked,this,&ImageWidget::prevImage); connect(this->image_Next_Btn,&QPushButton::clicked,this,&ImageWidget::nextImage); } QStringList ImageWidget::getImageList(){ //查询文件夹下所有图片 QStringList images = {""}; //选择目录 QString dirurl = QFileDialog::getExistingDirectory(this, tr("Open Directory"), "D:/", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); if(!dirurl.isEmpty()) { //获取所有图片 QDir dir(dirurl); dirurl = dir.fromNativeSeparators(dirurl); //将'\\'转'/' dir.setFilter(QDir::Files); dir.setSorting(QDir::Name); dir.setNameFilters(QString("*.jpg;*.jpeg;*.png").split(";")); images = dir.entryList(); for(int i=0;i<images.size();i++){ images[i] = dirurl+"/"+images[i]; } } return images; } void ImageWidget::labelLoadImage(QString imageUrl){ QImage img; if(img.load(imageUrl)){ QSize size = this->image_Label->size(); img = img.scaled(size,Qt::KeepAspectRatio);//图片按比例适应label尺寸 this->image_Label->setPixmap(QPixmap::fromImage(img)); } } void ImageWidget::chooseImageFile(){ //选择图片文件夹 this->imageList.clear(); this->location = 0; this->imageList = getImageList(); if(!this->imageList.isEmpty()){ labelLoadImage(imageList.at(location)); } } void ImageWidget::nextImage(){ //下一张图片 if(this->imageList.isEmpty()) return; this->location++; if(this->location >= this->imageList.size()) { this->location = 0; } labelLoadImage(this->imageList.at(location)); } void ImageWidget::prevImage(){ //上一张图片 if(this->imageList.isEmpty()) return; this->location--; if(this->location < 0) { this->location = this->imageList.size() -1; } labelLoadImage(this->imageList.at(location)); }

    音乐播放窗口实现

    实现功能:选取音乐文件夹,读取MP3文件;QListWidget显示歌曲列表并高亮正在播放;上一首\下一首的切换;播放\暂停;音量的调节显示;播放进度调节显示;QListWidget实现点击切换歌曲。 musicwidget.h

    #ifndef MUSICWIDGET_H #define MUSICWIDGET_H #include <QWidget> #include<QPushButton> #include<QLabel> #include<QMediaPlayer> #include<QSlider> #include<QListWidget> #include<QMediaPlaylist> #include<QTime> #include<QFileDialog> class MusicWidget : public QWidget { Q_OBJECT public: explicit MusicWidget(QWidget *parent = nullptr); QStringList getMusicList(); //返回歌曲路径列表 void chooseMusicFile();//选择音乐文件夹 void nextMusic(); //下一首 void prevMusic(); //上一首 void playOrPause(); //播放\暂停 void changePosition(qint64 position); //改变进度滑条和时间 void changePlayerPostition(int position); //拖到滑条,更改播放位置 void changePlayerVolume(int volume); //拖动音量滑条,改变音量 void changeVolume(int volume);//音量变化,更新显示 void clickListWidgetItem(QListWidgetItem *item); //点击列表播放 void changeListWidgetItemColor(int position); //切歌时更新列表正在播放颜色 private: //音乐播放界面控件 QPushButton *music_Choose_Btn; //选择音乐文件夹按钮 QPushButton *music_Next_Btn; //下一首歌曲按钮 QPushButton *music_Prev_Btn; //上一首歌曲按钮 QPushButton *music_Play_Btn; //播放\暂停按钮 QSlider *music_Slider; //歌曲播放进度滑条 QSlider *music_Volume_Slider; //歌曲音量滑条 QLabel *music_Volume_Label; //歌曲音量显示 QLabel *music_Time; //歌曲时长显示 QListWidget *music_List_Widget; //音乐列表窗口 QStringList musicList; //歌曲路径列表 bool isPlay; //播放状态 QMediaPlayer *music_Player; //音乐播放 QMediaPlaylist *music_Play_List; //音乐播放列表 signals: }; #endif // MUSICWIDGET_H

    musicwidget.cpp

    #include "musicwidget.h" MusicWidget::MusicWidget(QWidget *parent) : QWidget(parent) { //音乐窗口控件初始化 this->music_List_Widget = new QListWidget(this); this->music_List_Widget->setFixedSize(400,300); this->music_List_Widget->move(0,0); this->music_Choose_Btn = new QPushButton("choose",this); this->music_Next_Btn = new QPushButton("next",this); this->music_Prev_Btn = new QPushButton("prev",this); this->music_Play_Btn = new QPushButton("play",this); this->music_Choose_Btn->move(500,0); this->music_Prev_Btn->move(500,100); this->music_Next_Btn->move(500,200); this->music_Play_Btn->move(500,300); this->music_Play_List = new QMediaPlaylist(this); this->music_Player = new QMediaPlayer(this); this->music_Slider = new QSlider(this); this->music_Slider->setOrientation(Qt::Horizontal); this->music_Slider->setFixedWidth(300); this->music_Slider->setMinimum(0); this->music_Slider->setMaximum(100); this->music_Slider->setSingleStep(1);//滑条步长为1 this->music_Slider->move(30,320); this->music_Slider->setCursor(QCursor(Qt::PointingHandCursor)); this->music_Time = new QLabel("00:00/00:00",this); this->music_Time->move(350,320); this->music_Volume_Slider = new QSlider(this); this->music_Volume_Label = new QLabel(tr("000"),this); this->music_Volume_Slider->setOrientation(Qt::Vertical); this->music_Volume_Slider->setFixedWidth(10); this->music_Volume_Slider->setMinimum(0); this->music_Volume_Slider->setMaximum(100); this->music_Volume_Slider->setSingleStep(1); this->music_Volume_Slider->move(450,200); this->music_Volume_Slider->setCursor(QCursor(Qt::PointingHandCursor)); this->music_Volume_Label->move(430,290); connect(this->music_Choose_Btn,&QPushButton::clicked,this,&MusicWidget::chooseMusicFile); connect(this->music_Next_Btn,&QPushButton::clicked,this,&MusicWidget::nextMusic); connect(this->music_Prev_Btn,&QPushButton::clicked,this,&MusicWidget::prevMusic); connect(this->music_Play_Btn,&QPushButton::clicked,this,&MusicWidget::playOrPause); connect(this->music_Player,&QMediaPlayer::positionChanged,this,&MusicWidget::changePosition); connect(this->music_Slider,&QSlider::sliderMoved,this,&MusicWidget::changePlayerPostition); connect(this->music_Volume_Slider,&QSlider::sliderMoved,this,&MusicWidget::changePlayerVolume); connect(this->music_Player,&QMediaPlayer::volumeChanged,this,&MusicWidget::changeVolume); connect(this->music_List_Widget,&QListWidget::itemClicked,this,&MusicWidget::clickListWidgetItem); connect(this->music_Play_List,&QMediaPlaylist::currentIndexChanged,this,&MusicWidget::changeListWidgetItemColor); } QStringList MusicWidget::getMusicList(){ //返回歌曲路径列表 QStringList musics = {""}; //选择文件夹 QString dirurl = QFileDialog::getExistingDirectory(this,tr("Open Directory"), "D:/", QFileDialog::ShowDirsOnly|QFileDialog::DontResolveSymlinks); if(dirurl.isEmpty()){ qDebug()<<"Not find dirurl"; }else{ QDir dir(dirurl); dirurl = dir.fromNativeSeparators(dirurl); //将‘//’转 ‘/’ dir.setFilter(QDir::Files); dir.setSorting(QDir::Name); dir.setNameFilters(QString("*.mp3").split(";")); musics = dir.entryList(); for(int i = 0;i<musics.size();i++){ musics[i] = dirurl + "/" + musics[i]; } } return musics; } void MusicWidget::chooseMusicFile(){ //选择音乐文件夹 this->musicList.clear();//清空列表 this->music_Play_List->clear(); this->music_List_Widget->clear(); this->musicList = getMusicList(); // this->music_List_Widget->addItems(this->musicList); for(int i=0;i<this->musicList.size();i++){ this->music_Play_List->addMedia(QUrl(this->musicList.at(i)));//添加音乐播放列表项 QStringList list = this->musicList.at(i).split("/");//获取歌曲名 list = list.at(list.size()-1).split("."); this->music_List_Widget->addItem(list.at(0));//将歌曲名添加到列表窗口 } this->music_Play_List->setPlaybackMode(QMediaPlaylist::Loop); //列表循环播放 this->music_Player->setPlaylist(this->music_Play_List); this->music_Player->play(); int volume = this->music_Player->volume(); //获取音量 this->music_Volume_Label->setText(QString::number(volume)); this->music_Volume_Slider->setValue(volume); this->isPlay = true; this->music_Play_Btn->setText(tr("pause")); } void MusicWidget::nextMusic(){ //下一首歌曲 int currentIndex = this->music_Play_List->currentIndex(); currentIndex++; if(currentIndex == this->musicList.size()){ currentIndex = 0; } this->music_Play_List->setCurrentIndex(currentIndex); } void MusicWidget::prevMusic(){ //上一首 int currentIndex = this->music_Play_List->currentIndex(); currentIndex--; if(currentIndex < 0) { currentIndex = this->musicList.size()-1; } this->music_Play_List->setCurrentIndex(currentIndex); } void MusicWidget::playOrPause(){ //暂停\播放 if(this->isPlay){ this->isPlay = false; this->music_Player->pause(); this->music_Play_Btn->setText(tr("play")); }else{ this->isPlay = true; this->music_Player->play(); this->music_Play_Btn->setText(tr("pause")); } } void MusicWidget::changePosition(qint64 position){ //播放位置改变,更改时间,滑条位置 this->music_Slider->setMaximum(this->music_Player->duration()/1000); this->music_Slider->setValue(position/1000); QTime moveTime(0,(position/60000)%60,(position/1000)%60); QTime time(0,(this->music_Player->duration()/60000)%60,(this->music_Player->duration()/1000)%60); this->music_Time->setText(moveTime.toString("mm:ss")+"/"+time.toString("mm:ss")); } void MusicWidget::changePlayerPostition(int position){ //拖动滑条更改播放位置 position = position*1000; this->music_Player->setPosition(position); } void MusicWidget::changePlayerVolume(int volume){ //拖动音量滑条,改变音量 this->music_Player->setVolume(volume); } void MusicWidget::changeVolume(int volume){//音量变化,更新显示 this->music_Volume_Slider->setValue(volume); this->music_Volume_Label->setText(QString::number(volume)); } void MusicWidget::clickListWidgetItem(QListWidgetItem *item){ //点击列表播放 this->music_Play_List->setCurrentIndex(this->music_List_Widget->row(item)); } void MusicWidget::changeListWidgetItemColor(int position){ //切歌时更新列表正在播放颜色 QListWidgetItem *item = this->music_List_Widget->item(position); item->setSelected(true); }

    MP4播放窗口

    实现功能:选取MP4文件;播放\暂停;音量调节;进度调节。 videowidget.h

    #ifndef VIDEOWIDGET_H #define VIDEOWIDGET_H #include <QWidget> #include<QPushButton> #include<QMediaPlayer> #include<QSlider> #include<QLabel> #include<QFileDialog> #include<QTime> #include<QVideoWidget> class VideoWidget : public QWidget { Q_OBJECT public: explicit VideoWidget(QWidget *parent = nullptr); QString getVideoUrl();//返回视频路径 void chooseVideo();//选择mp4文件 void playOrPause();//播放\暂停 void changePosition(qint64 position);//改变进度滑条位置,时间显示 void changePlayerPosition(int position);//拖动进度滑条,改变播放位置 void changeVolume(int volume); //改变音量显示 void changePlayerVolume(int volume); //拖动音量滑条,更改音量 void changeState(QMediaPlayer::State state); //检测播放状态,结束则重新播放 private: //MP4播放界面控件 QPushButton *video_Choose_Btn; //视频选择按钮 QPushButton *video_Play_Btn; //视频播放\暂停按钮 QLabel *video_Time; //视频时长显示 QVideoWidget *video_Play_Widget; //视频窗口 QSlider *video_Slider; //视频进度滑条 QMediaPlayer *video_Player; //视频播放 bool video_play; //视频播放状态 QSlider *video_Volume_Slider; //视频音量滑条 QLabel *video_Volume_Label; //视频音量显示 signals: }; #endif // VIDEOWIDGET_H

    videowidget.cpp

    #include "videowidget.h" VideoWidget::VideoWidget(QWidget *parent) : QWidget(parent) { //视频窗口控件初始化 this->video_Choose_Btn = new QPushButton(tr("choose"),this); this->video_Play_Btn = new QPushButton(tr("play"),this); this->video_Time = new QLabel(tr("00:00/00:00"),this); this->video_Slider = new QSlider(this); this->video_Play_Widget = new QVideoWidget(this); this->video_Play_Widget->setFixedSize(400,300); this->video_Slider->setFixedWidth(300); this->video_Play_Widget->move(50,0); this->video_Choose_Btn->move(100,330); this->video_Play_Btn->move(200,330); this->video_Slider->move(50,310); this->video_Time->move(370,310); this->video_Slider->setOrientation(Qt::Horizontal); this->video_Slider->setMinimum(0); this->video_Slider->setMaximum(100); this->video_Slider->setSingleStep(1); this->video_Slider->setCursor(QCursor(Qt::PointingHandCursor)); this->video_Player = new QMediaPlayer(this); this->video_Player->setVideoOutput(this->video_Play_Widget); this->video_Volume_Label = new QLabel("000",this); this->video_Volume_Label->move(480,290); this->video_Volume_Slider = new QSlider(this); this->video_Volume_Slider->setOrientation(Qt::Vertical); this->video_Volume_Slider->setFixedWidth(10); this->video_Volume_Slider->setMinimum(0); this->video_Volume_Slider->setMaximum(100); this->video_Volume_Slider->setSingleStep(1); this->video_Volume_Slider->setCursor(QCursor(Qt::PointingHandCursor)); this->video_Volume_Slider->move(500,200); connect(this->video_Choose_Btn,&QPushButton::clicked,this,&VideoWidget::chooseVideo); connect(this->video_Play_Btn,&QPushButton::clicked,this,&VideoWidget::playOrPause); connect(this->video_Player,&QMediaPlayer::positionChanged,this,&VideoWidget::changePosition); connect(this->video_Slider,&QSlider::sliderMoved,this,&VideoWidget::changePlayerPosition); connect(this->video_Player,&QMediaPlayer::volumeChanged,this,&VideoWidget::changeVolume); connect(this->video_Volume_Slider,&QSlider::sliderMoved,this,&VideoWidget::changePlayerVolume); connect(this->video_Player,&QMediaPlayer::stateChanged,this,&VideoWidget::changeState); } QString VideoWidget::getVideoUrl(){ //返回视频路径 QString videoUrl = ""; //选择文件 videoUrl = QFileDialog::getOpenFileName(this,tr("open video file"), "D:/", tr("videos (*.mp4)")); return videoUrl; } void VideoWidget::chooseVideo(){ //选择mp4文件 QString videoUrl = getVideoUrl(); this->video_Player->setMedia(QUrl::fromLocalFile(videoUrl)); this->video_Player->play(); this->video_play = true; this->video_Play_Btn->setText(tr("pause")); this->video_Volume_Slider->setValue(this->video_Player->volume()); this->video_Volume_Label->setText(QString::number(this->video_Player->volume())); } void VideoWidget::playOrPause(){ //播放\暂停 if(this->video_play) { this->video_play = false; this->video_Player->pause(); this->video_Play_Btn->setText(tr("play")); }else{ this->video_play = true; this->video_Player->play(); this->video_Play_Btn->setText(tr("pause")); } } void VideoWidget::changePosition(qint64 position){ //改变进度滑条位置,时间显示 this->video_Slider->setMaximum(this->video_Player->duration()/1000); this->video_Slider->setValue(position/1000); QTime moveTime(0,(position/60000)%60,(position/1000)%60); QTime time(0,(this->video_Player->duration()/60000)%60,(this->video_Player->duration()/1000)%60); this->video_Time->setText(moveTime.toString("mm:ss")+"/"+time.toString("mm:ss")); } void VideoWidget::changePlayerPosition(int position){ //拖动进度滑条,改变播放位置 position = position*1000; this->video_Player->setPosition(position); } void VideoWidget::changeVolume(int volume){ //改变音量显示 this->video_Volume_Label->setText(QString::number(volume)); this->video_Volume_Slider->setValue(volume); } void VideoWidget::changePlayerVolume(int volume){ //拖动音量滑条,更改音量 this->video_Player->setVolume(volume); } void VideoWidget::changeState(QMediaPlayer::State state){ //检测播放状态,结束则重新播放 if(state == QMediaPlayer::StoppedState) { this->video_Player->play(); } }

    主窗口

    mywidget.h

    #ifndef MYWIDGET_H #define MYWIDGET_H #include<QTabWidget> #include"imagewidget.h" #include"musicwidget.h" #include"videowidget.h" class MyWidget : public QTabWidget { Q_OBJECT private: ImageWidget *imageWidget; //图片界面 MusicWidget *musicWidget; //音乐界面 VideoWidget *videoWidget; //视频界面 public: MyWidget(QTabWidget *parent = nullptr); ~MyWidget(); }; #endif // MYWIDGET_H

    mywidget.cpp

    #include "mywidget.h" MyWidget::MyWidget(QTabWidget *parent) : QTabWidget(parent) { this->imageWidget = new ImageWidget(this); this->musicWidget = new MusicWidget(this); this->videoWidget = new VideoWidget(this); this->addTab(this->imageWidget,"image"); this->addTab(this->musicWidget,"music"); this->addTab(this->videoWidget,"video"); this->setWindowTitle("MMS"); this->setFixedSize(600,400); //固定窗口大小 } MyWidget::~MyWidget() { }

    总结

    多看帮助文档,许多信号,槽函数已有定义; 使用QMediaPlayer要在pro文件添加QT += multimedia 室友QQVideoWidget要在pro文件添加 QT+= multimediawidgets 使用QMediaPlayer报错:DirectShowPlayerService::doRender: Unresolved error code 0x80040266,QMediaPlayer依赖本地解码器,下载安装LAVFilters即可。 github:https://github.com/jokebiubiu/study-Qt

    Processed: 0.010, SQL: 8