Part I: Basic Qt – Ch01. Hello Qt 본문

Programming/C++/Qt

Part I: Basic Qt – Ch01. Hello Qt

halatha 2008. 7. 31. 11:38

Hello Qt

Let's start with a very simple Qt program. We will first study it line by line, then we will see how to compile and run it.

매우 간단한 Qt 프로그램을 시작하자. 우리는 우선 행별로 공부하고, 다음에 어떻게 컴파일하고 실행하는지 것이다.

1 #include <QApplication>
2 #include <QLabel>
3 int main(int argc, char *argv[])
4 {
5     QApplication app(argc, argv);
6     QLabel *label = new QLabel("Hello Qt!");
7     label->show();
8     return app.exec();
9 }

Lines 1 and 2 include the definitions of the QApplication and QLabel classes. For every Qt class, there is a header file with the same name (and capitalization) as the class that contains the class's definition.

1 2행은QApplication QLabel클래스들의 정의들을 포함한다. 모든 Qt 클래스에 대해, 같은 이름(대문자 사용)을 클래스 정의를 포함하는 클래스로서 갖는 헤더 파일이 있다.

Line 5 creates a QApplication object to manage application-wide resources. The QApplication constructor requires argc and argv because Qt supports a few command-line arguments of its own.

5행은 어플리케이션 전반의 자원을 관리하기 위한 QApplication 개체를 생성한다. QApplication 구조체는 Qt 자신의 가지 명령행 인자를 지원하기 때문에 argc qrgv 요구한다.

Line 6 creates a QLabel widget that displays "Hello Qt!". In Qt and Unix terminology, a widget is a visual element in a user interface. The term stems from "window gadget" and is the equivalent of both "control" and "container" in Windows terminology. Buttons, menus, scroll bars, and frames are all examples of widgets. Widgets can contain other widgets; for example, an application window is usually a widget that contains a QMenuBar, a few QToolBars, a QStatusBar, and some other widgets. Most applications use a QMainWindow or a QDialog as the application window, but Qt is so flexible that any widget can be a window. In this example, the QLabel widget is the application window.

6행은 "Hello Qt!" 출력하는 QLabel 위젯을 생성한다. Qt 유닉스 용어에서, 위젯은 유저 인터페이스에 있는 시각적 요소이다. 말은 "window gadget"에서 왔고, 윈도우즈 용어의 "control" "container" 동일하다. 버튼, 메뉴, 스크롤 바와 프래임들은 위젯의 예들이다. 위젯은 다른 위젯을 포함할 있다; 예를 들어, 어플리케이션 윈도우는 보통 QMenuBar, 가지 QToolBar, QStatusBar 다른 위젯들을 포함하는 위젯이다. 대부분의 어플리케이션들은 어플리케이션 윈도우로서 QMainWindow QDialog 사용하지만, Qt 매우 유연해서 어떤 위젯이라도 윈도우가 있다. 예제에서, QLabel 위젯은 어플리케이션 윈도우이다.

Line 7 makes the label visible. Widgets are always created hidden, so that we can customize them before showing them, thereby avoiding flicker.

7행은 레이블을 시각적으로 보이도록 만든다. 위젯들은 항상 숨겨진 만들어지고, 그래서 우리는 그것들을 보이기 전에 설정을 변경할 있고, 깜빡거림을 방지한다.

Line 8 passes control of the application on to Qt. At this point, the program enters the event loop. This is a kind of stand-by mode where the program waits for user actions such as mouse clicks and key presses. User actions generate events (also called "messages") to which the program can respond, usually by executing one or more functions. For example, when the user clicks a widget, a "mouse press" and a "mouse release" event are generated. In this respect, GUI applications differ drastically from conventional batch programs, which typically process input, produce results, and terminate without human intervention.

8행은 어플리케이션의 제어를 Qt에게 넘긴다. 시점에서, 프로그램은 이벤트 루프에 진입한다. 이것은 프로그램이 사용자의 마우스 클릭이나 입력같은 동작을 기다리는 일종의 대기 모드이다. 사용자의 동작은 이벤트를 만들어(또한 "메시지" 불린다), 보통 하나나 이상의 함수들을 실행하는 것에 의해, 프로그램이 반응할 있게 한다. 예를 들어, 사용자가 위젯을 클릭할 , "마우스 버튼 누름" "마우스 버튼 놓음" 이벤트들이 생성된다. 경우, GUI 어플리케이션들은, 보통 사람의 간섭이 없이 입력을 진행하고 결과를 만들고 종료하는, 전통적인 배치 프로그램과는 매우 다르다,

For simplicity, we don't bother calling delete on the QLabel object at the end of the main() function. This memory leak is harmless in such a small program, since the memory will be reclaimed by the operating system when the program terminates.

단순성을 위해, 우리는 귀찮게 main() 함수 종료시 QLable개체에 delete 호출하지 않는다. 메모리 누수는 작은 프로그램에서는 해가 없다, 메모리는 운영 체제에 의해 프로그램이 종료될 회수됙 때문이다.

Figure 1.1. Hello on Linux

사용자 삽입 이미지
It is now possible to try the program on your own machine. First, you will need to install Qt 4.1.1 (or a later Qt 4 release), a process that is explained in Appendix A. From now on, we will assume that you have a correctly installed copy of Qt 4 and that Qt's bin directory is in your PATH environment variable. (On Windows, this is done automatically by the Qt installation program.) You will also need the program's source code in a file called hello.cpp in a directory called hello. You can type in hello.cpp yourself, or copy it from the CD provided with this book, where it is available as /examples/chap01/hello/hello.cpp.

이제 당신의 기계에서 프로그램을 시도해볼 있다. 우선, 당신은 Qt 4.1.1(혹은 이후의 Qt 4 릴리스) 설치할 필요가 있고, 작업은 Appendix A 설명되어 있다. 지금부터, 우리는 당신이 Qt 4 복사본을 정상적으로 설치했고, Qt bin 디렉토리가 당신의 PATH 환경 변수에 있다고 가정할 것이다(윈도우즈에서, 이것은 Qt 설치 프로그램에 의해 자동적으로 이루어진다). 당신은hello 디렉토리의 hello.cpp 불리는 파일에 프로그램의 소스 코드가 필요하다. 당신은 스스로 hello.cpp 입력하거나, 책과 함께 제공된 CD /examples/chap01/hello/hello.cpp에서 복사할 있다.

From a command prompt, change the directory to hello, then type

명령 프롬프트에서, 디렉토리를 hello 바꾸고, 다음을 입력하라

qmake -project

to create a platform-independent project file (hello.pro), then type

플랫폼 독립적인 프로젝트 파일(hello.pro) 만들기 위해, 다음을 입력하라

qmake hello.pro

to create a platform-specific makefile from the project file.

플랫폼에 맞는 makefile 프로젝트 파일로부터 생성하기.

Type make to build the program.[*] Run it by typing hello on Windows, ./hello on Unix, and open hello.app on Mac OS X. To terminate the program, click the close button in the window's title bar.

프로그램을 빌드하기 위해 make 입력하라. 윈도우즈에서 hello, 유닉스에서 ./hello, 그리고 OS X에서 open hello.app 입력해 실행하라. 프로그램을 종료하기 위해, 윈도우 타이틀 바의 종료 버튼을 클릭하라.

[*] If you get a compiler error on the <QApplication> include, it probably means that you are using an older version of Qt. Make sure that you are using Qt 4.1.1 or a later Qt 4 release.

<QApplication> include하는데 컴파일러 에러가 발생한다면, 그것은 아마도 당신이 Qt 이전 버전을 사용하는 것을 의미한다. 당신이 Qt 4.1.1이나 이후의 Qt 4 릴리스를 사용하는 것을 확실히 해라.

If you are using Windows and have installed the Qt Open Source Edition and the MinGW compiler, you will have a shortcut to a DOS Prompt window that has all the environment variables correctly set up for Qt. If you start this window, you can compile Qt applications within it using qmake and make as described above. The executables produced are put in the application's debug or release folder, for example, C:\qt-book\hello\release\hello.exe.

당신이 윈도우즈를 사용하고 Qt 오픈 소스판과 MinGW 컴파일러를 설치했다면, 당신은 Qt 위해 모든 환경 변수가 옳게 설정된 도스 프롬프트 윈도우에 대한 단축키를 얻을 것이다. 당신이 윈도우를 시작하면, 당신은 위에서 설명한 qmake make 사용해 그것 내에서 Qt 어플리케이션을 컴파일 있다. 실행 파일은 어플리케이션의 debug release 폴더에 생성된다, 예를 들어 C:\qt-book\hello\release\hello.exe

If you are using Microsoft Visual C++, you will need to run nmake instead of make. Alternatively, you can create a Visual Studio project file from hello.pro by typing

당신이 마이크로소프트 비주얼 C++ 사용한다면, 당신은 make 대신에 nmake 실행할 필요가 있다. 대신에, 당신은 다음을 입력해 비주얼 스튜디오 프로젝트 파일을 hello.pro로부터 생성할 있다

qmake -tp vc hello.pro

and then build the program in Visual Studio. If you are using Xcode on Mac OS X, you can generate an Xcode project using the command

그리고 비주얼 스튜디오에서 프로그램을 만들어라. 당신은 OS X에서 Xcode 사용한다면, 당신은 다음 명령을 이용해 Xcode 프로젝트를 생성할 있다.

qmake -spec macx-xcode

Figure 1.2. A label with basic HTML formatting 
사용자 삽입 이미지

Before we go on to the next example, let's have some fun: Replace the line

다음 예제로 넘어가기 전에, 재미있는 것을 보자: 다음 행을 바꾼다

QLabel *label = new QLabel("Hello Qt!");

 

with

이것과 같이

QLabel *label = new QLabel("<h2><i>Hello</i> "
                           "<font color=red>Qt!</font></h2>");

 

and rebuild the application. As the example illustrates, it's easy to brighten up a Qt application's user interface using some simple HTML-style formatting.

그리고 프로그램을 다시 만들어라. 예제에서 보여지듯이, Qt 어플리케이션의 유저 인터페이스를 어떤 간단한 HTML 스타일 포맷팅을 이용해 좋게 만드는 것은 쉽다.

Comments