Part I: Basic Qt – Ch01. Making Connections 본문

Programming/C++/Qt

Part I: Basic Qt – Ch01. Making Connections

halatha 2008. 7. 31. 11:46

Making Connections

The second example shows how to respond to user actions. The application consists of a button that the user can click to quit. The source code is very similar to Hello, except that we are using a QPushButton instead of a QLabel as our main widget, and we are connecting a user action (clicking a button) to a piece of code.

번째 예제는 사용자의 동작에 어떻게 반응하는지 보여줄 것이다. 어플리케이션은 사용자가 종료를 위해 클릭할 있는 버튼으로 구성되었다. 소스 코드는 Hello 매우 유사하다, 우리가 QLabel 대신 QPushButton 메인 위젯으로 이용하는 것과 우리가 유저의 동작(버튼을 클릭하는 ) 코드에 연결하는 것을 제외하면.

This application's source code is on the CD in the file /examples/chap01/quit/quit.cpp. Here's the contents of the file:

어플리케이션의 소스 코드는 CD /examples/chap01/quit/quit.cpp 있다. 여기 파일의 내용이 있다:

 1 #include <QApplication>
 2 #include <QPushButton>
 3 int main(int argc, char *argv[])
 4 {
 5     QApplication app(argc, argv);
 6     QPushButton *button = new QPushButton("Quit");
 7     QObject::connect(button, SIGNAL(clicked()),
 8                      &app, SLOT(quit()));
 9     button->show();
10     return app.exec();
11 }

 

Qt's widgets emit signals to indicate that a user action or a change of state has occurred.[*] For instance, QPushButton emits a clicked() signal when the user clicks the button. A signal can be connected to a function (called a slot in that context), so that when the signal is emitted, the slot is automatically executed. In our example, we connect the button's clicked() signal to the QApplication object's quit() slot. The SIGNAL() and SLOT() macros are part of the syntax; they are explained in more detail in the next chapter.

Qt 위젯은 유저의 동작이나 상태의 변화가 발생한 것을 표시하기 위해 시그널을 내보낸다. 예를 들어, QPushButton 사용자가 버튼을 클릭할 clicked() 시그널을 내보낸다. 시그널은 함수에 연결될 있어( 컨텍스트에서 슬롯이라 불린다), 시그널이 나올 , 슬롯은 자동으로 실행된다. 우리의 예제에서, 우리는 버튼의 clicked() 시그널을 QApplication 개체의 quit() 슬롯에 연결한다. SIGNAL() SLOT() 매크로는 문법의 일부이다; 그것들은 다음 장에서 자세하게 설명될 것이다.

[*] Qt signals are unrelated to Unix signals. In this book, we are only concerned with Qt signals.

Qt 시그널들은 유닉스 시그널과는 관련이 없다. 책에서는, 우리는 단지 Qt 시그널들만 관여할 것이다.

Figure 1.3. The Quit application

사용자 삽입 이미지
We will now build the application. We assume that you have created a directory called quit containing quit.cpp. Run qmake in the quit directory to generate the project file, then run it again to generate a makefile, as follows:

우리는 이제 어플리케이션을 만들 것이다. 우리는 당신이 quit.cpp 포함하는 quit라는 디렉토리를 이미 만든 것으로 가정한다. 프로젝트 파일을 생성하기 위해 quit 디렉토리에서 qmake 실행하고, 다음에 makefile 생성하기 위해, 다음과 같이, 그것을 다시 실행하라:

qmake -project
qmake quit.pro

Now build the application, and run it. If you click Quit, or press Space (which presses the button), the application will terminate.

이제 어플리케이션을 만들고, 실행하라. 당신이 Quit를 클릭하거나, (버튼을 입력하는) Space를 누르면 어플리케이션은 종료할 것이다.

Comments