QT + FFMPEG实现基本播放器(一):播放器基础界面实现
使用QT制作播放器的界面,界面使用QOpenGLWidget进行视频显示,将视频的每帧数据转为QImage,然后发送给Widget将QImage显示出来。
想要将QImage显示在Widget上的话,需要重写panitEvent事件:
void VideoWidget::paintEvent(QPaintEvent * /*e*/)
{
if (m_Image.isNull())
{
return;
}
QPainter painter(this);
//在widget上进行绘制
painter.drawImage(this->rect(), m_Image);
}
为了使画面显示流畅,重写定时器事件,定时刷新页面:
void VideoWidget::timerEvent(QTimerEvent * /*e*/)
{
//进行页面刷新操作
this->update();
}
设置文件打开动作:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
openFile = new QAction(tr("&Open"),this);
connect(openFile,&QAction::triggered,this,&MainWindow::OpenFileHanle);
ui->mainToolBar->addAction(openFile);
this->setWindowTitle(tr("FFPlayer"));
}
文件操作槽函数实现
void MainWindow::OpenFileHanle()
{
QFileDialog *OpenFileDialog = new QFileDialog(this,tr("Open File"),".");
OpenFileDialog->setAttribute(Qt::WA_DeleteOnClose,1);
QString strFileName = OpenFileDialog->getOpenFileName();
if(!strFileName.isEmpty())
{
qDebug()<<strFileName;
string strVideoPath = string((const char *)strFileName.toLocal8Bit()); //QString转string
string strVideoName = strVideoPath.substr(strVideoPath.find_last_of('/') + 1, strVideoPath.find_last_of('\0'));
//设置标题
this->setWindowTitle(QString::fromLocal8Bit(strVideoName.c_str()));
}
}
以下就是播放器的基础界面的图例,以后在基本功能实现之后将会进行界面美化工作: