Qt 调用mplayer视频

widget.h:

#ifndef WIDGET_H #define WIDGET_H #include <QtGui/QWidget> #include <QVBoxLayout> #include <QProcess> #include <QPushButton> #include <QDebug> #include <QCloseEvent> class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); private slots: void doMplayer(); void mplayerEnded(int, enum QProcess::ExitStatus); protected: void closeEvent(QCloseEvent *); private: QPushButton *startButton; void initValue(); bool isPlaying; QWidget *widget_mplyaer; QProcess *process; }; #endif // WIDGET_H

 

widget.cpp:

#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); widget_mplyaer = new QWidget(this); startButton= new QPushButton("play"); connect(startButton,SIGNAL(clicked()),this,SLOT(doMplayer())); process = new QProcess(this); connect(process, SIGNAL(finished(int, QProcess::ExitStatus)),this, SLOT(mplayerEnded(int, QProcess::ExitStatus))); //mplayerEnded在后面 widget_mplyaer->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred)); widget_mplyaer->setAttribute(Qt::WA_OpaquePaintEvent ); widget_mplyaer->setMinimumSize(500, 500); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(widget_mplyaer); layout->addWidget(startButton); setLayout(layout); initValue(); } void Widget::mplayerEnded(int,QProcess::ExitStatus) { process->write("quit"); if(!process->waitForFinished(100)) return; startButton->setText("play"); isPlaying = false; } void Widget::doMplayer() { if(startButton->text() == "play") { qDebug()<<"play "; QString pro = "./mplayer"; QStringList slist; slist << "-slave"; slist << "-quiet"; slist << "-wid" << QString::number(widget_mplyaer->winId()); slist << "2.rm"; process->start(pro,slist); if(!process->waitForStarted(100)) return; startButton->setText("stop"); isPlaying = true; }else { if(!isPlaying) return; qDebug()<<"stop "; process->write("q"); process->kill(); widget_mplyaer->repaint(); if(!process->waitForFinished(100)) return; startButton->setText("play"); isPlaying = false; } } void Widget::initValue() { isPlaying = false; } Widget::~Widget() { delete ui; } void Widget::closeEvent(QCloseEvent *event) { process->write("quit"); process->kill(); if(!process->waitForFinished(100)) return; event->accept(); }

 

其中mplayer是编译好的可执行文件。

你可能感兴趣的:(UI,layout,delete,qt,Signal)