完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
|
相关推荐
3个回答
|
|
嵌入式课程设计总结(基于Linux的Qt版MP3播放器)
废话不多写直接上图上代码,其中有很多不规范的地方,希望大佬们指正。 1.课设题目 设计一个MP3播放器,要求:使用Linux下的madplay进行歌曲的播放,一共有三大项:
2.设计思路
4.布局
2. smallwidget.ui 5.控件的使用
#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 errorn"); 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 %sn",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" < 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); }); 随机播放键 //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 ;i current = current->next; } 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 %sn",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 ;i if(ui->listWidget->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); } } } }
#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() < system(strChar); }); } smallwidget::~smallwidget() { delete ui; } |
|
|
|
//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 ); 7.遇到的问题
刚开始拿到这个题目,只自学了两天Qt的我是懵的,但是能怎么办呢,不闷头干就没有课设成绩,那就干呗! 有问题多思考,多百度,总会有很大的提升,祝看这篇文章的网友们也能写出漂亮的图形化!!! |
|
|
|
只有小组成员才能发言,加入小组>>
663 浏览 0 评论
1083 浏览 1 评论
2456 浏览 5 评论
2784 浏览 9 评论
移植了freeRTOS到STMf103之后显示没有定义的原因?
2619 浏览 6 评论
使用eim外接fpga可是端口一点反应都没有有没有大哥指点一下啊
639浏览 9评论
636浏览 7评论
请教大神怎样去解决iMX6Q在linux3.0.35内核上做AP失败的问题呢
766浏览 6评论
610浏览 5评论
651浏览 5评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-10 16:05 , Processed in 1.118462 second(s), Total 84, Slave 65 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号