废话不多写直接上图上代码,其中有很多不规范的地方,希望大佬们指正。
设计一个MP3播放器,要求:使用Linux下的madplay进行歌曲的播放,一共有三大项:
在本次课程设计中我主要负责Qt图形化的设计开发。
system("madplay xxx.mp3 &");
进行歌曲的播放简单解析:
播放展示:
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void on_pushbutton_click();
QMainWindow *qmainwindow;
bool AddNode(const char *songName,const char* path);
void InitializeList();
char *MoveNext();
char *MovePrevious();
void RemoveNode(QString s);
private slots:
void on_pushButton_5_clicked();
void on_listWidget_itemClicked(QListWidgetItem *item);
//void on_pushButton_3_clicked();
//void on_pushButton_4_clicked();
void on_pushButton_2_clicked();
//void keyPressEvent( QKeyEvent *k );
void on_pushButton_6_clicked();
// void on_mySlider_sliderReleased();
// void on_mySlider_sliderMoved(int position);
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
//Double director circle ListNode
struct Node
{
char songName[512];
struct Node *next;
struct Node *prev;
int flag;
char path[1024];
};
typedef struct Node* List;
static List head ;
static List tail ;
static List current;
void Widget::InitializeList()
{
head =(struct Node*)malloc(sizeof(struct Node));
tail = head;
head->next = tail;
head->prev = tail;
//current = head->next;
songNum = 0;
}
bool Widget::AddNode(const char *songName, const char *path)
{
List pnew;
List scan = head;
pnew = (struct Node*)malloc(sizeof(struct Node));//在堆上创建节点
if(pnew == NULL)
{
printf("malloc create error\n");
return false;
}
strcpy(pnew->path,path);
strcpy(pnew->songName,songName);
pnew->next = NULL;
pnew->prev = NULL;
pnew->flag = 0;
//judge overlap
while(scan->next != tail){
if(!strcmp(pnew->songName,scan->next->songName))
{
qDebug() << "this song already exists" << endl;
return false;
}
scan = scan->next;//找打链表尾
}
pnew->prev = scan;
pnew->next = tail;
scan->next = pnew;
tail->prev = pnew;
songNum++;
if(songNum == 1)
current = head->next;
return true;
}
char* Widget::MoveNext()
{
if(current->next != tail){
current = current->next;
}
else
{
current = head->next;
}
printf("this song is playing %s",current->songName);
return current->path; //fullname
}
char* Widget:: MovePrevious()
{
if(current->prev != head)
{
current = current->prev;
}
else
{
current = head->prev;
}
printf("this song is playing %s\n",current->songName);
return current->path;
}
void Widget::RemoveNode(QString s)
{
while(current->songName != s)
{
current = current->next;
}
List temp = current;
temp->next->prev = temp->prev;
current->prev->next = current->next;
// current->next->prev = current->prev;
}
//播放 pushButton_2
connect(ui->pushButton_2,&QPushButton::clicked,this,[=](){
//judge songList whether NULL
if(songNum == 0)
{
QMessageBox::critical(this,"critical","no songs in songList");
return ;
}
//"madplay /home/itcast/Desktop/1.mp3 &"
// -s 0:0:01:11 set action time
QString str = "madplay "+buf+" &";
const char* strChar = str.toLatin1();
QString RealfileName = buf.mid(buf.lastIndexOf('/')+1);
ui->lineEdit->setText(RealfileName);
if(flag)
{
if(buf!=onbuf)
{
system("killall -9 madplay");
system(strChar);
ui->lineEdit->setText(RealfileName);
onbuf = buf;
}
else
{
//system("killall -9 madplay");
system(strChar);
ui->lineEdit->setText(RealfileName);
}
}
else
{
system("killall -CONT madplay &");
flag = 1;
return;
}
//show lyric
QString lyricfile = buf.mid(0,buf.lastIndexOf('.'));
lyricfile = lyricfile+".txt";
//open lyricfile and show lyric
QFile file(lyricfile);
file.open(QIODevice::ReadOnly);
QByteArray arr;
arr = file.readAll();
ui->textEdit->setText(arr);
//change current
while(current->path != buf)
{
current = current->next;
}
//change label Image
int num2 = buf.lastIndexOf('/');
QString pictureName = buf.mid(num2,-1);
pictureName = pictureName.mid(0,pictureName.lastIndexOf('.'));
QString pictureName2 =":Image"+pictureName+ "1.jpg";
pictureName =":Image"+pictureName+ ".jpg";
//reset widget background
QPalette palette;
this->setAutoFillBackground(true);
QPixmap pixmap;
pixmap.load(pictureName);
//palette.setBrush(QPalette::Background,QBrush(pixmap));
//this->setPalette(palette);
palette.setBrush(this->backgroundRole(),QBrush(pixmap.scaled(1118,615).scaled(1118,615,Qt::IgnoreAspectRatio,Qt::SmoothTransformation)));
QPainter p3(&pixmap);
p3.setCompositionMode(QPainter::CompositionMode_Source);
p3.drawPixmap(0, 0, pixmap);
p3.setCompositionMode(QPainter::CompositionMode_DestinationIn);
this->setPalette(palette);
//添加标签中的图片
QMovie *myMovie6 = new QMovie(pictureName2);
ui->label_4->setMovie(myMovie6);
ui->label_4->setScaledContents(true);
myMovie6->start();
//set opacity
QGraphicsOpacityEffect* opacityEffect2 = new QGraphicsOpacityEffect;
opacityEffect2->setOpacity(0);
ui->label_6->setGraphicsEffect(opacityEffect2);
});
//暂停 pushButton
connect(ui->pushButton,&QPushButton::clicked,this,[=](){
if(songNum == 0)
{
QMessageBox::critical(this,"critical","no songs in songList");
return ;
}
system("killall -STOP madplay &");
flag = 0;
});
//last一曲
connect(ui->pushButton_3,&QPushButton::clicked,this,[=](){
if(songNum == 0)
{
QMessageBox::critical(this,"critical","no songs in songList");
return ;
}
buf = QString(MovePrevious());
onbuf = buf;
system("killall -9 madplay");
QString RealfileName = buf.mid(buf.lastIndexOf('/')+1);
ui->lineEdit->setText(RealfileName);
QString str = "madplay "+buf+" &";
const char* strChar = str.toLatin1();
system(strChar);
//show lyric
QString lyricfile = buf.mid(0,buf.lastIndexOf('.'));
lyricfile = lyricfile+".txt";
//open lyricfile and show lyric
QFile file(lyricfile);
file.open(QIODevice::ReadOnly);
QByteArray arr;
arr = file.readAll();
ui->textEdit->setText(arr);
//change label Image
int num2 = buf.lastIndexOf('/');
QString pictureName = buf.mid(num2,-1);
pictureName = pictureName.mid(0,pictureName.lastIndexOf('.'));
QString pictureName2 =":Image"+pictureName+ "1.jpg";
pictureName =":Image"+pictureName+ ".jpg";
//reset widget background
QPalette palette;
this->setAutoFillBackground(true);
QPixmap pixmap;
pixmap.load(pictureName);
palette.setBrush(QPalette::Background,QBrush(pixmap));
palette.setBrush(this->backgroundRole(),QBrush(pixmap.scaled(1118,615).scaled(1118,615,Qt::IgnoreAspectRatio,Qt::SmoothTransformation)));
QPainter p3(&pixmap);
p3.setCompositionMode(QPainter::CompositionMode_Source);
p3.drawPixmap(0, 0, pixmap);
p3.setCompositionMode(QPainter::CompositionMode_DestinationIn);
this->setPalette(palette);
//添加标签中的图片
QMovie *myMovie6 = new QMovie(pictureName2);
ui->label_4->setMovie(myMovie6);
ui->label_4->setScaledContents(true);
myMovie6->start();
//set opacity
QGraphicsOpacityEffect* opacityEffect2 = new QGraphicsOpacityEffect;
opacityEffect2->setOpacity(0);
ui->label_6->setGraphicsEffect(opacityEffect2);
});
//next一曲
//buf = QString(MoveNext());
connect(ui->pushButton_4,&QPushButton::clicked,this,[=](){
if(songNum == 0)
{
QMessageBox::critical(this,"critical","no songs in songList");
return ;
}
//get filename
buf = QString(MoveNext());
onbuf = buf;
//kill singing song
system("killall -9 madplay");
QString RealfileName = buf.mid(buf.lastIndexOf('/')+1);
ui->lineEdit->setText(RealfileName);
QString str = "madplay "+buf+" &";
const char* strChar = str.toLatin1();
system(strChar);
//qDebug() << "bofangyinyue xiayishou" <textEdit->setText(arr);
//change label Image
int num2 = buf.lastIndexOf('/');
QString pictureName = buf.mid(num2,-1);
pictureName = pictureName.mid(0,pictureName.lastIndexOf('.'));
QString pictureName2 =":Image"+pictureName+ "1.jpg";
pictureName =":Image"+pictureName+ ".jpg";
//reset widget background
QPalette palette;
this->setAutoFillBackground(true);
QPixmap pixmap;
pixmap.load(pictureName);
palette.setBrush(QPalette::Background,QBrush(pixmap));
palette.setBrush(this->backgroundRole(),QBrush(pixmap.scaled(1118,615).scaled(1118,615,Qt::IgnoreAspectRatio,Qt::SmoothTransformation)));
QPainter p3(&pixmap);
p3.setCompositionMode(QPainter::CompositionMode_Source);
p3.drawPixmap(0, 0, pixmap);
p3.setCompositionMode(QPainter::CompositionMode_DestinationIn);
this->setPalette(palette);
//添加标签中的图片
QMovie *myMovie6 = new QMovie(pictureName2);
ui->label_4->setMovie(myMovie6);
ui->label_4->setScaledContents(true);
myMovie6->start();
//set opacity
QGraphicsOpacityEffect* opacityEffect2 = new QGraphicsOpacityEffect;
opacityEffect2->setOpacity(0);
ui->label_6->setGraphicsEffect(opacityEffect2);
});
//set button_7 random play
connect(ui->pushButton_7,&QPushButton::clicked,this,[=](){
//judge error by songNum
if(songNum == 0)
{
QMessageBox::critical(this,"critical","no songs in songList");
return ;
}
system("killall -9 madplay");
//set randInt
int randInt = rand()%10+1;
for(int i = 0 ;inext;
}
if(current == head)
current = current->next;
buf = current->path;
onbuf = buf;
QString RealfileName = buf.mid(buf.lastIndexOf('/')+1);
ui->lineEdit->setText(RealfileName);
QString str = "madplay "+buf+" &";
const char* strChar = str.toLatin1();
system(strChar);
//show lyric
QString lyricfile = buf.mid(0,buf.lastIndexOf('.'));
lyricfile = lyricfile+".txt";
//open lyricfile and show lyric
QFile file(lyricfile);
file.open(QIODevice::ReadOnly);
QByteArray arr;
arr = file.readAll();
ui->textEdit->setText(arr);
//change label Image
int num2 = buf.lastIndexOf('/');
QString pictureName = buf.mid(num2,-1);
pictureName = pictureName.mid(0,pictureName.lastIndexOf('.'));
QString pictureName2 =":Image"+pictureName+ "1.jpg";
pictureName =":Image"+pictureName+ ".jpg";
//reset widget background
QPalette palette;
this->setAutoFillBackground(true);
QPixmap pixmap;
pixmap.load(pictureName);
palette.setBrush(QPalette::Background,QBrush(pixmap));
palette.setBrush(this->backgroundRole(),QBrush(pixmap.scaled(1118,615).scaled(1118,615,Qt::IgnoreAspectRatio,Qt::SmoothTransformation)));
QPainter p3(&pixmap);
p3.setCompositionMode(QPainter::CompositionMode_Source);
p3.drawPixmap(0, 0, pixmap);
p3.setCompositionMode(QPainter::CompositionMode_DestinationIn);
this->setPalette(palette);
//添加标签中的图片
QMovie *myMovie6 = new QMovie(pictureName2);
ui->label_4->setMovie(myMovie6);
ui->label_4->setScaledContents(true);
myMovie6->start();
//set opacity
QGraphicsOpacityEffect* opacityEffect2 = new QGraphicsOpacityEffect;
opacityEffect2->setOpacity(0);
ui->label_6->setGraphicsEffect(opacityEffect2);
});
void Widget::on_pushButton_5_clicked()
{
//select song
QFileDialog *fdlg = nullptr;
QString fileName = fdlg->getOpenFileName(this,"open File","/home/itcast/Desktop","(*.mp3)");
if(fileName == NULL)
return;
QString RealfileName = fileName.mid(fileName.lastIndexOf('/')+1);
buf = fileName;
//song add to List
const char* rfn = RealfileName.toLatin1();
const char* fn = fileName.toLatin1();
printf("%s %s\n",rfn,fn);
bool flag = AddNode(rfn,fn);
if(flag)
{
//add to QListWigetItem
QListWidgetItem *item = new QListWidgetItem(QIcon(":Image/To-like.png"),RealfileName);
ui->listWidget->addItem(item);
item->setTextAlignment(Qt::AlignLeft);
QString number = QString::number(songNum);
ui->lineEdit_2->setText(number);
}else{
QMessageBox::critical(this,"error","overlap exists");
}
}
void Widget::on_listWidget_itemClicked(QListWidgetItem *item)
{
buf = item->text();
buf = "/home/itcast/Desktop/"+buf;
//change current
while(current->path != buf)
{
current = current->next;
}
}
void Widget::on_pushButton_6_clicked()
{
if(songNum == 0)
{
QMessageBox::critical(this,"critical","no songs in songList");
return ;
}
QString RealfileName = buf.mid(buf.lastIndexOf('/')+1);
//foreach to find song to delete
for(int i=0 ;ilistWidget->item(i)->text() == RealfileName)
{
ui->listWidget->takeItem(i);
songNum--;
//updata songNum
QString number = QString::number(songNum);
ui->lineEdit_2->setText(number);
RemoveNode(RealfileName);
QString filename = "/home/itcast/Desktop/"+RealfileName;
if(onbuf == filename)
{
system("killall -9 madplay");
//reset picture in label_4
QMovie *myMovie = new QMovie(":Image/CD.png");
ui->label_4->setMovie(myMovie);
ui->label_4->setScaledContents(true);
myMovie->start();
ui->lineEdit->setText("");
ui->textEdit->setText("");
//reset widget background
QPalette palette;
this->setAutoFillBackground(true);
QPixmap pixmap;
pixmap.load(":Image/default.jpg");
palette.setBrush(QPalette::Background,QBrush(pixmap));
this->setPalette(palette);
//set opacity
QGraphicsOpacityEffect* opacityEffect2 = new QGraphicsOpacityEffect;
opacityEffect2->setOpacity(1);
ui->label_6->setGraphicsEffect(opacityEffect2);
}
}
}
}
smallwidget.h
#ifndef SMALLWIDGET_H
#define SMALLWIDGET_H
#include
#include
#include
namespace Ui {
class smallwidget;
}
class smallwidget : public QWidget
{
Q_OBJECT
public:
explicit smallwidget(QWidget *parent = nullptr);
~smallwidget();
private slots:
//void on_spinBox_valueChanged(const QString &arg1);
//void keyPressEvent( QKeyEvent *k );
private:
Ui::smallwidget *ui;
};
smallwidget.cpp
#include "smallwidget.h"
#include "ui_smallwidget.h"
smallwidget::smallwidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::smallwidget)
{
ui->setupUi(this);
//set spinBox color
QPalette pal(QColor(255,0,0));
pal.setColor(QPalette::Background,QColor(255,255,255));
ui->spinBox->setPalette(pal);
QPalette pa;
pa.setColor(QPalette::WindowText,Qt::red);
ui->spinBox->setPalette(pa);
//SpinBox connect QSlider
void(QSpinBox:: * spinSignal)(int) = &QSpinBox::valueChanged;
connect(ui->spinBox,spinSignal,ui->horizontalSlider,&QSlider::setValue);
connect(ui->horizontalSlider,&QSlider::valueChanged,ui->spinBox,&QSpinBox::setValue);
//set init value
ui->spinBox->setValue(99);
//click to change volumn
connect(ui->spinBox,spinSignal,this,[=](){
int val = ui->spinBox->value();
QString str = QString::number(val);
QString setVolume = "amixer set Master "+str+"%";
qDebug() <
//set font style
//set Size
ui->label_2->setFont(QFont( "Timers" , 12 , QFont::Bold));
ui->label_5->setFont(QFont( "Timers" , 12 , QFont::Bold));
ui->label_6->setFont(QFont( "Timers" , 12 , QFont::Bold));
ui->label->setFont(QFont( "Timers" , 12 , QFont::Bold));
ui->label_3->setFont(QFont( "Timers" , 12 , QFont::Bold));
//set Color
QPalette pa;
pa.setColor(QPalette::WindowText,Qt::red);
QPalette pa1;
pa1.setColor(QPalette::WindowText,Qt::yellow);
ui->label_2->setPalette(pa);
ui->label_5->setPalette(pa);
ui->label_3->setPalette(pa);
ui->label_6->setPalette(pa1);
ui->label->setPalette(pa);
ui->label_10->setPalette(pa);
//set button
QIcon icon(":/Image/pause2.png");
ui->pushButton->setIcon(icon);
ui->pushButton_2->setIcon(QIcon(":Image/play2.png"));
ui->pushButton_3->setIcon(QIcon(":Image/pre2.png"));
ui->pushButton_4->setIcon(QIcon(":Image/next2.png"));
ui->pushButton_5->setIcon(QIcon(":Image/addFromLocal.png"));
ui->pushButton_6->setIcon(QIcon(":Image/clear.png"));
ui->pushButton_7->setIcon(QIcon(":Image/random2.png"));
ui->pushButton->setFlat(true);
ui->pushButton_2->setFlat(true);
ui->pushButton_3->setFlat(true);
ui->pushButton_4->setFlat(true);
ui->pushButton_5->setFlat(true);
ui->pushButton_6->setFlat(true);
ui->pushButton_7->setFlat(true);
//add filename
QListWidgetItem *item = new QListWidgetItem(QIcon(":Image/music_list.png"),"music list:");
ui->listWidget->addItem(item);
item->setTextAlignment(Qt::AlignLeft);
ui->listWidget->setStyleSheet("background-color:transparent");
// QString path = "/home/itcast/Desktop/";
// QString str1 = "1.mp3";
// QListWidgetItem *item1 = new QListWidgetItem(str1);
// ui->listWidget->addItem(item1);
// item1->setTextAlignment(Qt::AlignLeft);
//添加标签中的图片
QMovie *myMovie = new QMovie(":Image/CD.png");
ui->label_4->setMovie(myMovie);
ui->label_4->setScaledContents(true);
myMovie->start();
//set transparency
QGraphicsOpacityEffect* opacityEffect2 = new QGraphicsOpacityEffect;
opacityEffect2->setOpacity(0.75);
ui->label_4->setGraphicsEffect(opacityEffect2);
//this->setAttribute(Qt::WA_TranslucentBackground);
QMovie *myMovie2 = new QMovie(":Image/music-playing.png");
ui->label_7->setMovie(myMovie2);
ui->label_7->setScaledContents(true);
myMovie2->start();
QMovie *myMovie3 = new QMovie(":Image/systemTrayIcon.png");
ui->label_8->setMovie(myMovie3);
ui->label_8->setScaledContents(true);
myMovie3->start();
//set lineEdit style
ui->lineEdit->setStyleSheet("background-color:transparent");
ui->lineEdit_2->setStyleSheet("background-color:transparent");
QPalette palette;
palette.setColor(QPalette::Text,Qt::red);
ui->lineEdit->setPalette(palette);
ui->lineEdit_2->setPalette(palette);
ui->lineEdit->setFont(QFont( "Timers" , 12 , QFont::Bold) );
ui->lineEdit_2->setFont(QFont( "Timers" , 12 , QFont::Bold));
//set textEdit style
ui->textEdit->setStyleSheet("background-color:transparent");
//set Widget image
this->setAutoFillBackground(true);
QPixmap pixmap;
pixmap.load(":Image/default.jpg");
palette.setBrush(QPalette::Background,QBrush(pixmap));
this->setPalette(palette);
//set listWidget font
ui->listWidget->setFont(QFont( "Timers" , 12 , QFont::Bold));
ui->listWidget->setPalette(palette);
//set textEdit font
ui->textEdit->setFont(QFont( "Timers" , 12 , QFont::Bold));
ui->textEdit->setTextColor(Qt::yellow);
//set textEdit center
ui->textEdit->setAlignment( Qt::AlignCenter );
刚开始拿到这个题目,只自学了两天Qt的我是懵的,但是能怎么办呢,不闷头干就没有课设成绩,那就干呗!
有问题多思考,多百度,总会有很大的提升,祝看这篇文章的网友们也能写出漂亮的图形化!!!
资源文件下载
alin还在成长的在路上
@alin 2020.6.15