最近在学Qt,但是发现在Qt Creator 下能正常运行的程序想直接移植到VS下还是要花点功夫的。由于在Creator当中并没有UI_xx.h文件,项目当中的声明和creator不一样,所以在摸索过程中遇到了一些障碍,本文在VS中已经配置好Qt的前提下,在VS平台下制作了一款播放器,支持mp3和mp4格式。下面贴上代码:
1.在ui_xx.h下先画好界面,如下图
接下来是.h文件,代码如下:
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QtWidgets/QWidget>
#include <qmediaplayer.h>
#include <QtGui/QMovie>
#include "ui_myclass.h"
//#include <iostream>
#include <stdlib.h>
using namespace std;
QT_BEGIN_NAMESPACE
class QAbstractButton;
class QSlider;
class QLabel;
QT_END_NAMESPACE
class MyClass : public QWidget,public Ui_MyClassClass
{
Q_OBJECT
public:
MyClass(QWidget *parent = 0);
~MyClass();
public slots:
void openFile();
void play1();
private slots:
void mediaStateChanged(QMediaPlayer::State state);
void positionChanged(qint64 position);
void durationChanged(qint64 duration);
void setPosition(int position);
void handleError();
public:
QMediaPlayer mediaPlayer;
/*QAbstractButton *playButton;
QAbstractButton *openButton;
QSlider *positionSlider;*/
QLabel *errorLabel;
};
#endif // MYCLASS_H
接下来是.cpp文件:
#include "myclass.h"
#include <qvideowidget.h>
#include <qvideosurfaceformat.h>
#include <QtWidgets>
MyClass::MyClass(QWidget *parent)
: QWidget(parent)
, mediaPlayer(0, QMediaPlayer::VideoSurface)
// , playButton(0)
// , positionSlider(0)
//, errorLabel(0)
{
setupUi(this);
QVideoWidget *videoWidget = new QVideoWidget;
// QAbstractButton *openButton = new QPushButton(tr("Open"));
connect(this->openButton, SIGNAL(clicked()), this, SLOT(openFile()));
//playButton = new QPushButton;
this->playButton->setEnabled(false);
this->playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
connect(this->playButton, SIGNAL(clicked()),this, SLOT(play1()));
// positionSlider = new QSlider(Qt::Horizontal);
// positionSlider->setRange(0, 0);
connect(this->positionSlider, SIGNAL(sliderMoved(int)),this, SLOT(setPosition(int)));
errorLabel = new QLabel;
errorLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
QBoxLayout *controlLayout = new QHBoxLayout;
controlLayout->setMargin(0);
controlLayout->addWidget(openButton);
controlLayout->addWidget(playButton);
controlLayout->addWidget(positionSlider);
QBoxLayout *layout = new QVBoxLayout;
layout->addWidget(videoWidget);
layout->addLayout(controlLayout);
layout->addWidget(errorLabel);
setLayout(layout);
mediaPlayer.setVideoOutput(videoWidget);
connect(&mediaPlayer, SIGNAL(stateChanged(QMediaPlayer::State)),
this, SLOT(mediaStateChanged(QMediaPlayer::State)));
connect(&mediaPlayer, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
connect(&mediaPlayer, SIGNAL(durationChanged(qint64)), this, SLOT(durationChanged(qint64)));
connect(&mediaPlayer, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(handleError()));
}
MyClass::~MyClass()
{
}
void MyClass::openFile()
{
errorLabel->setText("");
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"),QDir::homePath());
if (!fileName.isEmpty()) {
mediaPlayer.setMedia(QUrl::fromLocalFile(fileName));
playButton->setEnabled(true);
}
}
void MyClass::play1()
{
switch(mediaPlayer.state()) {
case QMediaPlayer::PlayingState:
mediaPlayer.pause();
break;
default:
mediaPlayer.play();
break;
}
}
void MyClass::mediaStateChanged(QMediaPlayer::State state)
{
switch(state) {
case QMediaPlayer::PlayingState:
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
break;
default:
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
break;
}
}
void MyClass::positionChanged(qint64 position)
{
positionSlider->setValue(position);
}
void MyClass::durationChanged(qint64 duration)
{
positionSlider->setRange(0, duration);
}
void MyClass::setPosition(int position)
{
mediaPlayer.setPosition(position);
}
void MyClass::handleError()
{
playButton->setEnabled(false);
errorLabel->setText("Error: " + mediaPlayer.errorString());
}
最后是main函数:
#include "myclass.h"
#include <QtWidgets/QApplication>
#include "stdafx.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyClass w;
w.show();
return a.exec();
}
以上就是我根据Qt当中的示例自己编辑的一个小播放器,虽然还有很多功能有待实现,但是已经有了播放器的基本功能。本人小白一个,希望能和广大站友一起探讨提高。