Self-taught QT simple music player based on QT5.5__QT

Source: Internet
Author: User

Your own music player, there are a lot of bugs, just as a try. Exchange Learning Group: 271251545

What the player implements: Add songs, Previous, paused, next, toggle songs, trays, taskbar to manipulate songs


Interface lazy, direct use of the designer interface, the song list is used by Qtablewidget to do

Player header File Code:

#ifndef player_h #define Player_h #include <QWidget> #include <QMouseEvent> #include <QPoint> #include <QMediaPlayer> #include <QFileDialog> #include <QSystemTrayIcon> #include <QEvent> #include <QMenu> #include <QAction> #include "about.h" #include <QWinThumbnailToolButton> #include < qwinthumbnailtoolbar> #include <QStandardItemModel> #include <QTableView> #include <QDirModel> # Include <QDir> #include <QStringListIterator> #include <QMediaPlaylist> #include <QTimer> name

Space Ui {class player;}
    Class Player:public Qwidget {q_object public:explicit player (Qwidget *parent = 0);
    ~player ();
    Qpoint drag;//defines a release point void Mousepressevent (Qmouseevent *);//Window Move event void Mousemoveevent (Qmouseevent *);//Window Move Event
    Qmediaplayer *myplayer;
Qsystemtrayicon *trayicon;
    Protected:qaction *quitaction;
    Qaction *aboutaction;
    Qaction *nextaction;Qaction *pauseaction;
    Qaction *backaction;
    Qwinthumbnailtoolbutton *backtoolbtn;
    Qwinthumbnailtoolbutton *pausetoolbtn;
    Qwinthumbnailtoolbutton *nexttoolbtn;
    Qwinthumbnailtoolbar *taskbar;
    Qstandarditemmodel *songlistmodel;
    Qstringlist path;
    Qmediaplaylist *playlist;
    Qtimer *timer;
    Qint64 pauseposition;


int currentindex;
    Private Slots:void on_closebtn_clicked ();
    void on_minibtn_clicked ();
    void on_pushbutton_2_clicked ();
    void on_pausebtn_clicked ();
    void on_volum_valuechanged (int value);
    void Tuopan (Qsystemtrayicon::activationreason reason);
    void Showabout ();
    void Myplayerslot (Qmodelindex index);
    void on_importbtn_clicked ();
    void SetPosition ();
    void on_horizontalslider_valuechanged (int value);
    void on_nextbtn_clicked ();
    void on_backbtn_clicked ();

void Mediachange (int);
Private:ui::p layer *ui;

};
 #endif//Player_h

Player.cpp File Code:

#include "player.h" #include "ui_player.h" #include <QDebug> #include <QtWinExtras> #include <QList> p
    Layer::p layer (Qwidget *parent): Qwidget (parent), UI (new UI::p layer) {ui->setupui (this); This->setwindowflags (Qt::framelesswindowhint | Qt::windowsystemmenuhint |
    Qt::windowminimizebuttonhint);//Hide title bar Setwindowicon (Qicon (":/new/prefix1/image/tray.jpg"));
    Myplayer=new Qmediaplayer ();
    Ui->volum->setvalue (50);
    Myplayer->setvolume (Ui->volum->value ());
    Playlist=new qmediaplaylist;
    Trayicon=new Qsystemtrayicon; Connect (trayicon,signal (activated (Qsystemtrayicon::activationreason)), this, SLOT (Tuopan (qsystemtrayicon::activ )//Ationreason tray icon and double-click Event Qpixmap pixmap (":/new/prefix1/image/background.jpg"); the picture below the current folder Qpalette palette
    = This->palette ();
    Palette.setbrush (Backgroundrole (), Qbrush (Pixmap)); Connect (playlist,signal (currentindexchanged (int)), This,slot (mediachange (int));

    SetPalette (palette);
    taskbar button Qmenu * menu=new qmenu (this);
    if (qtwin::iscompositionenabled ()) {Qtwin::enableblurbehindwindow (menu);
        else{Qtwin::d isableblurbehindwindow (menu);
    Menu->hide ();
    } taskbar=new Qwinthumbnailtoolbar ();
    Taskbar->setwindow (WindowHandle ());
    Backtoolbtn=new Qwinthumbnailtoolbutton (taskbar);
    Backtoolbtn->settooltip ("Last Song");
    Backtoolbtn->seticon (Qicon (":/new/prefix1/image/back.png"));
    Connect (backtoolbtn,signal (clicked ()), This,slot (on_backbtn_clicked ()));
    Pausetoolbtn=new Qwinthumbnailtoolbutton (taskbar);
    Connect (pausetoolbtn,signal (clicked ()), This,slot (on_pausebtn_clicked ()));
    Pausetoolbtn->settooltip ("suspend");
    Pausetoolbtn->seticon (Qicon (":/new/prefix1/image/pause.png"));
    Nexttoolbtn=new Qwinthumbnailtoolbutton (taskbar);
    Nexttoolbtn->settooltip ("next song");
    Nexttoolbtn->seticon (Qicon (":/new/prefix1/image/next.png")); Connect (nexttoolbtn,signAL (clicked ()), This,slot (on_nextbtn_clicked ()));
    Taskbar->addbutton (BACKTOOLBTN);
    Taskbar->addbutton (PAUSETOOLBTN);
    Taskbar->addbutton (NEXTTOOLBTN);
    Song list songlistmodel=new Qstandarditemmodel;   
    Songlistmodel->sethorizontalheaderitem (0,new qstandarditem qobject::tr ("song list"));
    Ui->songlist->setmodel (Songlistmodel);    
    Ui->songlist->setcolumnwidth (0,390);
    Ui->songlist->verticalheader ()->hide ();
    Ui->songlist->setedittriggers (qabstractitemview::noedittriggers);
    Ui->songlist->setselectionbehavior (qabstractitemview::selectrows);
    Connect (ui->songlist,signal (doubleclicked (Qmodelindex)), This,slot (Myplayerslot (Qmodelindex));
    Timer timer=new Qtimer;
Connect (timer,signal (timeout ()), This,slot (SetPosition ()));

} player::~player () {Delete UI;}
    void player::on_closebtn_clicked ()//Click the Close button to produce tray and tray menu {this->hide ();
    Ui->namelabel->stylesheet (); Trayicon->seticon (QICon (":/new/prefix1/image/tray.jpg"));
    Qmenu *traymenu=new Qmenu ();
    Aboutaction=new qaction (tr ("about"), this);
    Quitaction=new qaction (tr ("Exit"), this);
    Traymenu->addaction (aboutaction);
    Connect (aboutaction,signal (triggered (bool)), This,slot (Showabout ()));
    Traymenu->addaction (quitaction);
    Connect (quitaction,signal (triggered (bool)), This,slot (Close ()));
    Trayicon->setcontextmenu (Traymenu);
    Trayicon->settooltip ("tray icon");
    Trayicon->show (); Trayicon->showmessage ("Master", "I am here Oh", qsystemtrayicon::information,5000);//Be sure to put it here, otherwise the message will not show} void Player::on_ Minibtn_clicked () {this->showminimized ();} void Player::mousepressevent (Qmouseevent *e) {if (E->button () = = Qt::leftbutton)//button () is the button {Drag=e->globalpos ()-this->geometry (). TopLeft ()//mouse position minus left corner e-&
    Gt;accept ();
        } void Player::mousemoveevent (Qmouseevent *e) {if (e->buttons () &qt::leftbutton)//buttons () returns the button state { Move (e->Globalpos ()-drag);
    E->accept (); }//Select song void Player::on_pushbutton_2_clicked () {QString path=qfiledialog::getopenfilename (this,tr ("Select a song"), "/", "
    *.mp3 ");
    Qfileinfo FileInfo;
    Fileinfo=qfileinfo (path);
    QString Filename=fileinfo.basename ();
    Ui->namelabel->settext (FileName);
    Myplayer->setmedia (Qurl::fromlocalfile (path));

Myplayer->play ();
        //pause button void player::on_pausebtn_clicked () {switch (Myplayer->state ()) {case Qmediaplayer::P layingstate:
        Timer->stop ();
        Currentindex=playlist->currentindex ();
        Pauseposition=myplayer->position ();
        Myplayer->pause ();
    Break
        Case Qmediaplayer::P ausedstate:myplayer->setmedia (Playlist->media (currentindex));
        Myplayer->setposition (pauseposition);
        Timer->start (1000);
        Myplayer->play ();
    Break
    Default:break;
}//volume control void player::on_volum_valuechanged (int value){Myplayer->setvolume (value);} void Player::tuopan (Qsystemtrayicon::activationreason reason)/Double-click tray icon, restore program Normal interface {switch (reason) {case Qsyste
        Mtrayicon::D oubleclick:this->shownormal ();
    Break
    Default:break;
    }//Show about this program dialog box void Player::showabout () {about showabout;
    Showabout.setmodal (FALSE);
    Showabout.setwindowtitle ("About this procedure");
Showabout.exec ();
    ///import multiple songs void player::on_importbtn_clicked () {path=qfiledialog::getopenfilenames (This, "Select Song", "/", "(*.mp3)");
    int i=0;
    QString addsongs;
    Qlistiterator<qstring> mylist (path);
        while (Mylist.hasnext ()) {addsongs=mylist.next ();
        Qfileinfo FileInfo;
        Fileinfo=qfileinfo (addsongs);
        QString Filename=fileinfo.filename ();
        Songlistmodel->setitem (i,0,new Qstandarditem (fileName));
        Playlist->addmedia (Qurl::fromlocalfile (addsongs));
    i++; } playlist->setplaybackmode (Qmediaplaylist::loop);

Myplayer->setplaylist (playList);
    //Double-click play void Player::myplayerslot (Qmodelindex index) {timer->start (1000);
    int N=index.row ();

Playlist->setcurrentindex (n);
Myplayer->setmedia (Playlist->currentmedia ()); This sentence can not be added, but the wrong myplayer->play ();
    //Set Play progress bar void Player::setposition () {Qint64 position=myplayer->position ();
    Ui->horizontalslider->setmaximum (Myplayer->duration ());
Ui->horizontalslider->setsliderposition (position); } void player::on_horizontalslider_valuechanged (int value)/Fast forward and rewind function {Qint64 currentposition=myplayer->position ()
    ; if (Qabs (value-currentposition) >2000&& (Ui->horizontalslider->value ()!=0)) {Myplayer->stop (
        );
        Timer->stop ();
        Myplayer->setposition (value);
        Timer->start ();
    Myplayer->play (); } void Player::on_nextbtn_clicked ()//play the next one {if (Playlist->mediacount ()-playlist->currentindex ()!=1)//Here's the note ,Like our normal numbers, Mediacount is a//myplayer->setmedia (Playlist->media (Playlist->nextindex ()) that counts from 1.
        Playlist->setcurrentindex (Playlist->nextindex ());
    Myplayer->play ();
       else {playlist->setcurrentindex (0);
        Myplayer->setmedia (Playlist->media (0));
    Myplayer->play (); } void Player::on_backbtn_clicked ()//Play Previous {if (Playlist->currentindex ()!=0)///Note here, as we normally count,
        Mediacount is the//myplayer->setmedia (Playlist->media (Playlist->previousindex ()) that is counted starting from 1.
        Playlist->setcurrentindex (Playlist->previousindex ());
    Myplayer->play ();
        else {Playlist->setcurrentindex (Playlist->mediacount ()-1);
        Myplayer->setmedia (Playlist->media (Playlist->mediacount ()-1));
    Myplayer->play ();

 } void Player::mediachange (int i) {Ui->namelabel->settext (path.at (i));}

About the dialog box header file:

#ifndef about_h
#define ABOUT_H

#include <QWidget>
#include <QDialog>

class about: Public Qdialog
{
    q_object public
:
    Explicit About (Qwidget *parent = 0);

Signals: Public

slots:
};

#endif//About_h

About the dialog box Source:

#include "about.h"
#include <QLabel>
#include <QGridLayout>

about::about (Qwidget * Parent): Qdialog (parent)
{
    Qlabel *aboutlabel=new Qlabel;
    Aboutlabel->settext ("Small black version of the QT Music Player");
    Qhboxlayout *mainlayout=new Qhboxlayout (this);
    Mainlayout->addwidget (Aboutlabel,qt::aligncenter);


}

Main function:

#include "player.h"
#include <QApplication>

int main (int argc, char *argv[])
{
    Qapplication A (argc, argv);
    Player W;
    W.show ();

    return a.exec ();
}

Happy because of sharing. Qt&c++ Programming Enthusiasts: 271251545

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.