QT5:C++实现基于multimedia的音乐播放器(一)
发布时间:2020-12-15 04:40:19 所属栏目:百科 来源:网络整理
导读:上一篇里简略的描述了一下播放器的实现,这一篇开始具体描述一下过程。 环境配置:Qt Creator 打开Qt Creator,创建一个new project,项目名称随你喜欢(我的是MusicPlayer),类名也随你喜欢(我的是Music),基类选择QWidget,不勾选界面UI(你也可以勾选U
上一篇里简略的描述了一下播放器的实现,这一篇开始具体描述一下过程。 环境配置:Qt Creator 打开Qt Creator,创建一个new project,项目名称随你喜欢(我的是MusicPlayer),类名也随你喜欢(我的是Music),基类选择QWidget,不勾选界面UI(你也可以勾选UI,用QT自带的UI设计来创建界面),然后要记住项目保存路径不能有中文。 创建成功后,在MusicPlayer.pro(项目名称.pro)里加上“QT += multimedia”这一句: QT += QT += greaterThan(QT_MAJOR_VERSION,): QT += widgets
然后在头文件music.h(类名.h)里添加要用到的头文件名: #include
并且在music类里写上要用到的对象成员和函数: Music : Music(QWidget *parent =
volumChange( showMessage( seekChange(
QPushButton * QPushButton * QPushButton * QPushButton * QPushButton * QPushButton * QPushButton * QPushButton * QSlider * QSlider * QLabel * QLabel * QLabel * QLabel * QLabel * QListWidget * QTimer * QTimer * QMediaPlayer * QMediaPlaylist *
paintEvent(QPaintEvent * mousePressEvent(QMouseEvent * mouseMoveEvent(QMouseEvent * };
还要在源文件music.cpp里加上要用的头文件名: #include
#include
接着添加资源文件,把要用的图标和背景图片都添加到项目里,然后就可以在music.cpp里写播放器的界面了。 Music::Music(QWidget * background.load();
-> resize(background.width(),background.height());
-> setWindowFlags(Qt::FramelessWindowHint);
add = moved = timer = QTimer( timer2 = QTimer(
player = QMediaPlayer();
playList =
init_controls();
init_skin();
connect(player,SIGNAL(metaDataAvailableChanged()),,SLOT(showMessage( connect(seekSlider,SIGNAL(sliderMoved()),SLOT(seekChange( }
因为我创建的是一个无法移动的窗体,所以重写鼠标左键函数来让它可以被移动,这样的话,鼠标左键按住时可以拖动窗体了:
Music::mousePressEvent(QMouseEvent *(->buttons()===->globalPos()--><span style="color: #0000ff">void Music::mouseMoveEvent(QMouseEvent <span style="color: #0000ff">event<span style="color: #000000">)
{ <span style="color: #0000ff">if(<span style="color: #0000ff">event->buttons() & Qt::LeftButton) <span style="color: #000000"> { <span style="color: #0000ff">this ->move(<span style="color: #0000ff">event->globalPos() -<span style="color: #000000"> dragPosition); <span style="color: #0000ff">event-><span style="color: #000000">accept(); } } <span style="color: #008000">//<span style="color: #008000">绘制背景 <span style="color: #0000ff">void Music::paintEvent(QPaintEvent <span style="color: #0000ff">event<span style="color: #000000">) { QPainter paint(<span style="color: #0000ff">this<span style="color: #000000">); QPixmap backgound; backgound.load(<span style="color: #800000">"<span style="color: #800000">:/image/music_bg.bmp<span style="color: #800000">"<span style="color: #000000">); paint.drawPixmap(<span style="color: #800080">0,<span style="color: #800080">0<span style="color: #000000">,backgound.width(),backgound.height(),backgound); <span style="color: #0000ff">event -><span style="color: #000000">accept(); } 然后写创建按钮的函数以及连接槽函数来响应信号的连接语句:
= QPushButton(-> setObjectName();
BtnClose -> setGeometry(,,,);
BtnClose -> setToolTip(tr(-> setCursor(QCursor(Qt::PointingHandCursor));
= QPushButton(->setObjectName(tr(->setGeometry(,,,->setToolTip(tr(->
playPattern</span>=<span style="color: #0000ff">new</span> QPushButton(<span style="color: #0000ff">this</span><span style="color: #000000">);
</span><span style="color: #008000">/*</span><span style="color: #008000">QIcon icon1(":/image/Seq.png");
playPattern->setIcon(icon1);</span><span style="color: #008000">*/</span><span style="color: #000000">
playPattern</span>->setObjectName(tr(<span style="color: #800000">"</span><span style="color: #800000">playPattern</span><span style="color: #800000">"</span><span style="color: #000000">));
playPattern</span>->setGeometry(<span style="color: #800080">20</span>,<span style="color: #800080">255</span>,<span style="color: #800080">50</span>,<span style="color: #800080">50</span><span style="color: #000000">);
playPattern</span>->setToolTip(tr(<span style="color: #800000">"</span><span style="color: #800000">列表循环</span><span style="color: #800000">"</span><span style="color: #000000">));
playPattern</span>-><span style="color: #000000">setCursor(QCursor(Qt::PointingHandCursor));
BtnPlay</span>=<span style="color: #0000ff">new</span> QPushButton(<span style="color: #0000ff">this</span><span style="color: #000000">);
BtnPlay</span>->setObjectName(tr(<span style="color: #800000">"</span><span style="color: #800000">BtnPlay</span><span style="color: #800000">"</span><span style="color: #000000">));
BtnPlay</span>->setGeometry(<span style="color: #800080">160</span>,<span style="color: #800080">250</span>,<span style="color: #800080">56</span>,<span style="color: #800080">56</span><span style="color: #000000">);
BtnPlay</span>->setToolTip(tr(<span style="color: #800000">"</span><span style="color: #800000">播放</span><span style="color: #800000">"</span><span style="color: #000000">));
BtnPlay</span>-><span style="color: #000000">setCursor(QCursor(Qt::PointingHandCursor));
BtnPrev</span>=<span style="color: #0000ff">new</span> QPushButton(<span style="color: #0000ff">this</span><span style="color: #000000">);
BtnPrev</span>->setObjectName(tr(<span style="color: #800000">"</span><span style="color: #800000">BtnPrev</span><span style="color: #800000">"</span><span style="color: #000000">));
BtnPrev</span>->setGeometry(<span style="color: #800080">120</span>,<span style="color: #800080">45</span>,<span style="color: #800080">45</span><span style="color: #000000">);
BtnPrev</span>->setToolTip(tr(<span style="color: #800000">"</span><span style="color: #800000">上一首</span><span style="color: #800000">"</span><span style="color: #000000">));
BtnPrev</span>-><span style="color: #000000">setCursor(QCursor(Qt::PointingHandCursor));
BtnNext</span>=<span style="color: #0000ff">new</span> QPushButton(<span style="color: #0000ff">this</span><span style="color: #000000">);
BtnNext</span>->setObjectName(tr(<span style="color: #800000">"</span><span style="color: #800000">BtnNext</span><span style="color: #800000">"</span><span style="color: #000000">));
BtnNext</span>->setGeometry(<span style="color: #800080">210</span>,<span style="color: #800080">45</span><span style="color: #000000">);
BtnNext</span>->setToolTip(tr(<span style="color: #800000">"</span><span style="color: #800000">下一首</span><span style="color: #800000">"</span><span style="color: #000000">));
BtnNext</span>-><span style="color: #000000">setCursor(QCursor(Qt::PointingHandCursor));
muteButton</span>=<span style="color: #0000ff">new</span> QPushButton(<span style="color: #0000ff">this</span><span style="color: #000000">);
muteButton</span>->setObjectName(tr(<span style="color: #800000">"</span><span style="color: #800000">muteButton</span><span style="color: #800000">"</span><span style="color: #000000">));
muteButton</span>->setGeometry(<span style="color: #800080">370</span>,<span style="color: #800080">320</span>,<span style="color: #800080">25</span><span style="color: #000000">);
muteButton</span>->setToolTip(tr(<span style="color: #800000">"</span><span style="color: #800000">关闭声音</span><span style="color: #800000">"</span><span style="color: #000000">));
muteButton</span>-><span style="color: #000000">setCursor(QCursor(Qt::PointingHandCursor));
volumeControl</span>=<span style="color: #0000ff">new</span> QSlider(Qt::Vertical,<span style="color: #0000ff">this</span>);<span style="color: #008000">//</span><span style="color: #008000">QSlider(Qt::Vertical,this)创建一个竖直方向的滑动条QSlider控件</span>
volumeControl->setObjectName(tr(<span style="color: #800000">"</span><span style="color: #800000">volumeControl</span><span style="color: #800000">"</span><span style="color: #000000">));
volumeControl</span>->setGeometry(<span style="color: #800080">375</span>,<span style="color: #800080">240</span>,<span style="color: #800080">15</span>,<span style="color: #800080">80</span><span style="color: #000000">);
volumeControl</span>-><span style="color: #000000">setCursor(QCursor(Qt::PointingHandCursor));
volumeControl</span>->setRange(<span style="color: #800080">0</span>,<span style="color: #800080">100</span>);<span style="color: #008000">//</span><span style="color: #008000">设置滑动条控件的最小值和最大值</span>
volumeControl ->setValue(<span style="color: #800080">50</span>);<span style="color: #008000">//</span><span style="color: #008000">设置初值为50;</span>
<span style="color: #000000"> seekSlider -> setGeometry(<span style="color: #800080">100,<span style="color: #800080">345,<span style="color: #800080">200,<span style="color: #800080">15<span style="color: #000000">); seekSlider->setObjectName(tr(<span style="color: #800000">"<span style="color: #800000">seekSlider<span style="color: #800000">"<span style="color: #000000">)); seekSlider -><span style="color: #000000"> setCursor(QCursor(Qt::PointingHandCursor));
<span style="color: #000000"> title -> setGeometry(<span style="color: #800080">5,<span style="color: #800080">30<span style="color: #000000">); title ->setFont(QFont(<span style="color: #800000">"<span style="color: #800000">Times<span style="color: #800000">",<span style="color: #800080">15<span style="color: #000000">,QFont::Bold)); QPalette pa; pa.setColor(QPalette::WindowText,QColor(<span style="color: #800080">0,<span style="color: #800080">0<span style="color: #000000">)); title -><span style="color: #000000"> setPalette(pa); title -> setText(<span style="color: #800000">"<span style="color: #800000">MusicPlayer<span style="color: #800000">"<span style="color: #000000">);
} 但界面很丑,所以下一步就是加载qss文件,绘制界面样式:
QFile file( ->
}
我的qss代码如下(qss和css的语法几乎一模一样): QPushButton#playPattern:! border-image: url(:/image/ border-image: url(:/image/ border-image: url(:/image/ QPushButton#BtnClose:! border-image: url(:/image/ border-image: url(:/image/ border-image: url(:/image/ QPushButton#BtnClose:focus{padding:-
border-image: url(:/image/
QPushButton#BtnMin:! border-image: url(:/image/ border-image: url(:/image/ border-image: url(:/image/ QPushButton#BtnMin:focus{padding:-
QPushButton#BtnPlay:! border-image: url(:/image/ border-image: url(:/image/ border-image: url(:/image/ padding:-
QPushButton#BtnPrev:! border-image: url(:/image/ border-image: url(:/image/ border-image: url(:/image/ QPushButton#BtnPrev:focus{padding:-
QPushButton#BtnNext:! border-image: url(:/image/ border-image: url(:/image/ border-image: url(:/image/ QPushButton#BtnNext:focus{padding:-
QPushButton#muteButton:! border-image: url(:/image/ border-image: url(:/image/ border-image: url(:/image/ QPushButton#muteButton:focus{padding:-
background: rgba(, border-
QSlider#volumeControl::sub- background: rgba(, border: 0px solid # border-
QSlider#volumeControl::add- |