👉[QT]example3-QML+C++(1)의 추가 설명입니다.
Additional explanation of [QT]example3-QML+C++(1).
1.코드의 역할 / Role of code
✔️MyConnector.h :
— 클래스의 멤버 변수, 메서드, 시그널/슬롯 등을 정의 합니다.
Defines member variables, methods, signals/slots, etc. of a class.
— 여기에 기능 구현은 하지 않았습니다.
No functionality is implemented here.
— 다른 파일에서 이 클래스를 사용하려면 이 헤더를 #include 해야 합니다
To use this class in other files, you must #include this header.
— 이 코드에 기술한 내용을 MyConnector.cpp와 Screen01.qml에서 기능을 구현합니다.
Implement the functionality described in this code in MyConnector.cpp and Screen01.qml.
class MyConnector : public QObject {}
-- QObject상속 : signal/slot사용
QObject inheritance: using signal/slot
explicit
-- explict키워드를 사용해서 암시적 형변환을막음(오류 발생을 줄이기 위해)
Use the explicit keyword to prevent implicit type conversion (to reduce errors)
Q_INVOKABLE
-- 이 키워드는 C++ 메서드를 QML에서 직접 호출할 수 있게 해주는 키워드
This keyword allows you to call C++ methods directly from QML.
Q_OBJECT
-- QT와c++을 연결하기 위한 매크로(코드를 자동으로 치환하거나 삽입해주는 지시문
Macros for connecting QT and C++ (directives that automatically replace or insert code)
)
signals:
void resultReady(const QString &result);
-- 시그널을 정의 / define a signal
✔️MyConnector.cpp
— 여기서 기능 구현을 합니다. / This is where we implement the function.
MyConnector::MyConnector(QObject *parent) : QObject(parent) {}
-- 이 코드는 생성자 초기화 코드로 아래 부분의 코드와 같은 내용입니다..
This code is the constructor initialization code and is the same as the code below.
class MyConnector {
public:
MyConnector(QObject *parent);
};
QObject(parent) :
-- 부모 클래스 QObject의 생성자를 호출합니다.
Calls the constructor of the parent class QObject.
-- 상속 받을 떄 상속받는 부모의 생성자도 초기화 해야합니다.
When inheriting, you must also initialize the constructor of the inheriting parent.
-- 그래서 MyConnector 객체를 만들 때, QObject도 초기화 합니다.
So when we create the MyConnector object, we also initialize the QObject.
emit resultReady(combined);
-- updateText함수 호출시(Screen01.qml에서 버튼클릭 할때) 이 resultReady(combined)시그널을 발생시킵니다.
-- When the updateText function is called (when the button is clicked in Screen01.qml), this resultReady(combined) signal is generated.
✔️qml/Screen01.qml :
— Connections{ } ”
–>C++ 객체의 시그널을 QML에서 처리
Handling signals from C++ objects in QML
— target : myConnector
–>연결할 C++ 객체 (myConnector) [myConnector.h]
C++ object to connect (myConnector) [myConnector.h]
— QML UI에서 C++ 객체를 사용합니다.
Use C++ objects in QML UI.
— 버튼 클릭 시 updateText() 호출, 시그널 발생 시 onResultReady()로 반응합니다.
When the button is clicked, updateText() is called, and when a signal is generated, onResultReady() is responded to.
Button {
id: button
text: qsTr("Button")
// === 코드추가:클릭 이벤트 ===
// === Add code:Click Event ===
onClicked: myConnector.updateText(textEdit.text, textEdit2.text)
}
// === 코드추가:연결 ===
// === Add code:connection ===
Connections {
target: myConnector
// Qt QML에서는 시그널 이름이 resultReady이면,
// QML에서 그 시그널에 반응하는 함수 이름은 **자동으로 onResultReady가 됩니다.
// 파라메터 타입과 숫자가 일치해야함.
// In Qt QML, if the signal name is resultReady,
// the function that responds to that signal in QML is automatically named onResultReady.
// The parameter types and numbers must match.
function onResultReady(result) {
text1.text = result
}
}
✔️main.cpp :
— C++파일과 QML파일을 연결하는 브릿지 역할.
Acts as a bridge connecting C++ files and QML files.
— MyConnector.cpp와 Screen01.qml파일을 연결합니다.
Connect the MyConnector.cpp and Screen01.qml files.
✔️Main.qml :
— 이 qml파일에서 qml/Screen01.qml파일 로딩합니다.
Load the qml/Screen01.qml file from this qml file.
2.흐름 정리 / flow summary
1) 헤더에서 시그널 정의 합니다.
Define the signal in the header.
2) MyConnector.cpp에서 메서드와 함께 구현 합니다.
Implement it with methods in MyConnector.cpp.
3)메서드 호출시 시그널을 실행합니다.
Executes a signal when a method is called.
4)main.cpp에서 QML(Screen01.qml)파일과 MyConnector.cpp연결설정
Setting up the connection between the QML (Screen01.qml) file and MyConnector.cpp in main.cpp
5)버튼 클릭으로 값은 MyConnector.cpp의 함수를 호출하면서 값을 넘겨주고
함수내에서 시그널 실행한다.
When a button is clicked, the value is passed to a function in MyConnector.cpp, which then executes a signal within the function.
6)시그널 이름에 on<시그널>이름이 동일한 함수를 Screen02.qml의 Connection블럭에서 찾아 값을 넘겨둔다.
Find the function with the same name as on in the Connection block of Screen02.qml and pass the value to it.
7)결국은 시그널을 emit을 실행하면 on<시그널이름>과 같은 함수에 값을 넘겨주는게 핵심 포인트입니다.
Ultimately, the key point is that when you emit a signal, you pass a value to a function like on.