QT軟件開發(fā)-基于QTAV開發(fā)跨平臺音視頻播放器
一、環(huán)境介紹
操作系統(tǒng): win10 64位
**QT版本: ** Qt5.12.6
**編譯器: ** MinGW 32
QtAV版本: QtAV-1.12.0
FFMPEG版本: ffmpeg 3.1 使用的是QtAV提供的包,直接使用
完整源碼下載: https://download.csdn.net/download/xiaolong1126626497/19759245
二、編譯QTAV模塊
參考鏈接: https://blog.csdn.net/xiaolong1126626497/article/details/112209279
三、開發(fā)的視頻播放器效果
完整播放器下載地址: https://download.csdn.net/download/xiaolong1126626497/15560367
四、功能介紹
1. 支持命令行傳入視頻播放
2. 支持圖像旋轉(zhuǎn)播放
3. 支持查看媒體信息
4. 支持選擇GPU加速解碼
5. 支持快進、快退
6. 支持預覽畫面(鼠標放在進度條上查看畫面縮略圖)
7. 支持單幀播放,就是一幀一幀的點擊切換畫面
8. 支持畫面拍照、截圖
9. 支持復位到視頻首頁
10. 支持音量調(diào)整
11. 支持拖拽文件到窗口播放
12. 默認打開視頻不會自動播放。 自動顯示在第一幀,視頻放完停留在最后一幀。
13. 支持視頻列表
五、示例代碼
5.1 整體工程
【領QT開發(fā)教程學習資料,點擊下方鏈接莬費領取↓↓,先碼住不迷路~】
點擊這里:「鏈接」
5.2 widget.cpp代碼
#include "widget.h"#include "ui_widget.h"Widget::Widget(QString Filename,QWidget *parent) : QWidget(parent) , UI(new Ui::Widget){ ui->setupUi(this); //設置窗口的標題名稱 this->setWindowTitle("視頻播放器-DS小龍哥"); //加載樣式表 SetStyle(":/resource/VideoPlayer.qss"); //讀取配置文件 ReadConfig(); //QTAV初始化 QtAV_InitConfig(); //UI界面相關初始化 UI_InitConfig(); setAcceptDrops(true); //如果構造函數(shù)傳入的視頻文件就直接加載 if(!filename.isEmpty()) { load_video_file(0,filename); }}Widget::~Widget(){ delete ui;}/*工程: ECRS_VideoPlayer日期: 2021-02-23作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: 加載樣式表*/void Widget::SetStyle(const QString &qssFile){ QFile file(qssFile); if (file.open(QFile::ReadOnly)) { QByteArray qss=file.readAll(); qApp->setStyleSheet(qss); file.close(); }}/*工程: ECRS_VideoPlayer日期: 2021-02-23作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: UI界面初始化*/void Widget::UI_InitConfig(){ //音量滑塊范圍設置 ui->horizontalSlider_AudioValue->setMaximum(10); ui->horizontalSlider_AudioValue->setMinimum(0); //播放速度設置 ui->MediaSpeedBtn->setCheckable(true); m_TimeSpeedGrp = new QActionGroup(this); QStringList strSpeedItems; strSpeedItems << tr("0.03X") << tr("0.05X") << tr("0.1X") << tr("0.5X") << tr("1.0X") << tr("2.0X") << tr("4.0X") << tr("8.0X"); double speeds[] = { 0.03, 0.05, 0.1, 0.5, 1.0, 2.0, 4.0, 8.0 }; for (int i = 0; i < strSpeedItems.size(); i ) { QAction *pSpeedItem = m_SpeedMenu.addAction(strSpeedItems.at(i)); pSpeedItem->setData(QVariant::fromValue(speeds[i])); pSpeedItem->setCheckable(true); m_TimeSpeedGrp->addAction(pSpeedItem); if (i == 4) { pSpeedItem->setChecked(true); } } connect(m_TimeSpeedGrp, SIGNAL(triggered(QAction *)), this, SLOT(slot_onSetTimeSpeed(QAction *))); //圖像的旋轉(zhuǎn)方向 m_RotateGrp = new QActionGroup(this); QStringList strDegrees; strDegrees << tr("0~") << tr("90~") << tr("180~") << tr("270~"); int Degrees[] = {0, 90, 180, 270 }; for (int i = 0; i < strDegrees.size(); i ) { QAction *pItem = m_RotateMenu.addAction(strDegrees.at(i)); pItem->setData(QVariant::fromValue(Degrees[i])); pItem->setCheckable(true); m_RotateGrp->addAction(pItem); } connect(m_RotateGrp, SIGNAL(triggered(QAction *)), this, SLOT(slot_onMediaRotate(QAction *))); //截圖保存 ui->MediaSnapshotBtn->setCheckable(true); m_SnapshotGrp = new QActionGroup(this); QAction *pClipboard = m_SnapshotMenu.addAction(tr("保存到剪切板")); QAction *pFileDirectory = m_SnapshotMenu.addAction(tr("保存到文件")); pClipboard->setData(MENU_COPY_CLIPBOARD); //保存到剪切板 pFileDirectory->setData(MENU_SAVE_FILE_SYSTEM); //保存到文件 m_SnapshotGrp->addAction(pClipboard); //添加到分組 m_SnapshotGrp->addAction(pFileDirectory); //添加到分組 connect(m_SnapshotGrp, SIGNAL(triggered(QAction *)), this, SLOT(slot_onMediaSnapshot(QAction *))); //安裝事件監(jiān)聽器 事件篩選器是接收發(fā)送到此對象的所有事件的對象 ui->horizontalSlider_PlayPosition->installEventFilter(this); ui->widget_videoDisplay->installEventFilter(this); //狀態(tài)信息初始化 MediaInfo.state=MEDIA_NOLOAD; //工具提示信息 ui->toolButton_init_load->setToolTip(tr("加載視頻,也可以直接將視頻文件拖拽到窗口")); ui->toolButton_load->setToolTip(tr("加載視頻,也可以直接將視頻文件拖拽到窗口")); ui->toolButton_media_info->setToolTip(tr("媒體信息")); ui->MediaPrevBtn->setToolTip(tr("快退")); ui->MediaPlayBtn->setToolTip(tr("快進")); ui->MediaPauseBtn->setToolTip(tr("暫停/繼續(xù)")); ui->MediaSpeedBtn->setToolTip(tr("倍速選擇")); ui->MediaResetBtn->setToolTip(tr("復位")); ui->MediaSnapshotBtn->setToolTip(tr("截圖")); ui->MediaRotateBtn->setToolTip(tr("畫面旋轉(zhuǎn)")); ui->ReverseFrameBtn->setToolTip(tr("左一幀")); ui->ForwardFrameBtn->setToolTip(tr("右一幀")); ui->VolumeBtn->setToolTip(tr("靜音切換")); ui->checkBox_gpu_set->setToolTip(tr("GPU硬件解碼支持(需要重啟軟件才能生效)")); //播放進度條滑塊初始化 connect(ui->horizontalSlider_PlayPosition, SIGNAL(onLeave()), SLOT(onTimeSliderLeave())); connect(ui->horizontalSlider_PlayPosition, SIGNAL(onHover(int,int)), SLOT(onTimeSliderHover(int,int))); connect(ui->horizontalSlider_PlayPosition, SIGNAL(sliderMoved(int)), SLOT(seek(int))); connect(ui->horizontalSlider_PlayPosition, SIGNAL(sliderPressed()), SLOT(seek())); this->setMouseTracking(true); connect(this, SIGNAL(s_pause_state(bool,qint64)),ui->widget_videoDisplay,SLOT(slot_pause_state(bool,qint64))); connect(this, SIGNAL(s_PlayPosTime(qint64)),ui->widget_videoDisplay,SLOT(slot_PlayPosTime(qint64)));}/*工程: ECRS_VideoPlayer日期: 2021-02-23作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: QTAV初始化配置*/void Widget::QtAV_InitConfig(){ Widgets::registerRenderers(); m_player = new AVPlayer(this); m_vo=new my_qtav_videoOut; m_player->setRenderer(m_vo); //設置間隔時間(ms單位) m_player->setNotifyInterval(100); //設置視頻在最后一幀停止 m_player->setMediaEndAction(MediaEndAction_Pause); //播放的進度改變信號 connect(m_player, SIGNAL(positionChanged(qint64)),this, SLOT(updateSliderPosition(qint64))); //關聯(lián)播放器的視頻幀顯示 connect(m_vo, SIGNAL(SendOneFrame(QImage)),ui->widget_videoDisplay,SLOT(slotGetOneFrame(QImage))); //設置跳轉(zhuǎn)幀類型 m_player->setSeekType(AccurateSeek); //得到音頻輸出接口 m_audio=m_player->audio(); //音量設置 m_audio->setVolume(0.5); //0.0 (silence) to 1.0 ui->horizontalSlider_AudioValue->setValue(5); //顯示進度條上的小窗口 m_preview = new VideoPreviewWidget(); //顯示媒體信息的對話框 mpStatisticsView=nullptr; //播放器信號關聯(lián) connect(m_player, SIGNAL(started()), this, SLOT(onStartPlay())); connect(m_player, SIGNAL(stopped()), this, SLOT(onStopPlay())); connect(m_player, SIGNAL(paused(bool)), this, SLOT(onPaused(bool))); //判斷是否需要設置硬件解碼. 設置解碼器的順序 if(ui->checkBox_gpu_set->isChecked()) { m_player->setVideoDecoderPriority(QStringList() << "CUDA" << "D3D11" << "DXVA" << "FFmpeg"); } qDebug()<<m_player->videoDecoderPriority();}/*工程: ECRS_VideoPlayer日期: 2021-02-23作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: 更新播放進度*/void Widget::updateSliderPosition(qint64 value){ //QMouseEvent event(QEvent::MouseButtonPress, pos, 0, 0, 0); //QApplication::sendEvent(hWindow, &event); // int m_unit = m_player->notifyInterval(); //獲取更新間隔時間 // qDebug()<<"視頻持續(xù)時間:"<<int(m_player->duration()/m_unit); // qDebug()<<"當前進度:"<<int(value/m_unit); //設置進度條的時間 ui->horizontalSlider_PlayPosition->setValue(int(value)); //通知播放器顯示窗體 emit s_PlayPosTime(value); //設置右上角的時間 //ui->label_Total_Time->setText(QString("%1").arg(int(m_player->duration()/m_unit))); //ui->label_current_Time->setText(QString("%1").arg(int(value/m_unit))); ui->label_current_Time->setText(QTime(0, 0, 0).addMSecs(int(value)).toString(QString::fromLatin1("HH:mm:ss"))); ui->label_Total_Time->setText(QTime(0, 0, 0).addMSecs(int(m_player->mediaStopPosition())).toString(QString::fromLatin1("HH:mm:ss")));}/*工程: ECRS_VideoPlayer日期: 2021-02-23作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: 加載視頻文件 flag=0 重新加載文件 flag=1 重新開始播放QString file_path 這參數(shù)可以傳入文件名稱,因為窗口支持拖放文件進來返回值: true 成功 false 失敗*/bool Widget::load_video_file(bool flag,QString file_path){ if(flag==false) { QString filename=file_path; if(filename.isEmpty()) { filename=QFileDialog::getOpenFileName(this,"選擇播放的視頻","D:/",tr("*.mp4 *.wmv *.*")); } strncpy(video_name,filename.toUtf8().data(),sizeof(video_name)); ui->toolButton_init_load->setVisible(false); } //判斷文件是否存在 if(QFileInfo::exists(video_name)==false) { return false; } else { MediaInfo.state=MEDIA_LOAD; MediaInfo.mediaName=video_name; } //每次加載新文件設置播放進度條為0 ui->horizontalSlider_PlayPosition->setValue(0); qDebug()<<"停止原視頻"; //停止播放 m_player->stop(); //播放視頻 m_player->play(video_name); qDebug()<<"當期播放視頻:"<<video_name; //設置當前播放的視頻名稱 QFileInfo info(video_name); ui->label_FileName->setText(QString("%1").arg(info.fileName())); return true;}/*工程: ECRS_VideoPlayer日期: 2021-02-23作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: 加載視頻文件*/void Widget::on_toolButton_load_clicked(){ qDebug()<<"加載視頻文件狀態(tài):"<<load_video_file(0,"");}void Widget::on_toolButton_init_load_clicked(){ load_video_file(0,"");}/*工程: ECRS_VideoPlayer日期: 2021-02-23作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: 暫停播放*/void Widget::on_MediaPauseBtn_clicked(){ if (!m_player->isPlaying()) { //通知播放器 s_pause_state(true,ui->horizontalSlider_PlayPosition->value()); m_player->play(); return; } m_player->pause(!m_player->isPaused());}/*工程: ECRS_VideoPlayer日期: 2021-02-23作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: 重新開始播放*/void Widget::on_MediaResetBtn_clicked(){ //加重新開始播放 load_video_file(true,"");}/*工程: ECRS_VideoPlayer日期: 2021-02-23作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: 快退播放*/void Widget::on_MediaPrevBtn_clicked(){// //得到播放進度的當前位置// int value=ui->horizontalSlider_PlayPosition->value();// int m_unit = m_player->notifyInterval(); //獲取更新間隔時間// //設置每次點擊快退的距離// m_player->seek(qint64(value*m_unit)-10*m_unit); m_player->seekBackward();}/*工程: ECRS_VideoPlayer日期: 2021-02-23作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: 快進播放*/void Widget::on_MediaPlayBtn_clicked(){// //得到播放進度的當前位置// int value=ui->horizontalSlider_PlayPosition->value();// int m_unit = m_player->notifyInterval(); //獲取更新間隔時間// //設置每次點擊快進的距離// m_player->seek(qint64(value*m_unit) 10*m_unit); m_player->seekForward();}/*工程: ECRS_VideoPlayer日期: 2021-02-23作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: 向左一幀*/void Widget::on_ReverseFrameBtn_clicked(){ //得到播放進度的當前位置 int value=ui->horizontalSlider_PlayPosition->value(); int m_unit = m_player->notifyInterval(); //獲取更新間隔時間 //設置每次點擊快退的距離 m_player->seek(qint64(value-m_unit));}/*工程: ECRS_VideoPlayer日期: 2021-02-23作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: 向右一幀*/void Widget::on_ForwardFrameBtn_clicked(){ //得到播放進度的當前位置 int value=ui->horizontalSlider_PlayPosition->value(); int m_unit = m_player->notifyInterval(); //獲取更新間隔時間 m_player->seek(qint64(value m_unit));}/*工程: ECRS_VideoPlayer日期: 2021-02-23作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: 音量設置*/void Widget::on_VolumeBtn_clicked(){ //得到音頻輸出接口 AudioOutput *ao=m_player->audio(); bool checked=ui->VolumeBtn->isChecked(); if(checked) { //靜音 //音量設置 ao->setVolume(0.0); //0.0 (silence) to 1. } else { //設置正常音量 qreal volume_val=ui->horizontalSlider_AudioValue->value(); //音量設置 ao->setVolume(volume_val/10.0); //0.0 (silence) to 1. }}/*工程: ECRS_VideoPlayer日期: 2021-02-23作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: 音量設置*/void Widget::on_horizontalSlider_AudioValue_valueChanged(int value){ //得到音頻輸出接口 AudioOutput *ao=m_player->audio(); //音量設置 ao->setVolume(value/10.0); //0.0 (silence) to 1.}/*工程: ECRS_VideoPlayer日期: 2021-02-23作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: 播放速度設置菜單選擇*/void Widget::slot_onSetTimeSpeed(QAction *action){ action->setChecked(true); ui->MediaSpeedBtn->setToolTip(action->text()); ui->MediaSpeedBtn->setText(action->text()); //設置速度 正常速度是1.0 m_player->setSpeed(qreal(action->data().toFloat()));}/*工程: ECRS_VideoPlayer日期: 2021-02-23作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: 點擊了速度設置按鈕*/void Widget::on_MediaSpeedBtn_clicked(){ QPoint ptWgt = ui->MediaSpeedBtn->mapToGlobal(QPoint(0, 0)); ptWgt -= QPoint(10, 180); QAction *pSelect = m_SpeedMenu.exec(ptWgt); if (pSelect == nullptr) return;}/*工程: ECRS_VideoPlayer日期: 2021-02-23作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: 點擊了旋轉(zhuǎn)選擇菜單*/void Widget::slot_onMediaRotate(QAction *action){ action->setChecked(true); ui->MediaRotateBtn->setToolTip(action->text()); ui->widget_videoDisplay->Set_Rotate(action->data().toInt());}/*工程: ECRS_VideoPlayer日期: 2021-02-23作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: 點擊了畫面旋轉(zhuǎn)菜單*/void Widget::on_MediaRotateBtn_clicked(){ QPoint ptWgt = ui->MediaRotateBtn->mapToGlobal(QPoint(0, 0)); ptWgt -= QPoint(10, 94); QAction *pSelect = m_RotateMenu.exec(ptWgt); if (pSelect == nullptr) return;}/*工程: ECRS_VideoPlayer日期: 2021-02-24作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: 點擊了截圖菜單*/void Widget::slot_onMediaSnapshot(QAction *action){ if (action == nullptr) return; //得到按下的序號 MENU_ITEM item = MENU_ITEM(action->data().toInt()); QImage Pic=ui->widget_videoDisplay->GetImage(); if (Pic.isNull() || Pic.height() <= 0)return; //保存到剪切板 if (item == MENU_COPY_CLIPBOARD) { QApplication::clipboard()->setImage(Pic); } //保存到文件 else if (item == MENU_SAVE_FILE_SYSTEM) { QString strFile = QDateTime::currentDateTime().toString("yyyyMMddHHmmss") ".png"; QString strFileName = QFileDialog::getSaveFileName(nullptr, "保存圖片", strFile, "PNG(*.png);;BMP(*.bmp);;JPEG(*.jpg *.jpeg)"); if (!strFileName.isEmpty()) { strFileName = QDir::toNativeSeparators(strFileName); QFileInfo fInfo(strFileName); Pic.save(strFileName, fInfo.completeSuffix().toStdString().c_str(), 80); } }}/*工程: ECRS_VideoPlayer日期: 2021-02-24作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: 點擊了截圖按鈕*/void Widget::on_MediaSnapshotBtn_clicked(){ QPoint ptWgt = ui->MediaSnapshotBtn->mapToGlobal(QPoint(0, 0)); ptWgt -= QPoint(10, 48); QAction *pSelect = m_SnapshotMenu.exec(ptWgt); if (pSelect == nullptr) return;}/*工程: ECRS_VideoPlayer日期: 2021-02-24作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: 攔截事件*/bool Widget::eventFilter(QObject *obj, QEvent *event){// //判斷是否是視頻播放進度條產(chǎn)生了事件,解決QSlider點擊不能到鼠標指定位置的問題// if(obj==ui->horizontalSlider_PlayPosition)// {// //暫停狀態(tài)才可以點擊進度條進行跳轉(zhuǎn)// if(m_player->isPaused())// {// if (event->type()==QEvent::MouseButtonPress) //判斷類型// {// //視頻加載成功才能進行下面的操作// if(MediaInfo.state==MEDIA_LOAD)// {// QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);// if (mouseEvent->button() == Qt::LeftButton) //判斷左鍵// {// int value = QStyle::sliderValueFromPosition(ui->horizontalSlider_PlayPosition->minimum(), ui->horizontalSlider_PlayPosition->maximum(), mouseEvent->pos().x(), ui->horizontalSlider_PlayPosition->width());// ui->horizontalSlider_PlayPosition->setValue(value);// //跳轉(zhuǎn)幀// int m_unit = m_player->notifyInterval(); //獲取更新間隔時間// m_player->seek(qint64(value*m_unit));// }// }// }// }// } //判斷是否在視頻窗口范圍內(nèi)按下的鼠標 if(obj==ui->widget_videoDisplay) { //視頻加載成功才能進行下面的操作 if(MediaInfo.state==MEDIA_LOAD) { //判斷是否是鼠標事件 if (event->type()==QEvent::MouseButtonPress) { QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event); //如果按下的是左鍵 if(mouseEvent->button() == Qt::LeftButton) { //如果是暫停狀態(tài) if(m_player->isPaused()) { //繼續(xù)播放 m_player->pause(false); //通知播放器 s_pause_state(true,ui->horizontalSlider_PlayPosition->value()); } else { //暫停播放 m_player->pause(true); } } } } } return QObject::eventFilter(obj,event);}void Widget::on_toolButton_set_clicked(){}/*工程: ECRS_VideoPlayer日期: 2021-02-24作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: 查看媒體信息*/void Widget::on_toolButton_media_info_clicked(){ if(mpStatisticsView==nullptr) mpStatisticsView = new StatisticsView(this); if (m_player) mpStatisticsView->setStatistics(m_player->statistics()); //設置對話框的大小與父窗口一樣大 mpStatisticsView->setGeometry(this->geometry()); mpStatisticsView->show();}void Widget::onTimeSliderHover(int pos, int value){ QPoint gpos = mapToGlobal(ui->horizontalSlider_PlayPosition->pos() QPoint(pos, 0)); QToolTip::showText(gpos, QTime(0, 0, 0).addMSecs(value).toString(QString::fromLatin1("HH:mm:ss"))); if(ui->widget_videoDisplay->GetImage().height()<=0) return; m_preview->setFile(m_player->file()); m_preview->setTimestamp(value); m_preview->preview(); const int w =ui->widget_videoDisplay->width()/5; const int h = ui->widget_videoDisplay->height()/5; m_preview->setWindowFlags(m_preview->windowFlags() |Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint); m_preview->resize(w, h); m_preview->move(gpos - QPoint(w/2, h)); m_preview->show();}void Widget::onTimeSliderLeave(){ if (m_preview && m_preview->isVisible()) m_preview->hide();}void Widget::seek(int value){ if(ui->widget_videoDisplay->GetImage().height()<=0) return; m_player->setSeekType(AccurateSeek); m_player->seek((qint64)value); m_preview->setTimestamp(value); m_preview->preview(); m_preview->setWindowFlags(m_preview->windowFlags() |Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint); m_preview->resize(ui->widget_videoDisplay->width()/5,ui->widget_videoDisplay->height()/5); m_preview->show();}void Widget::seek(){ seek(ui->horizontalSlider_PlayPosition->value());}void Widget::onPaused(bool p){ if (p) { ui->MediaPauseBtn->setChecked(true); qDebug("start pausing..."); } else { ui->MediaPauseBtn->setChecked(false); qDebug("stop pausing..."); }}void Widget::onStartPlay(){ if (!m_player) return; ui->horizontalSlider_PlayPosition->setMinimum(int(m_player->mediaStartPosition())); ui->horizontalSlider_PlayPosition->setMaximum(int(m_player->mediaStopPosition())); //視頻第一次加載只顯示第一幀畫面 //暫停播放 m_player->pause(true); //偏移到第一幀 int m_unit = m_player->notifyInterval(); //獲取更新間隔時間 m_player->seek(qint64(m_unit)); qDebug()<<"開始播放";}void Widget::onStopPlay(){ if (m_preview) m_preview->setFile(QString());}void Widget::dragEnterEvent(QDragEnterEvent *e){ if (e->mimeData()->hasUrls()) { e->acceptProposedAction(); }}void Widget::dropEvent(QDropEvent *e){ foreach (const QUrl &url, e->mimeData()->urls()) { QString fileName = url.toLocalFile(); qDebug() << "拖入的文件名稱:" << fileName; //加載視頻文件 load_video_file(false,fileName); }}/*工程: ECRS_VideoPlayer日期: 2021-02-24作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: 保存配置*/void Widget::SaveConfig(){ //從UI界面獲取用戶的個性化配置參數(shù) bool val=ui->checkBox_gpu_set->isChecked(); /*保存數(shù)據(jù)到文件,方便下次加載*/ QString text; text=QCoreApplication::applicationDirPath() "/" ConfigFile; QFile filesrc(text); filesrc.open(QIODevice::WriteOnly); QDataStream out(&filesrc); out << val; //序列化 filesrc.flush(); filesrc.close();}/*工程: ECRS_VideoPlayer日期: 2021-02-24作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: 讀取配置*/void Widget::ReadConfig(){ //讀取配置文件 QString text; text=QCoreApplication::applicationDirPath() "/" ConfigFile; bool val; //判斷文件是否存在 if(QFile::exists(text)) { QFile filenew(text); filenew.open(QIODevice::ReadOnly); QDataStream in(&filenew); // 從文件讀取序列化數(shù)據(jù) in >> val; //提取寫入的數(shù)據(jù) filenew.close(); //設置界面值 ui->checkBox_gpu_set->setChecked(val); }}/*工程: ECRS_VideoPlayer日期: 2021-02-24作者: DS小龍哥環(huán)境: win10 QT5.12.6 MinGW32功能: GPU支持選擇*/void Widget::on_checkBox_gpu_set_clicked(bool checked){ SaveConfig();}
【領QT開發(fā)教程學習資料,點擊下方鏈接莬費領取↓↓,先碼住不迷路~】
點擊這里:「鏈接」
作者:DS小龍哥
鏈接:https://juejin.cn/post/6985337265384456205