The Road to KDE Devland – step 4

logo

The end of summer

Whew, long time no see! After the summer vacation I’ve had my hands full, so I never found the motivation to sit down and write this. Sorry about the delay.

This step marks the end of summer and will show you what I’ve achieved in these two months. But first something about Qt.

The start of Qt programming

To start off, I need to say this:

Qt is easy.

When I was a child, I made some GUI applications in Visual Basic. It was very easy to get started with – you place some widgets on a form, double click on one (e.g. a pushbutton) and write some code to be run (e.g. when the pushbutton is pressed). Simple.

Even with my limited knowledge of Visual Basic, I could do pretty much. The code was usually awful – I preferred to store values in (invisible) textboxes instead of using variables, just to mention one thing – but they actually worked.

Now, I would argue that Qt is harder to get started with, probably because it uses C++ (yes I know that Qt has a lot of bindings for other languages). It’s easy to lay out widgets in Qt Designer, but it’s far from obvious what to do afterwards. The workflow is probably clearer if you use Qt Creator, but I haven’t tried it yet.

To my aid I had my books, and I soon found that I was able to do a lot in Qt that I didn’t know how to implement in Visual Basic. The Qt layout system makes it easy to lay out widgets and takes care of the position and size of widgets when you resize the window. To make an application with menubar and toolbars, you derive from QMainWindow and add a few objects and functions.

Derive. That’s something you’ll do a lot in Qt. An excellent example of object-oriented programming.

If you want an object A that have the same properties (talking about general properties, not Qt specific) as object B, but further specialized, you can derive A from B. For example, I want a cat that can play Go. Most cats can’t do that, right? A normal Cat is not enough. But I don’t need to create a new Cat from scratch, I can make the new class inherit from Cat (i.e., it is derived from Cat):

class  GoCat : public Cat
{
    Q_OBJECT

public:
    GoCat(QWidget *parent = 0);

private slots:
    void playGo();
};

This is just standard C++. Q_OBJECT is included because we want to make use of Qt’s meta-object system, including signals and slots (read more here).

In a similar fashion, we can build GUI applications by using the classes Qt provides. A Main Window with menubar, toolbar and system tray? No problem, QMainWindow takes care of it. If we want to make a custom widget that consist of two Qt widgets, for example a QSlider that’s connected to QLCDNumber like in this tutorial, we simply derive from QWidget.

MyWidget(QWidget *parent = 0); is a typical constructor declaration for derived classes. We then pass the parent pointer to the base class (the one we derive from) in the constructor:

MyWidget::MyWidget(QWidget *parent)
     : QWidget(parent)
[...]

In this example  (taken from tutorial) MyWidget is derived from QWidget, as you can see.

(Qt’s parent-child mechanism is out of scope for this article. I touched upon the subject in step 2, when I wrote about pointers in Qt).

Result

As promised I’ll show you the two Main Windows (derived from QMainWindow, of course) that I’ve done by following my two Qt books.

In The Book of Qt 4: The Art of Building Qt Applications you create a simple text editor in chapter 4. The GUI is done in Qt Designer and the final application looks like this:

Cute Editor

CuteEditor

I liked this application. It’s simple – most of the functionality is already in QTextEdit. You need to setup the actions in the toolbar and menubar and implement a few functions functions, such as loading and saving files. Oh, and don’t forget the status bar. That’s pretty much it.

This chapter gave me a good understanding of how to create similar small applications. However, I found the part about the dock window (“Templates” in screenshot) somewhat vague and incomplete. The code wouldn’t compile for me at first – I need to look into it and see if it’s a mistake from my part.

The application in C++ GUI Programming with Qt 4 is more complex. You build a spreadsheet, completely from a text editor (chapter 3 and 4). Well, for most parts at least.

Spreadsheet

Spreadsheet

The reason I said “for most parts” is because you create some of the dialogs in Qt Designer in an earlier chapter. It’s very nice that you get to reuse them to create your Spreadsheet application and learn how dialogs work. Completing the application was also a big motivation booster – “woah, I could actually make this application”.

That’s unfortunately also the bad thing about this example – to me, it was a bit too complex and it was easy to get lost in details. Yet it’s listed under “Basic Qt”, which I find somewhat amusing.

Another thing I didn’t like was that I couldn’t compile it until I was finished. I prefer to take it in smaller steps, to be able to see what I’ve accomplished after a while. In hindsight, I should have copied example files from chapter 4 (included on CD) to test the application.

Overall I get the feeling that C++ GUI Programming with Qt 4 assumes the reader has more prior knowledge in C++ than The Book of Qt 4: The Art of Building Qt Applications. In my opinion Daniel Molkentin, the author of the latter book, gives a more expanded explanation on some topics, such as memory management (stack vs heap) and how to use Ui files created in Qt Designer. On the other hand, I also appreciate the insight Jasmin Blanchette and Mark Summerfield (authors of C++ GUI Programming with Qt 4) provides and their elegant code.

To summarize, I don’t regret getting both books – so far, I think they work pretty well as complements.

Johan Thelin has been kind enough to send me a review copy of his book, Foundations of Qt Development - thanks Johan! It’s still at my parents’ place but I’ll pick it up the next time I visit them. I’m looking forward to seeing what this book has to offer.

Final words

Everything above (mainly referring to “The start of Qt programming”) is my interpretation. If you find any errors in the text, please don’t hesitate to correct me.

Next time I’ll show you the applications I recently started working on. Simple small applications, very similar to the ones I showed above (but rewritten from scratch). I do, however, have plans to eventually turn one into a plasmoid later.

My lectures have started again, so I’ll have even less time to learn about KDE Development. That also means less frequent steps, which in unfortunate. I’ll try to keep you updated as often as my time (and motivation) allows.

About these ads

6 Responses to “The Road to KDE Devland – step 4”

  1. mutlu Says:

    I really enjoy reading your Road to KDE Devland series. Please continue it. I am sure it will motivate some users with interest in programming to take their first steps.

  2. Hans Chen (mogger) 's status on Tuesday, 15-Sep-09 09:25:17 UTC - Identi.ca Says:

    […] The Road to KDE Devland – step 4 « Who Says Penguins Can't Fly? a few seconds ago from web […]

  3. Hans Says:

    Thank you mutlu. =)

  4. Sergio Says:

    Really nice :D Keep up the good work!

  5. Hans Says:

    Thanks Sergio!

  6. Plaristote Says:

    I’m following you more closely that I thought !
    It’s just *incredible* how Qt is easy to use ! I just needed QtCreator and the Qt documentation to do some things I was thinking I would never be able to do !

    Stay hot for me KDE we are coming !
    Here I’m specially waiting for all the cmake stuff and the use of KDE libs instead of the Qt ones.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 38 other followers

%d bloggers like this: