The user interface is in Norwegian, or we write in QT

    In this article, I’ll talk about the development of user interfaces using the QT library, we will also take a short excursion into the history of Trolltech, and consider the development features on QT for Mac OS X with a small example.

    How it all began ...

    The Norwegian company Trolltech, was registered in 1994, by two Norwegian guys - Haavard North and Eirik Chambe-Eng. Initially, the company was called Quasar Technologies, but was later renamed Trolltech.

    On duty, Haavard was developing an object-oriented system for outputting information from an ultrasound machine. Being friends from the university, the guys seriously thought about their own startup. They dreamed of developing the best C ++ library for developing graphical interfaces for operating systems of the Unix class.

    In 1991, work marked the beginning of the QT era. In 1992, Eirik came up with one of the fundamental QT paradigms - the signal / slot system; Haavard immediately realized this idea. So in QT appeared its own C ++ code preprocessor - moc. And by the end of 1993, the core of rendering was ready, and the first version of QT was released next year!

    Develop - developed, but selling your product turned out to be problematic. There was no way out, it was necessary to look for customers, because it was worthless for the mighty Norwegian Vikings to sit on the neck of their wives. With the help of old friends, I managed to conclude a contract with the Norwegian company Metis, to develop a graphical interface on QT.

    Things went uphill, and in 1996 the European Space Agency also became Trolltech customers. QT itself gained support for Windows, and reached version 1.0! Trolltech began to grow, hired two more developers. The start was behind, the guys were standing firmly on their feet, and in front of them were good prospects.

    In 1997, Matthias Ettrich, the future father of KDE, made the decision to create a graphical shell for Linux on QT. With the release of the first version of KDE, it became clear that QT was becoming the de facto standard for developing graphical interfaces for Linux.

    QT 2 was released in 1999, QT 3 appeared in 2001. In June 2005, a historical event takes place - QT 4, a significantly improved version of the framework, is released. A GPL version of the library also appears under Windows, QT is becoming the standard for cross-platform development. By version 4, QT has become a full-fledged framework. It has everything you could wish for: I / O, containers and algorithms, RPC support with D-Bus, and much more.

    Much water has flowed since then ... Trolltech reached the size of 250 people, and opened offices around the world. In 2006, the company conducted an IPO. In January 2008, Trolltech announced that Nokia had made them an offer that they could not refuse. The deal amounted to 104 million euros, and Trolltech became the property of Nokia. Why did Nokia need trolls, you ask? Everything is very simple! QT is by no means the only Trolltech product. In 2004, the QTopia product was released. QTopia is nothing more than a platform for mobile devices based on Embeded Linux and QT. Apparently Nokia sees in QTopia a replacement for the current Maemo platform, and in the future even Symbian is possible.

    A distinctive feature of Trolltech is their business model. QT is available under two licenses: GPL and proprietary. Thus, either you buy a QT license and contribute to the Trollltech piggy bank and develop QT in rubles, or write open the source codes of your application, making a contribution to the development of Open Source and QT in particular.

    QT Overview

    So. Several frameworks are available for developing user interface applications on Mac OS X:
    • Cocoa This framework is the legacy of the powerful Next Computer that Apple has not failed to take advantage of. This framework was written in Objective-C and at the moment it is Apple's only strategic goal.
    • Carbon Apple also took care of the C ++ developers, so Carbon was created. However, the Carbon era is the era of Mac OS Classic, in Mac OS X it turns into a useless rudiment, which sooner or later, perhaps even in Snow Leopard, will be eliminated. Carbon existed in order to facilitate porting of third-party applications from Mac OS 9 to Mac OS X. In particular, if you want your application to be 64-bit and skillfully address up to 64 GB of virtual memory, then Carbon is not for you. Adobe already managed to get burned on it.
    • Third party frameworks. There are also a great many of them, for example WxWidgets, GTK, TCL, but in this article we will talk about QT :)

    Now QT is a fairly powerful tool that greatly expands the capabilities of C ++. Chasing Licenses: The commercial version of QT includes drivers for RDBS such as DB2, etc.

    It is worth mentioning that the interests of QT and Mac OS X intersected on the KHTML engine. KHTML is an HTML page rendering engine from the KDE project, which Apple once chose as the basis for its WebKit project, which later spawed Safari, Google Chrome and other smaller browsers :) Although the entire QT part was dropped from the project. Speaking of WebKit. At one time, Apple also considered Gecko as the main candidate for the role of the HTML engine. However, due to the prevalence of RPC technology - XPCOM in Gecko, KHTML was elected.

    QT applications have a native Aqua look, but somewhat outdated. This is because QT uses Carbon to render widgets, and the modern Mac OS X interface is mostly written in Cocoa. Nevertheless, Trolltech is constantly trying to improve the appearance of the poppy version and in the latest versions of QT there are opportunities to create native menus for Mac OS X ToolBar, as well as additional functions for interacting with the Dock. QT also has Growl support.

    Why exactly QT? If you need to write a small application with a user interface and you do not know Objective C, then QT is your choice, because learning Carbon does not make much sense or any visible prospects. If you have an application in C ++, and you need to port it to Mac OS X, then it’s time to choose QT, because C ++ has certain limitations when paired with Objective-C code. Moreover, the updated version of QT, which uses Cocoa to render widgets - just around the corner, the first snapshots are already available.

    From words to deeds

    First we need to download the GPL version of QT, this can be done from the Trolltech website

    We need to collect these source codes, and with the possibility of creating a Universal Binary (let me remind you must have Apple Xcode installed). Unpack QT into a directory convenient for you, start the terminal and execute the following line in the QT directory:
    ./configure -universal -sdk /Developer/SDKs/MacOSX10.4u.sdk -fast –static

    This command will configure QT for static assembly in the Universal Binary static libraries. Since UB applications built statically take up a lot of space, I recommend renaming the examples folder before building:
    mv -R examples examples_

    Now let's build the library using the make command. Depending on the power of your processor, the assembly may take from 20 minutes to several hours. After the assembly is completed, we execute the following commands in the terminal:
    make install
    PATH = $ PATH: /usr/local/Trolltech/QT-4.4.1/
    export PATH

    Please note that the path to the installed QT varies from version to version. Now everything is ready for further development.


    So, let's create the simplest UB application with one button and text label. To do this, create a HelloQT folder in our home directory. Launch QT Designer, it looks something like this: Create a Dialog form and drag the QPushButton and QLabel widgets onto it. Give them object names helloBt and helloLbl respectively. Name the form itself helloDlg and save the results of our manipulations in the project directory with the name HelloDlg. The end result will look like this: Now we have to write program code. Create the files HelloDlg.cpp, HelloDlg.h, main.cpp and HelloQT. pro (project QT file) in the project directory. In the HelloQT file. pro write the line data:

    SOURCES + = HelloDlg.cpp main.cpp
    HEADERS + = HelloDlg.h
    FORMS + = HelloDlg.ui
    CONFIG + = x86 ppc

    QT uses its own qmake makefile generator. The qmake directives and format can be explored in more detail with QT Asistant. Pay attention to the last line, here you explicitly indicate that you are interested in the Universal Binary application.

    Trolltech is trying to improve the integration of QT and the most popular integrated development environments, but things are not as good as we would like. You have two ways: you can compile the application directly from the command line, or you can create an Xcode project to continue writing the project directly from under this IDE. I prefer the second option, generating a new file from time to time. Run the following lines from the terminal:
    qmake -spec macx-xcode

    Now the most important part remains. Change the contents of the main.cpp file to the following lines:

    #include "HelloDlg.h"

    int main (int argc, char * argv [])
      QApplication appl (argc, argv);
      HelloDlg mainDlg; ();
      return appl.exec ();

    HelloDlg file. h should look like this:
    #ifndef _HELLODLG_H_
    #define _HELLODLG_H_


    #include "ui_HelloDlg.h"

    class HelloDlg: public QDialog, Ui :: helloDlg

      HelloDlg ();
      ~ HelloDlg ();
    private slots:
      void onHelloBtClicked (bool);


    Well and finally HelloDlg.cpp:
    #include "HelloDlg.h"

    HelloDlg :: HelloDlg (): QDialog (NULL)
      Ui :: helloDlg :: setupUi (this);

      connect (helloBt, SIGNAL (clicked (bool)), this, SLOT (onHelloBtClicked (bool)));

    HelloDlg :: ~ HelloDlg ()


    void HelloDlg :: onHelloBtClicked (bool)
      helloLbl-> setText (QString :: fromUtf8 ("Hello QT"));

    Let us dwell on this moment and make a theoretical excursion in order to clarify these lines a little to beginners in QT. To respond to events, i.e., for example pressing a button, QT uses its own signal / slot pradigm. Since C ++ does not have enough features, Trolltech decided to use its own moc preprocessor, which I mentioned earlier. The Q_OBJECT macro indicates that this class contains slots and requires special processing by the QT preprocessor (this process is called moc 'ing), each such class should directly or indirectly inherit from the QObject class. The private slots directive indicates that these methods of the class are private slots, i.e., the so-called event handlers. It should be added that many slots can be connected to one signal,

    The Ui :: helloDlg class, located in the Ui namespace, is a class that is created by a special preprocessor from the HelloDlg.ui form file (this process is called ui 'ing). UI file - an xml file that describes the form prepared in QT Designer and its elements.

    That's all, compiling the application. Either from Xcode by pressing cmd + B, or from the terminal with the command:

    The results can be seen in the screenshots:

    If you are interested in this topic, ask questions in the comments :)

    Also popular now: