呃……反正这篇博客我是本不打算写的,这次的音乐播放器没什么大的变化,主要是在界面上稍微做了些改进,代码是照搬v1.0的,不过这次加了换肤(这个是班门弄斧了些哈),不过话说回来,也是自己辛辛苦苦自己组装的音乐播放器吧,没有窃取别人家的劳动成果,我感觉这个还算的上是一个让我欣慰的原因,一向是剃头挑子-一头热的我,不知道这次能坚持多久,只能说声走一步算一步了。甭废话了,放代码:
widget.h
#include"widget.h"
#include"ui_widget.h"
#include
Widget::Widget(QWidget*parent):
QWidget(parent),
ui(newUi::Widget)
{
ui->setupUi(this);
player=newQMediaPlayer(this);
list=newQMediaPlaylist;
QDirdir("E:/音乐");
foreach(QFileInfoinfo,dir.entryInfoList())
{
if(info.exists())
{
list->addMedia(QUrl(info.absoluteFilePath()));
ui->show->addItem(info.fileName());
}
}
ui->V->setRange(0,100);
list->setCurrentIndex(1);
player->setPlaylist(list);
player->setVolume(50);
connect(ui->V,SIGNAL(valueChanged(int)),player,SLOT(setVolume(int)));
intwidth=this->width();
QToolButton*minButton=newQToolButton(this);
QToolButton*closeButton=newQToolButton(this);
connect(closeButton,SIGNAL(clicked(bool)),this,SLOT(on_closeButton_clicked()));
connect(minButton,SIGNAL(clicked(bool)),this,SLOT(on_minButton_clicked()));
QPixmapminPix=style()->standardPixmap(QStyle::SP_TitleBarUnshadeButton);
QPixmapclosePix=style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
minButton->setIcon(minPix);
closeButton->setIcon(closePix);
minButton->setGeometry(width-46,5,20,20);
closeButton->setGeometry(width-25,5,20,20);
minButton->setToolTip(tr("换肤"));
closeButton->setToolTip(tr("关闭"));
minButton->setStyleSheet("background-color:transparent;");
closeButton->setStyleSheet("background-color:transparent;");
ui->num->setText("已找到"+QString::number(ui->show->count(),10)+"首音乐");
player->setVolume(25);
ui->V->setValue(25);
ui->V->accessibleDescription();
connect(ui->V,SIGNAL(valueChanged(int)),player,SLOT(setVolume(int)));
connect(ui->show,SIGNAL(clicked(QModelIndex)),this,SLOT(toplay(QModelIndex)));
}
Widget::~Widget()
{
deleteui;
}
voidWidget::on_Prvious_clicked()
{
list->previous();
player->play();
ui->show->autoFillBackground();
}
voidWidget::on_Play_clicked()
{
if(ui->Play->text()=="播放")
{
player->play();
ui->Play->setText("暂停");
}
else
{
player->pause();
ui->Play->setText("播放");
}
}
voidWidget::on_Next_clicked()
{
list->next();
player->play();
}
voidWidget::mousePressEvent(QMouseEvent*event)
{
if(event->button()==Qt::LeftButton)
{
mouse_press=true;
move_point=event->pos();//鼠标相对于窗体的位置(或者使用event->globalPos()-this->pos())
}
}
voidWidget::mouseMoveEvent(QMouseEvent*event)
{
if(mouse_press)//若鼠标左键被按下
{
QPointmove_pos=event->globalPos();//鼠标相对于屏幕的位置
this->move(move_pos-move_point);//移动主窗体位置
}
}
voidWidget::mouseReleaseEvent(QMouseEvent*event)
{
mouse_press=false;//设置鼠标为未被按下
}
voidWidget::on_closeButton_clicked()
{
this->close();
}
voidWidget::on_minButton_clicked()
{
staticinti=0;
i++;
QPalettep;
QPixmap*map;
switch(i%4){
case0:
map=newQPixmap(":/picture/001.jpg");
break;
case1:
map=newQPixmap(":/picture/002.jpg");
break;
case2:
map=newQPixmap(":/picture/003.jpg");
break;
case3:
map=newQPixmap(":/picture/004.jpg");
break;
default:
break;
}
p.setBrush(QPalette::Window,QBrush(*map));
this->setPalette(p);
}
voidWidget::toplay(QModelIndexx)
{
list->setCurrentIndex(x.row());
player->play();
}
main.cpp
#include"widget.h"
#include
intmain(intargc,char*argv[])
{
QApplicationa(argc,argv);
Widgetw;
w.setWindowFlags(Qt::FramelessWindowHint);
w.setWindowTitle("MusicBox");
w.setAutoFillBackground(true);
QPalettep;
QPixmappixmap("E:/照片/壁纸/002.jpg");
p.setBrush(QPalette::Window,QBrush(pixmap));
w.setPalette(p);
QPixmappixmap1(":/picture/MP3.ico");
QIconpicture(pixmap1);
w.setWindowIcon(picture);
w.show();
returna.exec();
}
widget.h
#ifndefWIDGET_H
#defineWIDGET_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
namespaceUi{
classWidget;
}
classWidget:publicQWidget
{
Q_OBJECT
public:
explicitWidget(QWidget*parent=0);
voidmouseReleaseEvent(QMouseEvent*event);
voidmouseMoveEvent(QMouseEvent*event);
voidmousePressEvent(QMouseEvent*event);
~Widget();
privateslots:
voidtoplay(QModelIndexx);
voidon_Prvious_clicked();
voidon_Play_clicked();
voidon_Next_clicked();
voidon_minButton_clicked();
voidon_closeButton_clicked();
private:
Ui::Widget*ui;
QMediaPlayer*player;
QMediaPlaylist*list;
QPointmove_point;//移动的距离
boolmouse_press;//鼠标按下
};
#endif//WIDGET_H