QT ui以绘制好界面实现视频播放,包含开始、暂停、停止按钮和进度条、总时长、播放进度.cpp
时间: 2023-10-20 13:12:41 浏览: 104
以下是一个简单的示例代码,演示如何使用QT UI绘制视频播放器界面,并实现视频播放、暂停、停止等功能:
首先,需要在QT Designer中设计一个界面,包含一个QMediaPlayer控件、开始、暂停、停止按钮、进度条、总时长和当前播放进度等控件。将该界面保存为一个.ui文件,然后使用uic工具将其转换为C++代码。
ui文件代码如下:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>VideoPlayer</class>
<widget class="QWidget" name="VideoPlayer">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Video Player</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QMediaPlayer" name="mediaPlayer"/>
</item>
<item>
<widget class="QSlider" name="progressSlider">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="playButton">
<property name="text">
<string>Play</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pauseButton">
<property name="text">
<string>Pause</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="stopButton">
<property name="text">
<string>Stop</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="durationLabel">
<property name="text">
<string>00:00:00 / 00:00:00</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
```
转换后的C++代码如下:
```cpp
#include "ui_VideoPlayer.h"
#include <QMediaPlayer>
#include <QFileDialog>
#include <QBoxLayout>
#include <QLabel>
#include <QSlider>
#include <QPushButton>
#include <QTime>
class VideoPlayer : public QWidget
{
Q_OBJECT
public:
explicit VideoPlayer(QWidget *parent = nullptr);
~VideoPlayer();
private slots:
void openFile();
void play();
void pause();
void stop();
void updateDuration(qint64 duration);
void updatePosition(qint64 position);
private:
Ui::VideoPlayer ui;
QMediaPlayer *m_player;
QLabel *m_durationLabel;
QSlider *m_progressSlider;
};
VideoPlayer::VideoPlayer(QWidget *parent) :
QWidget(parent),
m_player(new QMediaPlayer(this)),
m_durationLabel(new QLabel(this)),
m_progressSlider(new QSlider(Qt::Horizontal, this))
{
ui.setupUi(this);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(ui.mediaPlayer);
layout->addWidget(m_progressSlider);
QHBoxLayout *buttonLayout = new QHBoxLayout;
QPushButton *openButton = new QPushButton(tr("Open"));
QPushButton *playButton = new QPushButton(tr("Play"));
QPushButton *pauseButton = new QPushButton(tr("Pause"));
QPushButton *stopButton = new QPushButton(tr("Stop"));
buttonLayout->addWidget(openButton);
buttonLayout->addWidget(playButton);
buttonLayout->addWidget(pauseButton);
buttonLayout->addWidget(stopButton);
layout->addLayout(buttonLayout);
QHBoxLayout *labelLayout = new QHBoxLayout;
labelLayout->addWidget(m_durationLabel);
layout->addLayout(labelLayout);
setLayout(layout);
m_player->setVideoOutput(ui.mediaPlayer);
connect(openButton, &QPushButton::clicked, this, &VideoPlayer::openFile);
connect(playButton, &QPushButton::clicked, this, &VideoPlayer::play);
connect(pauseButton, &QPushButton::clicked, this, &VideoPlayer::pause);
connect(stopButton, &QPushButton::clicked, this, &VideoPlayer::stop);
connect(m_progressSlider, &QSlider::sliderMoved, this, [this](int value) {
m_player->setPosition(value * m_player->duration() / m_progressSlider->maximum());
});
connect(m_player, &QMediaPlayer::durationChanged, this, &VideoPlayer::updateDuration);
connect(m_player, &QMediaPlayer::positionChanged, this, &VideoPlayer::updatePosition);
}
VideoPlayer::~VideoPlayer()
{
}
void VideoPlayer::openFile()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Video"), ".", tr("Video Files (*.mp4 *.avi *.wmv)"));
if (!fileName.isEmpty()) {
m_player->setMedia(QUrl::fromLocalFile(fileName));
m_player->play();
}
}
void VideoPlayer::play()
{
m_player->play();
}
void VideoPlayer::pause()
{
m_player->pause();
}
void VideoPlayer::stop()
{
m_player->stop();
}
void VideoPlayer::updateDuration(qint64 duration)
{
QString durationText = QTime(0, 0).addMSecs(duration).toString("hh:mm:ss");
m_durationLabel->setText(QString("00:00:00 / %1").arg(durationText));
m_progressSlider->setMaximum(duration / 1000);
}
void VideoPlayer::updatePosition(qint64 position)
{
QString positionText = QTime(0, 0).addMSecs(position).toString("hh:mm:ss");
m_durationLabel->setText(QString("%1 / %2").arg(positionText).arg(m_durationLabel->text().split("/").last()));
m_progressSlider->setValue(position / 1000);
}
```
在主函数中创建一个VideoPlayer对象,然后将其显示出来即可:
```cpp
#include "VideoPlayer.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
VideoPlayer player;
player.show();
return a.exec();
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)