Home / PHP-Qt Calculator Example

Calculator Example

This movie demonstrates static casting in php-qt.
        function eventFilter($target, $event){
            if ($target == $this->display) {
                if ($event->type() == QEvent::MouseButtonPress
                    || $event->type() == QEvent::MouseButtonDblClick
                    || $event->type() == QEvent::MouseButtonRelease
                    || $event->type() == QEvent::ContextMenu) {


                    qstatic_cast($event, 'QMouseEvent');
                    $mouseEvent = $event;
                    if ($mouseEvent->buttons() & Qt::LeftButton) {
                        echo "**** BUTTON AND LEFT BUTTON TOGETHER AT LAST\n";


In Qt 4, each subclass of QObject has an eventFilter method. Any QWidget or QObject in Qt can filter events for any other QWidget. The only problem is, the eventFilter method can only accept the most generic type of event, the QEvent.

The QEvent object does not have special methods and variables that sub-classes do, like QMouseEvent. In strongly typed languages, when a specialized sub-class object is delivered to a method which requires a generic parent class in its argument list, the object "becomes" the generic parent. This problem is not common in PHP because there is no true *casting*. There is only explicit type coercing.

But, the object is still an implementation of the specialized sub-class, you just can't see the extra methods. Luckily, we can use methods of the QEvent object to determine exactly which type of event has been triggered.

After you discover what type of event object you received, you can convert to the specific event type if you want to use the special methods of that particular sub-class. This is done with a "static cast", using the qstatic_cast() method.

Get the Flash Player to see this video.

In this particular example, once we know that we have a special QMouseEvent, we cast it from it's parent QEvent, then we can ask the event which buttons were pressed.

//... from above example
                    qstatic_cast($event, 'QMouseEvent');
                    $mouseEvent = $event;

                    //buttons() is not a function of QEvent, only of QMouseEvent
                    if ($mouseEvent->buttons() & Qt::LeftButton) {
                        echo "**** BUTTON AND LEFT BUTTON TOGETHER AT LAST\n";

                        $newPalette = $this->palette();
                        //here, all we ar edoing is swapping the text and base colors with 
                        //each other... which you can see in the movie.
                        $newPalette->setColor(QPalette::Base,
                            $this->display->palette()->color(QPalette::Text));
                        $newPalette->setColor(QPalette::Text,
                            $this->display->palette()->color(QPalette::Base));
                        $this->display->setPalette($newPalette);
                    } else {
                        $this->display->setPalette($this->palette());
                    }