Leveraging Qt For Smooth Transition Effects
I hate the days close to exams. I am pretty annoyed with learning by then, and the times I decide to call “creative breaks” get longer and longer. Yesterday, I was looking for something that might be fun, touching an area I haven’t been touching before. Now I don’t consider myself a graphics expert. As such, I am a total n00b when it comes to nice-looking effects.
However what has always bothered me is that despite virtually anyone having a bare minimum of OpenGL support nowadays, we still don’t have OpenGL-accelerated blending or transitioning effects in KDE. Back when I was at Trolltech, Matthias voiced the same opinion. And so I decided to program something cool “when I would find some spare time”. Now, as we all know, there is no such thing as spare time. It’s merely a matter of feeling The Itch or, like in my case, feel like you need to do something funny for a change.
I think our users deserve some eye candy, so I hope a lot of people who write photo albums, etc, watch this. So I wrote a demo that leverages Qt’s OpenGL widget, good old QTimer and the rather new QTimeLine class to transiate smoothly between two images. I’d love to see something like this in Gwenview or KPhotoAlbum for instance.
(I also feel that I owe a nice QtOpenGL example to the readers of my Qt 4 book, which I am linking here purely for commercial reasons, but that’s a different story).
Anyway, highlights of the demo I came up with include:
- extra smooth transition using EaseInOutCurve
- toggling between fullscreen mode and window using Alt+Tab (although implemented quick and dirty)
As you dive into the source code, you will realize that there is nothing OpenGL specific about it. All it does is using plain Qt APIs. In fact, the source code allows for an easy replacement of QGLWidget with QWidget. After a recompile, the blending effect still works, although by far not as smooth on X11. This is because if you use QGLWidget, it will redirect all your Painter calls to a special paint device using a special paint engine, which translates your QPainter calls into OpenGL commands. You can, but don’t have to bother with native OpenGL calls at all.
I wish it was possible to detect a software renderer in a generic way, but I’m afraid that will only be possible with GLX specific code. That way the code would simply not use a transition on non OpenGL-accelerated hardware. Hints welcome in the comments, please prove me wrong.