QT简单播放器实现


本文适合初学者借鉴和参考,且作者能力所限制,疏漏错误之处望各位指出,谢谢。

 

功能简陋,仅仅实现文件打开视频进行播放,看效果:

 

直接上代码:

man.cpp

 1 #include "mainwindow.h"
 2 #include <QApplication>
 3 
 4 int main(int argc, char *argv[])
 5 {
 6     QApplication a(argc, argv);
 7     MainWindow w;
 8     w.show();
 9 
10     return a.exec();
11 }

manwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 class QMediaPlayer;
 6 class QVideoWidget;
 7 class MainWindow : public QMainWindow
 8 {
 9     Q_OBJECT
10 
11 public:
12     MainWindow(QWidget *parent = 0);
13     ~MainWindow();
14 private slots:
15     void openFile();
16 private:
17     QMediaPlayer *mediaPlayer;
18     QVideoWidget *videoWidget;
19     void createMenus();
20     void createAction();
21     QMenu *fileMenu;
22     QAction *openAction;
23 };
24 
25 #endif // MAINWINDOW_H

manwindow.cpp

 1 #include "mainwindow.h"
 2 #include<QMediaPlayer>
 3 #include<QVideoWidget>
 4 #include<QtWidgets>
 5 
 6 MainWindow::MainWindow(QWidget *parent)
 7     : QMainWindow(parent)
 8 {
 9     mediaPlayer=new QMediaPlayer;
10     videoWidget=new QVideoWidget;
11     //this->setFixedHeight(768);
12     //this->setFixedWidth(1366);
13     this->resize( QSize( 800, 600 ));
14     setCentralWidget(videoWidget);
15     mediaPlayer->setVideoOutput(videoWidget);
16     createAction();
17     createMenus();
18 }
19 
20 MainWindow::~MainWindow()
21 {
22 
23 }
24 
25 void MainWindow::openFile()
26 {
27     QString fileName=QFileDialog::getOpenFileName(this);
28     mediaPlayer->setMedia(QUrl::fromLocalFile(fileName));
29     mediaPlayer->setVolume(50);
30     mediaPlayer->play();
31 }
32 
33 void MainWindow::createMenus()
34 {
35     fileMenu=menuBar()->addMenu("文件");
36     fileMenu->addAction(openAction);
37 }
38 
39 void MainWindow::createAction()
40 {
41     openAction=new QAction("打开action",this);
42     connect(openAction,SIGNAL(triggered()),this,SLOT(openFile()));
43 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM