QT5 Official Demo Set 35--music Player (using Winextras module)

Source: Internet
Author: User

All articles in this series can be viewed here http://blog.csdn.net/cloud_castle/article/category/2123873

Then Qt5 the official demo set 34--concentric Circles Example

Look at the headline you might think we're going to talk today about the multimedia module in Qt, indeed, the demo is based on this module to implement a music player, but today we are more focused on the use of the Winextras module in the demo.

As the name can guess, this module can be used to provide us with some additional extensions on the Windows platform, such as the DWM (Desktop Window Manager) effect, Aero peek,taskbar,jump Lists,thumbnail toolbar and so on, QT has encapsulated relevant APIs for us to make them easier to use. If you are unfamiliar with these nouns, you can visit the QT website for more details: http://doc.qt.io/qt-5/qtwinextras-overview.html. Or, I'll give you a couple of chestnuts around:

I wonder if everyone has noticed that when we build with Qt Creator, the progress status on the taskbar icon?

Or when we put the left mouse button on the QQ music taskbar icon, the previous song, pause, the next song these preview window buttons:

Or, right-click on the latest listening list that appears on QQ music:

And so on and so on, I will not list, standing in the GUI perspective, these things are not dispensable

The details determine success or failure, and the user is always able to reap surprises and touches in small details.

So, let's see how we can use these intimate gadgets in Qt.

Remember to add in the Pro file

[CPP]View PlainCopy
    1. QT + = Winextras

Then look at Main.cpp, which has a useful helper function for the associated file format:

[CPP]View PlainCopy
  1. #include "Musicplayer.h"
  2. #include <QApplication>
  3. #include <QFileInfo>
  4. #include <QSettings>
  5. #include <QIcon>
  6. #include <QDir>
  7. //! [0]
  8. static void Associatefiletypes (const qstringlist &filetypes) //This is a helper function used to associate a file format with this program
  9. {
  10. QString displayName = Qguiapplication::applicationdisplayname ();
  11. QString FilePath = Qcoreapplication::applicationfilepath ();
  12. QString filename = qfileinfo (filePath). FileName ();
  13. Qsettings settings ("hkey_current_user\\software\\classes\\applications\\" + fileName, Qsettings::nativeformat)  ;
  14. Settings.setvalue ("Friendlyappname", displayName);
  15. Settings.begingroup ("supportedtypes");
  16. foreach (const qstring& fileType, FileTypes)
  17. Settings.setvalue (FileType, QString ());
  18. Settings.endgroup ();
  19. Settings.begingroup ("shell");
  20. Settings.begingroup ("open");
  21. Settings.setvalue ("Friendlyappname", displayName);
  22. Settings.begingroup ("Command");
  23. Settings.setvalue (".", Qchar (' "') + qdir::tonativeseparators (FilePath) + QString (" \"\"%1\ "));
  24. }
  25. //! [0]
  26. int main (int argc, char *argv[])
  27. {
  28. Qapplication app (argc, argv);
  29. App.setapplicationname ("Musicplayer");
  30. App.setorganizationname ("Qtwinextras");
  31. App.setorganizationdomain ("qt-project.org");
  32. App.setapplicationdisplayname ("Qtwinextras Music Player");
  33. App.setwindowicon (Qicon (":/logo.png"));
  34. Associatefiletypes (Qstringlist (". mp3")); //How helper functions are used
  35. Musicplayer player;
  36. const Qstringlist arguments = Qcoreapplication::arguments ();
  37. if (arguments.size () > 1) //If open parameter contains file name
  38. Player.playfile (arguments.at (1)); //Start playing the first song
  39. Player.resize (300, 60);
  40. Player.show ();
  41. return app.exec ();
  42. }

The Volumebutton class inherits from the Qtoolbutton and is set to its own icon using the standard volume image in Qt.

[CPP]View PlainCopy
    1. SetIcon (Style ()->standardicon (Qstyle::sp_mediavolume));

It also provides a pop-up menu that adjusts the volume, and determines its own display status based on the DWM's blending state, and the code is easy to understand and does not post.

In the Musicplayer class, first remember

[CPP]View PlainCopy
    1. #include <QtWinExtras>

We then create the jump Lists by using the following function:

[CPP]View PlainCopy
    1. void Musicplayer::createjumplist ()
    2. {
    3. Qwinjumplist jumplist;
    4. Jumplist.recent ()->setvisible (true);
    5. }

Taskbar is created similar to progress, and we associate it with a progress bar on the window:

[CPP]View PlainCopy
  1. void Musicplayer::createtaskbar ()
  2. {
  3. Taskbarbutton = New Qwintaskbarbutton (this);
  4. Taskbarbutton->setwindow (WindowHandle ()); //using a window handle as a parameter
  5. Taskbarprogress = Taskbarbutton->progress ();
  6. Connect (Positionslider, SIGNAL (valuechanged (int)), taskbarprogress, SLOT (setValue (int)));
  7. Connect (Positionslider, SIGNAL (rangechanged (int, int)), Taskbarprogress, SLOT (setRange (int,int)));
  8. Connect (&mediaplayer, SIGNAL (statechanged (qmediaplayer::state)), this , SLOT (Updatetaskbar ()));
  9. }


To create a preview window button, you first need to create a qwinthumbnailtoolbar and then add a button to it:

[CPP]View PlainCopy
  1. void Musicplayer::createthumbnailtoolbar ()
  2. {
  3. Thumbnailtoolbar = New Qwinthumbnailtoolbar (this);
  4. Thumbnailtoolbar->setwindow (WindowHandle ());
  5. Playtoolbutton = new Qwinthumbnailtoolbutton (Thumbnailtoolbar);
  6. Playtoolbutton->setenabled (false);
  7. Playtoolbutton->settooltip (tr ("Play"));
  8. Playtoolbutton->seticon (Style ()->standardicon (Qstyle::sp_mediaplay));
  9. Connect (Playtoolbutton, SIGNAL (clicked ()), this , SLOT (Toggleplayback ()));
  10. Forwardtoolbutton = new Qwinthumbnailtoolbutton (Thumbnailtoolbar);
  11. Forwardtoolbutton->setenabled (false);
  12. Forwardtoolbutton->settooltip (tr ("Fast Forward"));
  13. Forwardtoolbutton->seticon (Style ()->standardicon (Qstyle::sp_mediaseekforward));
  14. Connect (Forwardtoolbutton, SIGNAL (clicked ()), this , SLOT (Seekforward ()));
  15. Backwardtoolbutton = new Qwinthumbnailtoolbutton (Thumbnailtoolbar);
  16. Backwardtoolbutton->setenabled (false);
  17. Backwardtoolbutton->settooltip (tr ("Rewind"));
  18. Backwardtoolbutton->seticon (Style ()->standardicon (Qstyle::sp_mediaseekbackward));
  19. Connect (Backwardtoolbutton, SIGNAL (clicked ()), this , SLOT (Seekbackward ()));
  20. Thumbnailtoolbar->addbutton (Backwardtoolbutton);
  21. Thumbnailtoolbar->addbutton (Playtoolbutton);
  22. Thumbnailtoolbar->addbutton (Forwardtoolbutton);
  23. Connect (&mediaplayer, SIGNAL (positionchanged (Qint64)), this , SLOT (Updatethumbnailtoolbar ()));
  24. Connect (&mediaplayer, SIGNAL (durationchanged (Qint64)), this , SLOT (Updatethumbnailtoolbar ()));
  25. Connect (&mediaplayer, SIGNAL (statechanged (qmediaplayer::state)), this , SLOT (Updatethumbnailtoolbar ()));
  26. }

Finally, let's see how the program works:


Under Win7 When we pause the playback, the taskbar appears with a pause style icon, while in Win8 the green progress bar turns yellow:

http://blog.csdn.net/cloud_castle/article/details/43672509

QT5 Official Demo Set 35--music Player (using Winextras module)

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.