用QT搭建简单的播放器外壳

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

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

直接上代码

xxx.pro工程文件

#-------------------------------------------------
#
# Project created by QtCreator 2011-11-10T09:15:11
#
#-------------------------------------------------

QT       += core gui

TARGET = XPlayer
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += \
    mywindow.ui

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QFileDialog>
#include <QMessageBox>

#define FILE_NAME_LENGTH    128

namespace Ui {
    class MyWindow;
}

class MyWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MyWindow(QWidget *parent = 0);
    ~MyWindow();
    /* 获取当前播放还是暂停的状态 */
    int  getPlayState(void);
    /* 设置当前播放还是暂停的状态 */
    void setPlayState(int i);

private:
    Ui::MyWindow *ui;
    /* 表示当前是播放还是暂停的状态的变量 */
    int  iPlayPause;
    /* 表示要播放的文件名称 */
    char caFileName[FILE_NAME_LENGTH];

    /* 保存打开的文件名称(包括路径) */
    void SaveFileName(QString file);
    /* 获取文件名称(包括路径) */
    char *GetFileName(void);

public slots:
    /* 快退 */
    void SlotsBackward(void);
    /* 快进 */
    void SlotsForward(void);
    /* 播放暂停 */
    void SlotsPlayPause(void);
    /* 停止 */
    void SlotsStop(void);
    /* 打开文件选择对话框 */
    void SlotsOpenFile(void);
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mywindow.h"

MyWindow::MyWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MyWindow)
{
    ui->setupUi(this);
    iPlayPause = 0;
    connect(ui->Backward,   SIGNAL(clicked()), this, SLOT(SlotsBackward()));
    connect(ui->Forward,    SIGNAL(clicked()), this, SLOT(SlotsForward()));
    connect(ui->PlayPause,  SIGNAL(clicked()), this, SLOT(SlotsPlayPause()));
    connect(ui->Stop,       SIGNAL(clicked()), this, SLOT(SlotsStop()));
    connect(ui->OpenFile,   SIGNAL(clicked()), this, SLOT(SlotsOpenFile()));
}

MyWindow::~MyWindow()
{
    delete ui;
}

/* 快退 */
void MyWindow::SlotsBackward(void)
{
    qDebug("Backward");
}

/* 快进 */
void MyWindow::SlotsForward(void)
{
    qDebug("Forward");
}


/* 第一次点下去从播放变为暂停,以后每次布尔状态切换播放和暂停状态 */
void MyWindow::SlotsPlayPause(void)
{
    if (getPlayState()==0) {
        qDebug("Play: %s", GetFileName());
        setPlayState(1);
    } else {
        qDebug("Pause");
        setPlayState(0);
    }
}

/* 停止当前的播放 */
void MyWindow::SlotsStop(void)
{
    qDebug("Stop");
}

/* 获取当前的播放状态,播放还是暂停 */
int MyWindow::getPlayState(void)
{
    return iPlayPause;
}

/* 设置当前的播放状态,播放还是暂停 */
void MyWindow::setPlayState(int i)
{
    iPlayPause = i;
}

/* 与OPEN按钮关联的动作,显示文件选择对话框,让用户选择想要打开的文件 */
void MyWindow::SlotsOpenFile(void)
{
    qDebug("Open File");
    QFileDialog *fd = new QFileDialog(this);
    fd->setModal(QFileDialog::ExistingFile);    /* 设置模式为存在的文件 */
    fd->setViewMode(QFileDialog::Detail);       /* 设置显示模式为详细 */
    fd->setFilter("Video (*.mpeg *.avi)");      /* 设置过滤器,显示特定后缀名的文件 */
    if (fd->exec() == QDialog::Accepted) {
        QString file = fd->selectedFiles()[0];  /* 取得选择的文件,包括了绝对路径 */
        //qDebug(file.toAscii().data());
        SaveFileName(file);
    }
}

/* 将想要打开的媒体文件的名字保存到一个数组里面,方便使用 */
void MyWindow::SaveFileName(QString file)
{
    memset(caFileName, 0, FILE_NAME_LENGTH);
    strcpy(caFileName, file.toAscii().data());
}

/* 获取想要打开的媒体文件的名字 */
char* MyWindow::GetFileName(void)
{
    return caFileName;
}

main.cpp

#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyWindow w;
    w.show();

    return a.exec();
}

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

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MyWindow</class>
 <widget class="QMainWindow" name="MyWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>450</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="MyScreen">
   <widget class="QPushButton" name="Backward">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>400</y>
      <width>50</width>
      <height>50</height>
     </rect>
    </property>
    <property name="text">
     <string><<</string>
    </property>
   </widget>
   <widget class="Line" name="SepLine">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>400</y>
      <width>400</width>
      <height>1</height>
     </rect>
    </property>
    <property name="orientation">
     <enum>Qt::Horizontal</enum>
    </property>
   </widget>
   <widget class="QPushButton" name="PlayPause">
    <property name="geometry">
     <rect>
      <x>80</x>
      <y>400</y>
      <width>50</width>
      <height>50</height>
     </rect>
    </property>
    <property name="text">
     <string>Play</string>
    </property>
   </widget>
   <widget class="QPushButton" name="Forward">
    <property name="geometry">
     <rect>
      <x>150</x>
      <y>400</y>
      <width>50</width>
      <height>50</height>
     </rect>
    </property>
    <property name="text">
     <string>>></string>
    </property>
   </widget>
   <widget class="QPushButton" name="Stop">
    <property name="geometry">
     <rect>
      <x>220</x>
      <y>400</y>
      <width>50</width>
      <height>50</height>
     </rect>
    </property>
    <property name="text">
     <string>STOP</string>
    </property>
   </widget>
   <widget class="QPushButton" name="OpenFile">
    <property name="geometry">
     <rect>
      <x>290</x>
      <y>400</y>
      <width>50</width>
      <height>50</height>
     </rect>
    </property>
    <property name="text">
     <string>OPEN</string>
    </property>
   </widget>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值