📜 ⬆️ ⬇️

User interface in Norwegian, or write to QT

In this article, I will talk about the development of user interfaces using the QT library, we will also make a brief excursion into the history of Trolltech, and consider the features of QT development under Mac OS X using a small example.



How it all began ...


The Norwegian company Trolltech was registered in 1994 by two Norwegian guys, Haavard North and Eirik Chambé-Eng. Initially, the company was called Quasar Technologies, but was later renamed Trolltech.
')
On duty, Haavard was developing an object-oriented system for displaying information from an ultrasound machine. Being friends from university, the guys really thought about their own startup. They dreamed of developing the best C ++ library for developing graphical interfaces for an Unix-class operating system.

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

Develop - developed, but to sell your product turned out to be problematic. There was no way out, it was necessary to look for clients, because it is not worth the powerful Norse vikings to sit on the neck of their wives. With the help of old acquaintances, we managed to conclude a contract with the Norwegian company Metis to develop a graphical user interface on QT.

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

In 1997, Mathias Ettrich, the future father of KDE, makes 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 is becoming the de facto standard GUI development for Linux.

In 1999, QT 2 comes out, in 2001, QT 3 appears. In June 2005, a historical event takes place - QT 4 is released, a significantly improved version of the framework. Also under Windows, the GPL version of the library appears, QT is becoming the standard for cross-platform development. To version 4, QT has become a full-fledged framework. It has everything you could want: I / O facilities, containers and algorithms, RPC support using D-Bus, and more.

A lot of water has flowed since then ... Trolltech has reached the size of 250 people, and has opened offices around the world. In 2006, the company held an IPO. In January 2008, Trolltech announced that Nokia had made them an offer they could not refuse. The amount of the transaction was 104 million euros, and Trolltech became the property of Nokia. Why Nokia needed the trolls you ask. Everything is very simple! QT is not 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, perhaps even Symbian.

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 with a ruble, or write to open the source codes of your application, contributing to the development of Open Source and QT in particular.

A quick tour of QT features


So. There are several frameworks available for developing applications with a user interface in Mac OS X:

Now QT is a fairly powerful tool that greatly enhances the capabilities of C ++. After licensing: the commercial version of QT includes drivers for DB2 RDBS, etc.

It is worth mentioning that the interests of QT and Mac OS X crossed on the KHTML engine. KHTML is the HTML rendering engine for the KDE project, which at one time chose Apple as the basis for its WebKit project, which eventually spawned Safari, Google Chrome and other smaller browsers :) Although the entire QT part was thrown out of 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-XPCOM technology in Gecko, KHTML was elected.

QT applications have native Aqua look, but a bit outdated. This is due to the fact that QT uses Carbon to draw widgets, and the modern Mac OS X interface is written mostly on Cocoa. Nevertheless, Trolltech is constantly trying to improve the look and feel of the Mac version, and in the latest QT versions there are opportunities to create native Mac OS X ToolBar menus, as well as additional functions for interacting with the Dock. Also in QT is Growl support.

Why exactly QT? If you need to write a small application with a user interface and you don’t know Objective C, then QT is your choice, because learning Carbon has no particular meaning or visible perspectives. If you have an application in C ++, and you need to port it to Mac OS X, then at the time of time, choose QT, because C ++ has certain limitations when pairing with Objective C code. Moreover, the updated version of QT, which uses Cocoa to render widgets, is just around the corner, the first snapshots are already available.

From words to deeds


First we need to download the GPL version of QT, it can be done from the Trolltech website http://trolltech.com/developer/downloads/qt/mac

We need to collect these source codes, and with the possibility of creating Universal Binary (let me remind you that you should have Apple XCode installed). Unpack QT in a convenient directory 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 configures QT for a static assembly in the Universal Binary static libraries. Since UB applications collected statically take up quite a lot of space, I recommend renaming the examples folder before building:
mv -R examples examples_

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

Note that the path to the installed QT changes from version to version. Now everything is ready for further development.

HelloQT


So, we will create the simplest UB application with one button and a text label. To do this in our home directory, create a folder HelloQT. Run QT Designer, it looks like this:


Create a Dialog form and move the QPushButton and QLabel widgets onto it. Give them the object name helloBt and helloLbl, respectively. Name the form name 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 the program code. Create the HelloDlg.cpp, HelloDlg.h, main.cpp, and HelloQT files. pro (project QT file) in the project directory.

In the HelloQT file. pro write these lines:
SOURCES + = HelloDlg.cpp main.cpp
HEADERS + = HelloDlg.h
FORMS + = HelloDlg.ui
CONFIG + = x86 ppc

QT uses the qmake's own make generator. The qmake directives and format can be further explored with QT Asistant. Pay attention to the last line, here you clearly 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 still 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, from time to time generating a new file. Run the following lines from the terminal:
qmake -spec macx-xcode HelloQT.pro

Now the most important part remains. Change the contents of the main.cpp file to the following lines:
#include <QApplication>

#include "HelloDlg.h"

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

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

#include <QDialog>

#include "ui_HelloDlg.h"

class HelloDlg: public QDialog, Ui :: helloDlg
{
Q_OBJECT

public :
HelloDlg ();
~ HelloDlg ();

private slots:
void onHelloBtClicked ( bool );
};

#endif

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 point and make a theoretical excursion in order to slightly explain these lines to newcomers in QT. To react to events, i.e. for example button presses, 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 must directly or indirectly inherit from the QObject class. The private slots directive indicates that these class methods are closed slots, i.e., so-called event handlers. It should be added that you can connect multiple slots to a single signal, just as you can connect any number of signals to each slot.

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

That's it, compile the application. Either from XCode by clicking on cmd + B, or from the terminal with the command:
qmake
make

The results can be seen in the screenshots:



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

Source: https://habr.com/ru/post/39209/


All Articles