写在前面(学习Qt的记录):因为最初的程序设计问题,造成代码的耦合性很高,各个功能函数都集中在主函数中,某些功能未有完全实现(左边树列表点下一首时,继续播放在左列表的),记录经验,吸取教训;代码就贴的主要功能,还有XML文件配置等没贴出,下面是运行效果图。
以下为各个功能函数:
1.整体可拖动窗口:
声明:
//整体可拖动窗口
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
定义:
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton)//只响应左键
{
m_draging = true;
m_startPostion = event->globalPos();
m_framePostion = frameGeometry().topLeft();
}
QWidget::mousePressEvent(event);//调用父类保持原有按键行文
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton)
{
QPoint offset = event->globalPos() - m_startPostion;
move(m_framePostion+offset);
}
QWidget::mouseMoveEvent(event);
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
m_draging = false;
QWidget::mouseReleaseEvent(event);
}
左列表双击播放:
//左菜单双击播放
void MainWindow::treewidgetMusic(QString leftMusic)
{
int count = ui->MusicWidget->count();
for(int i = 0;i < count; i++)
{
if(leftMusic == ui->MusicWidget->item(i)->text()) //比较右边列表的歌名,此方法有缺陷。
{
m_playList->setCurrentIndex(i);
m_player->play();
timer->start(1000);
QIcon icon("D:/QT_Library/MusicPlayer/icon/newStop.png");
ui->playerButton->setIcon(icon);
player_stop = false;
ui->pregressHorizontalSlider->setEnabled(true);
ui->lastButton->setEnabled(true);
ui->nextButton->setEnabled(true);
ui->delButton->setEnabled(true);
ui->playerButton->setEnabled(true);
}
}
}
右键菜单(槽函数连接):
QAction *m_Love;
QAction *m_Try;
void MainWindow::creatAction()
{
m_Love = new QAction("添加到My-Love",this);
m_Try = new QAction("添加到Try-hear",this);
}
void MainWindow::showRightMenu(QPoint pos)
{
QMenu *menu = new QMenu;
menu->addAction(m_Love);
menu->addAction(m_Try);
if(ui->MusicWidget->currentItem())
{
// menu->exec(QCursor::pos());
menu->exec(ui->MusicWidget->mapToGlobal(pos));//全局坐标显示
}
}
播放暂停:
//播放/暂停
void MainWindow::OnPlayBtnClicked()
{
ui->pregressHorizontalSlider->setEnabled(true);
//正在播放showLabe显示
ui->showLabel->setText(ui->MusicWidget->currentItem()->text());
if((player_stop == false)
|| (m_player->state() == QMediaPlayer::PlayingState))
{
timer->stop();
m_player->pause();
QIcon icon("D:/QT_Library/MusicPlayer/icon/newPlayer.png");
ui->playerButton->setIcon(icon);
player_stop = true;
}
else
if((player_stop == true)
|| (m_player->state() == QMediaPlayer::PausedState))
{
timer->start(1000);
m_player->play();
QIcon icon("D:/QT_Library/MusicPlayer/icon/newStop.png");
ui->playerButton->setIcon(icon);
player_stop = false;
}
}
双击播放:
void MainWindow::OnDoubleClicked(QModelIndex index)
{
player_stop = true;
ui->pregressHorizontalSlider->setEnabled(true);
ui->lastButton->setEnabled(true);
ui->nextButton->setEnabled(true);
ui->delButton->setEnabled(true);
ui->playerButton->setEnabled(true);
QIcon icon("D:/QT_Library/MusicPlayer/icon/newStop.png");
ui->playerButton->setIcon(icon);
player_stop = false;
ui->showLabel->setText(ui->MusicWidget->currentItem()->text());
m_playList->setCurrentIndex(ui->MusicWidget->currentIndex().row() );
m_player->play();
timer->start(1000);
time.setDate(date);//所以上边的data初始化时,数值不为0就好
time.setTime(timeProgress);
}
上一首:
//上一首
void MainWindow::LastMusic()
{
// ui->playerButton->setText("暂停");
QIcon icon("D:/QT_Library/MusicPlayer/icon/newStop.png");
ui->playerButton->setIcon(icon);
player_stop = false;
m_player->setPosition(0);
ui->pregressHorizontalSlider->setValue(m_player->position());
//这里注意的是,和我们正常数数一样,mediaCount是从1开始计数的
if(m_playList->currentIndex()!=0)
{
m_playList->setCurrentIndex(m_playList->previousIndex());
m_playList->previous();
m_player->play();
}
else
{
m_playList->setCurrentIndex(m_playList->mediaCount()-1);
m_player->play();
}
}
下一首:
//下一首
void MainWindow::NextMusic()
{
QIcon icon("D:/QT_Library/MusicPlayer/icon/newStop.png");
ui->playerButton->setIcon(icon);
player_stop = false;
m_player->setPosition(0);
ui->pregressHorizontalSlider->setValue(m_player->position());
//这里注意的是,和我们正常数数一样,mediaCount是从1开始计数的
if(m_playList->mediaCount() - m_playList->currentIndex()!=1)
{
m_playList->setCurrentIndex(m_playList->nextIndex());
m_player->play();
}
else
{
m_playList->setCurrentIndex(0);
m_player->play();
}
}
播放模式:
//播放模式
void MainWindow::OnConBoxChangeClicked(int number)
{
switch (number)
{
case 0:
m_playList->setPlaybackMode(QMediaPlaylist::Loop);//循环列表
qDebug() << "0";
break;
case 1:
m_playList->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);//当前单曲循环
qDebug() << "1";
break;
case 2:
m_playList->setPlaybackMode(QMediaPlaylist::Random);//随机
qDebug() << "2";
break;
default:
break;
}
}
音量调节:
//音量调节
void MainWindow::OnVolumeChange(int volume)
{
m_player->setVolume(volume);
}
快进://快进
void MainWindow::HorizontalSliderChange(int value)
{
if(m_player->state() == QMediaPlayer::PlayingState)
{
m_player->setPosition(value);
m_player->pause();
QIcon icon("D:/QT_Library/MusicPlayer/icon/newPlayer.png");
ui->playerButton->setIcon(icon);
player_stop = true;
}
m_player->play();
QIcon icon("D:/QT_Library/MusicPlayer/icon/newStop.png");
ui->playerButton->setIcon(icon);
player_stop = false;
}
进度条跟随:
//进度条跟随
void MainWindow::ChangPostion()
{
//music->position()获取音乐当前播放位置
curr_position = m_player->position();
//music->duration()获取音乐总长度(注意:这个长度是音乐大小)
ui->pregressHorizontalSlider->setMaximum(m_player->duration());
if(curr_position >= ui->pregressHorizontalSlider->maximum())//播放完毕
{
timer->stop();
m_player->stop();
QIcon icon("D:/QT_Library/MusicPlayer/icon/newPlayer.png");
ui->playerButton->setIcon(icon);
player_stop = true;
ui->pregressHorizontalSlider->setSliderPosition(0);
time.setDate(date);
time.setTime(timeProgress);//重新把时间标签改为00:00
m_playList->next();
}
timer->start(1000);
m_player->play();
//刷新滑块位置
ui->pregressHorizontalSlider->setSliderPosition(curr_position);
}
实时播放时间:
//实时时间
void MainWindow::upTime(qint64 position)
{
QTime duration(0, position / 60000, qRound((position % 60000) / 1000.0));
ui->startTimeLabel->setText(duration.toString("mm:ss"));
if(!(m_playList->isEmpty()))
{
//当前播放歌曲在列表醒目提示
ui->MusicWidget->setCurrentRow(m_playList->currentIndex());
//正在播放showLabe显示
ui->showLabel->setText(ui->MusicWidget->currentItem()->text());
}
}
播放总时间:
//播放总时长
void MainWindow::endTime(qint64 position)
{
if(m_player->state() == QMediaPlayer::PlayingState)
{
QTime duration(0, position / 60000, qRound((position % 60000) / 1000.0));
ui->endTimelabel->setText(duration.toString("mm:ss"));
}
}
删除歌曲://删除
void MainWindow::delBtnClicked()
{
QListWidgetItem *item = ui->MusicWidget->currentItem();
ui->MusicWidget->removeItemWidget(item);
delete item;
if(m_playList->mediaCount() > 1)
{
qDebug() << "++++++++++++++++++++";
qDebug() << m_playList->mediaCount();
m_playList->removeMedia(m_playList->currentIndex());
m_player->EndOfMedia;
}
else
if(m_playList->mediaCount() == 1)
{
qDebug() << "--------------";
qDebug() << m_playList->mediaCount();
m_playList->clear();
m_player->stop();
timer->stop();
ui->showLabel->clear();
ui->MusicWidget->clear();
ui->playerButton->setDisabled(true);
ui->pregressHorizontalSlider->setDisabled(true);
ui->nextButton->setDisabled(true);
ui->lastButton->setDisabled(true);
ui->delButton->setDisabled(true);
}
}
美化相关:
//Qcombox美化设置/委托
QStyledItemDelegate* itemDelegate = new QStyledItemDelegate();
ui->comboBox->setItemDelegate(itemDelegate);
//允许鼠标右键操作
ui->MusicWidget->setContextMenuPolicy(Qt::CustomContextMenu);
//关闭滚动条
ui->MusicWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->MusicWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setWindowFlags(Qt::Window | Qt::FramelessWindowHint);//无边框设置
setAttribute(Qt::WA_TranslucentBackground);//设置背景透明
主设计界面(偷懒,皮肤直接用的styleSheet)
具体代码: 源码下载