用QT来搭建一个简易的播放器的外壳,除了一个框框用来显示视频之外,前进按钮,快退按钮,播放/暂停按钮,停止按钮,和一个选择文件的按钮。

没有什么太重点的,主要就是熟悉一下QT的基本操作,在选择文件上比较费劲因为涉及到另外的类。然后就是熟悉基本的信号与槽绑定的问题。

直接上代码

xxx.pro工程文件

[plain] view plaincopy print?
  1. #-------------------------------------------------
  2. #
  3. # Project created by QtCreator 2011-11-10T09:15:11
  4. #
  5. #-------------------------------------------------
  6. QT       += core gui
  7. TARGET = XPlayer
  8. TEMPLATE = app
  9. SOURCES += main.cpp\
  10. mainwindow.cpp
  11. HEADERS  += mainwindow.h
  12. FORMS    += \
  13. mywindow.ui

mainwindow.h

[cpp] view plaincopy print?
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. #include <QFileDialog>
  5. #include <QMessageBox>
  6. #define FILE_NAME_LENGTH    128
  7. namespace Ui {
  8. class MyWindow;
  9. }
  10. class MyWindow : public QMainWindow
  11. {
  12. Q_OBJECT
  13. public:
  14. explicit MyWindow(QWidget *parent = 0);
  15. ~MyWindow();
  16. /* 获取当前播放还是暂停的状态 */
  17. int  getPlayState(void);
  18. /* 设置当前播放还是暂停的状态 */
  19. void setPlayState(int i);
  20. private:
  21. Ui::MyWindow *ui;
  22. /* 表示当前是播放还是暂停的状态的变量 */
  23. int  iPlayPause;
  24. /* 表示要播放的文件名称 */
  25. char caFileName[FILE_NAME_LENGTH];
  26. /* 保存打开的文件名称(包括路径) */
  27. void SaveFileName(QString file);
  28. /* 获取文件名称(包括路径) */
  29. char *GetFileName(void);
  30. public slots:
  31. /* 快退 */
  32. void SlotsBackward(void);
  33. /* 快进 */
  34. void SlotsForward(void);
  35. /* 播放暂停 */
  36. void SlotsPlayPause(void);
  37. /* 停止 */
  38. void SlotsStop(void);
  39. /* 打开文件选择对话框 */
  40. void SlotsOpenFile(void);
  41. };
  42. #endif // MAINWINDOW_H

mainwindow.cpp

[cpp] view plaincopy print?
  1. #include "mainwindow.h"
  2. #include "ui_mywindow.h"
  3. MyWindow::MyWindow(QWidget *parent) :
  4. QMainWindow(parent),
  5. ui(new Ui::MyWindow)
  6. {
  7. ui->setupUi(this);
  8. iPlayPause = 0;
  9. connect(ui->Backward,   SIGNAL(clicked()), this, SLOT(SlotsBackward()));
  10. connect(ui->Forward,    SIGNAL(clicked()), this, SLOT(SlotsForward()));
  11. connect(ui->PlayPause,  SIGNAL(clicked()), this, SLOT(SlotsPlayPause()));
  12. connect(ui->Stop,       SIGNAL(clicked()), this, SLOT(SlotsStop()));
  13. connect(ui->OpenFile,   SIGNAL(clicked()), this, SLOT(SlotsOpenFile()));
  14. }
  15. MyWindow::~MyWindow()
  16. {
  17. delete ui;
  18. }
  19. /* 快退 */
  20. void MyWindow::SlotsBackward(void)
  21. {
  22. qDebug("Backward");
  23. }
  24. /* 快进 */
  25. void MyWindow::SlotsForward(void)
  26. {
  27. qDebug("Forward");
  28. }
  29. /* 第一次点下去从播放变为暂停,以后每次布尔状态切换播放和暂停状态 */
  30. void MyWindow::SlotsPlayPause(void)
  31. {
  32. if (getPlayState()==0) {
  33. qDebug("Play: %s", GetFileName());
  34. setPlayState(1);
  35. } else {
  36. qDebug("Pause");
  37. setPlayState(0);
  38. }
  39. }
  40. /* 停止当前的播放 */
  41. void MyWindow::SlotsStop(void)
  42. {
  43. qDebug("Stop");
  44. }
  45. /* 获取当前的播放状态,播放还是暂停 */
  46. int MyWindow::getPlayState(void)
  47. {
  48. return iPlayPause;
  49. }
  50. /* 设置当前的播放状态,播放还是暂停 */
  51. void MyWindow::setPlayState(int i)
  52. {
  53. iPlayPause = i;
  54. }
  55. /* 与OPEN按钮关联的动作,显示文件选择对话框,让用户选择想要打开的文件 */
  56. void MyWindow::SlotsOpenFile(void)
  57. {
  58. qDebug("Open File");
  59. QFileDialog *fd = new QFileDialog(this);
  60. fd->setModal(QFileDialog::ExistingFile);    /* 设置模式为存在的文件 */
  61. fd->setViewMode(QFileDialog::Detail);       /* 设置显示模式为详细 */
  62. fd->setNameFilter("Video (*.mpeg *.avi)");      /* 设置过滤器,显示特定后缀名的文件 */
  63. if (fd->exec() == QDialog::Accepted) {
  64. QString file = fd->selectedFiles()[0];  /* 取得选择的文件,包括了绝对路径 */
  65. //qDebug(file.toAscii().data());
  66. SaveFileName(file);
  67. }
  68. }
  69. /* 将想要打开的媒体文件的名字保存到一个数组里面,方便使用 */
  70. void MyWindow::SaveFileName(QString file)
  71. {
  72. memset(caFileName, 0, FILE_NAME_LENGTH);
  73. strcpy(caFileName, file.toLatin1().data());
  74. }
  75. /* 获取想要打开的媒体文件的名字 */
  76. char* MyWindow::GetFileName(void)
  77. {
  78. return caFileName;
  79. }

main.cpp

[cpp] view plaincopy print?
  1. #include <QtGui/QApplication>
  2. #include "mainwindow.h"
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication a(argc, argv);
  6. MyWindow w;
  7. w.show();
  8. return a.exec();
  9. }

最后是ui文件,只能传个xml上来了~~

[html] view plaincopy print?
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>MyWindow</class>
  4. <widget class="QMainWindow" name="MyWindow">
  5. <property name="geometry">
  6. <rect>
  7. <x>0</x>
  8. <y>0</y>
  9. <width>400</width>
  10. <height>450</height>
  11. </rect>
  12. </property>
  13. <property name="windowTitle">
  14. <string>MainWindow</string>
  15. </property>
  16. <widget class="QWidget" name="MyScreen">
  17. <widget class="QPushButton" name="Backward">
  18. <property name="geometry">
  19. <rect>
  20. <x>10</x>
  21. <y>400</y>
  22. <width>50</width>
  23. <height>50</height>
  24. </rect>
  25. </property>
  26. <property name="text">
  27. <string><<</string>
  28. </property>
  29. </widget>
  30. <widget class="Line" name="SepLine">
  31. <property name="geometry">
  32. <rect>
  33. <x>0</x>
  34. <y>400</y>
  35. <width>400</width>
  36. <height>1</height>
  37. </rect>
  38. </property>
  39. <property name="orientation">
  40. <enum>Qt::Horizontal</enum>
  41. </property>
  42. </widget>
  43. <widget class="QPushButton" name="PlayPause">
  44. <property name="geometry">
  45. <rect>
  46. <x>80</x>
  47. <y>400</y>
  48. <width>50</width>
  49. <height>50</height>
  50. </rect>
  51. </property>
  52. <property name="text">
  53. <string>Play</string>
  54. </property>
  55. </widget>
  56. <widget class="QPushButton" name="Forward">
  57. <property name="geometry">
  58. <rect>
  59. <x>150</x>
  60. <y>400</y>
  61. <width>50</width>
  62. <height>50</height>
  63. </rect>
  64. </property>
  65. <property name="text">
  66. <string>>></string>
  67. </property>
  68. </widget>
  69. <widget class="QPushButton" name="Stop">
  70. <property name="geometry">
  71. <rect>
  72. <x>220</x>
  73. <y>400</y>
  74. <width>50</width>
  75. <height>50</height>
  76. </rect>
  77. </property>
  78. <property name="text">
  79. <string>STOP</string>
  80. </property>
  81. </widget>
  82. <widget class="QPushButton" name="OpenFile">
  83. <property name="geometry">
  84. <rect>
  85. <x>290</x>
  86. <y>400</y>
  87. <width>50</width>
  88. <height>50</height>
  89. </rect>
  90. </property>
  91. <property name="text">
  92. <string>OPEN</string>
  93. </property>
  94. </widget>
  95. </widget>
  96. </widget>
  97. <layoutdefault spacing="6" margin="11"/>
  98. <resources/>
  99. <connections/>
  100. </ui>

用QT搭建简单的播放器外壳相关推荐

  1. 【C++】QT制作简单音乐播放器

    QT音乐播放器Mymusic 整体布局 写ui文件: Headers Sources 整体布局 创建QT项目,使用Qt5.9版本 分为三栏,最上面放标题和图标,中间放播放的列表,最下面放控制按钮. 引 ...

  2. Android复习02(ListView具体操作[很详细]、简单音乐播放器)

    2020年 3月24日 星期二 Android录播回放 笔记[腾讯课堂] https://ke.qq.com/webcourse/index.html#cid=989760&term_id=1 ...

  3. 基于QT的网络音乐播放器(一)

    自学Qt已经有一段时间了,但是始终感觉自己还是很弱(其实并不是感觉自己很弱,是自己本来就很弱,哈哈).自己也照着书上敲了几个例子,但觉得还是要写点东西才能真正运用起来.所以,前段时间就写了个很简单的音 ...

  4. 树莓派3B qt+mplayer制作音乐播放器(10)

    内容 树莓派3B qt+mplayer制作音乐播放器:播放.暂停.上一曲.下一曲,音量调节. 平台:树莓派+qt+mplayer 1.配置 qt安装见此: https://blog.csdn.net/ ...

  5. 使用 MediaSource 搭建流式播放器

    https://zhuanlan.zhihu.com/p/26374202 使用 MediaSource 搭建流式播放器 Starkwang ​ JavaScript 话题的优秀回答者 137 人赞同 ...

  6. 搭建webassembly网页播放器(五)---网页播放器开发

    在前面的章节中,我们解决emcc环境以及使用emcc来编译ffmpeg得到网页开发中可以使用的js库,本章节,我们就来实现一个简单的播放器. 视频课程以及源码下载: https://edu.csdn. ...

  7. Qt之多媒体 Phonon播放器(一)

    熬了几个晚上终于用Phonon做了个简单的播放器了.以后慢慢优化,打算用上线程,网络编程,希望还能用上QML. //mainwindow.h #ifndef MAINWINDOW_H #define ...

  8. 搭建webassembly网页播放器(二)---emcc环境搭建

    emcc全称 emscripten,最重要的功能就是让网页js调用c/c++ 成为可能,是我们基于webassembly搭建网页播放器必须依赖的编译工具. emcc官网的搭建教程较为简单,安装过程中最 ...

  9. [SimplePlayer] 实现一个简单的播放器

    简单的播放器需要实现一个最基本的功能:播放视频文件. 实现这个功能需要包含以下几个步骤: 从视频文件中提取视频图像 在屏幕上显示视频图像 视频帧的同步,也就是保证视频图像在合适的时间在屏幕上显示 从视 ...

最新文章

  1. 深证信息等三方拟联合开展大数据研究
  2. 【DDS】基于FPGA的DDS研究与设计
  3. [Python]从零开始学python——Day03 字典,元组
  4. OpenCV距离变换和分水岭算法的图像分割
  5. 2017计算机软件,2017年计算机防病毒软件排名全面保护PC安全!
  6. leader选举的源码分析
  7. 面试必问系列之在浏览器中输入URL后到网页显示 其间发生了什么?
  8. [转]Ajax跨域访问问题-方法大全
  9. 逐步淘汰,微软宣布多款产品将停止支持 IE11
  10. 基于Tezos的算法稳定币Kolibri启动测试版
  11. maven启动web服务报错原因
  12. jenkins教程_1 简介
  13. 【POJ-2452】Sticks Problem【二分右端点+线段树】
  14. 蓝桥杯 省赛 杨辉三角形 python组(转)
  15. cnpack 菜单顺序
  16. C盘清理攻略--拯救你的C盘空间
  17. 网站跳出率(Bounce Rate)
  18. 数据库Mysql:存储过程(PROCEDURE )函数(FUNCTION)
  19. android之manifest
  20. html5火焰字体效果,CSS3文字特效属性text-shadow如何实现火焰文字的效果

热门文章

  1. 基于Verilog实现的移动机器人芯片设计
  2. 教你如何轻松搞定云上打印管理
  3. 2020应届毕业生,Android春招总结,已入职小米,进阶加薪全靠它
  4. 2021.5.24-5.31 人工智能行业每周技术动态
  5. QM/MM--GROMACS+CP2K 安装时报错
  6. m.444lu.co show.php,PHP生成的Vcard:更改字符編碼,可以顯示重音字符。
  7. 计算机原理 信息化教学设计,信息化教学设计
  8. 逆向分析系列——加壳工具
  9. 数字校园php,利用PHP技术加强高校数字化校园的信息资源建设
  10. 电子制作与DIY合集