๐[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.