Skip to content

First Qt …

I am kind of under press with a project, but I am really tired of it, so here is a little post that was waiting since two weeks ago to be posted.

I wanted my first post about Qt to be really positive, and in a matter of fact it probably will be, but I do have some details about Qt that got my hair to be in less shampoo demand than last year.
To start with, it was really simple to get started with windowing and such. Some basic stuff as displaying pictures was also trivial. Some tutorials in Qt’s online documentation and elsewhere are showing how to use QLabel as primary imaging widget, and some open sourced apps I found online, follow that example. My choice is rather a raw QPixmap in a QGraphicsScene viewed through QGraphicsView. I think it is more natural to use QGraphicsScene for that purpose than a label widget. QGraphicsScene should also be good choice for some other stuff later, like creating thumbnails, showing two images side by side and so on. For now on I haven’t got longer than basics like showing image, zooming in and out and such, which is already built-in in the framework.

Another really positive experience is QSettings class. Usually I use to write myself some basic configuration file, usually in linux-like key-value style conf file. I use to store values in std::map as pair and then try to convert a value to whatever it might be. QSettings does that already, and even have support for nesting keys. I have never before used .ini file as such in my own code, but with QSettings it was straightforward. It took maybe an hour to learn it and to add support for pretty much all basic window customizations (borders, background, size, scrollabars and so on). Thus my original idea that everything should be customizable through configuration file was straightforward to implement.
This also includess shortcuts; but It needed a bit of testing and try-and-error coding to get it right. Main problem being that I didn’t understand how things are parsed by QSettings engine. This is my shortcuts part of .ini file:

[shortcuts]
openFile    =   "Open File",         o
openDir     =   "Open Directory",    Ctrl+o
fullscreen  =   "Fullscren",         F11
slideshow   =   "Slideshow",         F5
zoomIn      =   "Zoom in",           +
zoomOut     =   "Zoom out",          -
next        =   "Next Image",        x
previous    =   "Previous Image",    z
resetView   =   "Reset View",        r
clear       =   "Close All Images",  c
centerOnScreen  =   "Center View On Screen",    h

What I have not got from beginning was the separator thing in ini file. I thought white spaces would suffice, but they don’t. However a comma after first string makes all work. Passing a modifier was also easy, no extra work needed. Just type kind of Alt+key or Ctrl+key and pass it as character sequence. Nice. In my .ini syntax first string after equality sign is used as Description for QActon, while second is obviously the shortcut itself.

This brings us to subject of QAction which turned to be a bit of a bugger. It seems to be quite central class in Qt’s event handling, and it is well documented, yet I had hard time until I got it right. Reason is not less old documentation and examples flourishing on the net! Seems like every version of Qt brings major differences in structure and breaks “old ways” of doing things. Thus looking at many docks, tutorials and mail lists is worthless, yet main documentation is not always clear as I wished it to be, nor is it very complete. Obviously best place to look are numerous examples included in Qt release.
I was implemented both actions that I needed, and dialog to customize them at same time. There is an old tutorial on Qt’s docs, which I tried to follow. It turned to be really outdated, and a lot of code was broken when compiling with qt4.6 version. Both code for actions stuff, tag widget and not less collections. With some documentation reading and googling around everything settled after few tries.

Obligatory screenshot showing an image in frameless window with transparent background:

No code snapshots are posted, but source is here if anyone is looking for a simple example how to display images with Qt.

Everything is not straightforward of course, and there are quite some things I don’t really liked about Qt so long, but I’ll leave this to another post.

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*