본문 바로가기
대외활동/한화비전 VEDA

시험대비 qt c++ 복습

by hoshi03 2024. 8. 25.

• 시작하기

#include "mainwindow.h"
#include <QApplication>
#include <QLabel>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QLabel *hello = new QLabel("Hello Worild!");
    hello->show();
    hello->resize(1280,720);
    hello->move(100,100);

    return a.exec();
}

 

Qlabel을 이용해서 heelo worid 출력하기

 

• qt 이벤트 처리

qpushbutton을 이용해서 이벤트 처리

 

버튼 클릭시 시그널을 이용해서 종료 이벤트 호출

    QPushButton *btn = new QPushButton("btn",0);
    btn->show();
    QObject::connect(btn, SIGNAL(clicked(bool)), &a, SLOT(quit()));

 

• 사용자 정의 위젯

 

헤더 클래스

선언부 아래쪽애 Q_OBJECT 매크로를 이용해서 사용자 정의라는걸 명시해준다

#ifndef CUSTOMWIDGET_H
#define CUSTOMWIDGET_H

#include <QWidget>

class CustomWidget : public QWidget{
    Q_OBJECT
public:
    CustomWidget(QWidget *parent = 0);

signals:
    void widgetClicked();
public slots:
    void processClick();
};

#endif // CUSTOMWIDGET_H

 

구현부

버튼이 클릭이 되면 processclick 메서드를 호출하고

그 메서드에서는 메인에 widgetClicked 시그널을 emit 한다

#include "customwidget.h"

#include <QPushButton>

CustomWidget::CustomWidget(QWidget *parent) : QWidget(parent) {
    QPushButton *button = new QPushButton("Quit",this);
    button->resize(120,35);
    this->resize(120,35);
    move(300,300);

    connect(button, SIGNAL(clicked()),SLOT(processClick()));
}

void CustomWidget::processClick(){
    emit widgetClicked();
}

 

메인에서는 wigetClicked 시그널을 받은 후 qapp을 quit하는 슬롯을 호출하게 된다

#include <QApplication>
#include "customwidget.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    CustomWidget * widget = new CustomWidget(0);
    QObject::connect(widget, SIGNAL(widgetClicked()), &a, SLOT(quit()));
    widget->show();
    return a.exec();
}

'대외활동 > 한화비전 VEDA' 카테고리의 다른 글

라즈베리파이 크로스컴파일 설정  (0) 2024.08.26
0826 리눅스 시스템 프로그래밍  (0) 2024.08.26
시험대비 c++ 복습  (0) 2024.08.25
시험대비 C 복습  (0) 2024.08.23
라이브러리  (0) 2024.08.22