Subclassing Phonon::VideoPlayer for personalization
Written on February 4, 2009, by Milot Shala.

Well it’s a long story with me and my own media players!
I always want to build something from scratch just to learn new things as much as I can. Now I am building a personal Video Player for my own purposes. But I needed a specific behavior of a mouse double click and I found a solution based on knowledge from a book that I read before on Qt (Foundations of Qt Development).

In Qt you have signals which you can emit them on a specific behavior and then call a slot to treat that behavior for example we have a QPushButton clicked signal which is emitted when a QPushButton was clicked and it can be connected to a slot and then treat the clicked signal from that slot. In the other hand in Qt you can catch events which occur for example on mouse clicks, mouse double clicks, keyboard key presses and other events that will happen to a QWidget or other Qt class. And what I needed for my video player was the mouse double click and I needed it to make the video full screen when user double clicks on the video widget and I ended up subclassing (inheriting) the Phonon::VideoPlayer (which is a subclass of a QWidget and implements functions that represents the Phonon::VideoWidget and Phonon::MediaObject) and implementing a virtual method called mouseDoubleClickEvent shown below with its signature:

1
void mouseDoubleClickEvent(QMouseEvent *event);

In the first line you must include the headers that you need, which are phonon/videoplayer, phonon/videowidget and QMouseEvent which you will need because mouseDoubleClickEvent method takes a QMouseEvent pointer, then declare a virtual method that you are going to implement, below is shown the header file, later in the post you will see the actual implementation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <phonon/videoplayer.h>
#include <phonon/videowidget.h>
#include <qmouseEvent>
using namespace Phonon; // to simplify code
 
class YourPersonalVideoPlayerClass : public VideoPlayer
{
public:
      // we specify the optional parameter QWidget because if it's not specified
      // leave it 0.
     YourPersonalVideoPlayerClass(VideoCategory category, QWidget *parent = 0);
 
protected:
      void mouseDoubleClickEvent(QMouseEvent *event);
};

And that’s it for your class definition, now let’s take a look at the source file the actual implementation of our event, first we implement our constructor and explicitly call the base class’s constructor.

1
2
3
YourPersonalVideoPlayerClass::YourPersonalVideoPlayerClass(VideoCategory category, QWidget *parent) : VideoPlayer(category, parent)
{
}

then we catch the double click event, now for simplicity purposes I am going to detect which mouse button was double clicked and then show a simple QMessageBox.

1
2
3
4
5
6
7
8
9
10
11
12
13
void YourPersonalVideoPlayerClass::mouseDoubleClickEvent(QMouseEvent *event)
{
        switch(event->button())
        {
             case Qt::LeftButton:
                    QMessageBox::information(0, "Left Mouse Button Double-Clicked", "Left Mouse Button Double-Clicked");
                    break;
             case Qt::RightButton:
                    QMessageBox::information(0, "RightMouse Button Double-Clicked", "Right Mouse Button Double-Clicked");
                    break;
        }
 
}

And nothing for the end, because Qt simplified things and made our lives easier :D . Well I don’t know if I cleared things or made them messy but if you have better explanations to this post, please post it as a comment and I will correct my post.

  • http://codespartan.org/blog/2009/02/dooble-contribution/ Milot Shala’s Blog » Dooble Contribution

    [...] It was a bit challenging because implementing this feature I had to write my own implementation of QLabel and QDialog which I will post a tutorial how to subclass the two classes mentioned above and implement other features in detail, but the brief introduction of subclassing QWidgets you have here. [...]

  • http://flavio.tordini.org Flavio

    Just to let you know that your post has been useful to me while developing this app: http://flavio.tordini.org/minitube

  • http://www.wotlk-power-leveling.com wow power leveling

    A wonderful article…. In my life, I have never seen a man be so selfless in helping others around him to get along and get working. I feel good that there are people like you too. Thanks for this great weblog of yours. Its surely going to get me to go to higher places!

blog comments powered by Disqus

1,394 views

© Copyright Phalanx Blogosphere - Powered by Wordpress - Phalanx logo is designed by Leopard Cana