본문 바로가기

QT/QT 4.6.2

시그널(Signal) / 슬롯(Slot)을 이용한 메세지 띄우기

이번에는 다이얼로그 안에 QPushButton 두개의 위젯을 포함시켜
각각의 버튼 위젯으로 각각의 메세지가 띄워지도록 구현을 해 보았다.

먼저 소스를 보면,

/*********************************************
                 connect.h
*********************************************/

#ifndef CONNECT_H
#define CONNECT_H

#include 
#include 

class Connect : public QDialog
{
    Q_OBJECT
public:
    Connect(QWidget *parent = 0);
public slots:
    void Message1();
    void Message2();
private:
    QPushButton *button1;
    QPushButton *button2;
    QMessageBox *message;
};

#endif // CONNECT_H

/*********************************************
                 connect.cpp
*********************************************/

#include"connect.h"
#include 

Connect::Connect(QWidget *parent)
    : QDialog(parent)
{
    button1 = new QPushButton;
    button2 = new QPushButton;
    message = new QMessageBox;

    button1->setText("Hello");
    button2->setText("QT");

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(button1);
    layout->addWidget(button2);
    setLayout(layout);

    connect(button1, SIGNAL(clicked()), this, SLOT(Message1()));
    connect(button2, SIGNAL(clicked()), this, SLOT(Message2()));
}
void Connect::Message1()
{
    message->setText("Hello");
    message->show();
}
void Connect::Message2()
{
    message->setText("QT");
    message->show();
}


/*********************************************
                   main.cpp
*********************************************/

#include 

#include "connect.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    Connect *conn = new Connect;
    conn->show();

    return app.exec();
}



이렇게 구현을 하였다.

결과 화면을 보게 되면,


이렇게 출력이 나왔습니다.

간단하게 소스 분석을 하면,

    connect(button1, SIGNAL(clicked()), this, SLOT(Message1()));
    connect(button2, SIGNAL(clicked()), this, SLOT(Message2()));

이렇게 되어 있는데, 세번째 인자는 Connect 클래스 안에 있는 슬롯을 사용하기 때문에 this라고 적었습니다.
그리고
클래스 선언에서

 public slots:

라고 있는 부분은 매크로의 일종으로 전처리기에 의해 미리 이 함수들은 슬롯으로 사용이 될 것이라는 것을 알려 줄 때 사용함

여기에는 없지만

signals:

이라고 된 섹션이 보이면, 슬롯과 마찬가지로 이는 시그널로 사용될 것이라는 것을 알려 줄 때 사용하는 것이다.



'QT > QT 4.6.2' 카테고리의 다른 글

Phonon::MediaObject  (0) 2010.05.06
간단한 파일 입 / 출력 하기!!  (0) 2010.04.30
시그널(Signal) / 슬롯(Slot)을 이용한 위젯 닫기  (0) 2010.04.27
두개의 버튼을 하나의 창에 띄우기!!  (0) 2010.04.24
버튼 출력하기  (0) 2010.04.24