{"id":2272,"date":"2025-10-11T14:29:18","date_gmt":"2025-10-11T05:29:18","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=2272"},"modified":"2025-10-13T12:27:57","modified_gmt":"2025-10-13T03:27:57","slug":"qtexample3-qmlc2","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2025\/10\/11\/qtexample3-qmlc2\/","title":{"rendered":"[QT]example3-QML+C++(2)"},"content":{"rendered":"\n<p>\ud83d\udc49[QT]example3-QML+C++(1)\uc758 \ucd94\uac00 \uc124\uba85\uc785\ub2c8\ub2e4.<br>Additional explanation of [QT]example3-QML+C++(1).<\/p>\n\n\n\n<p><strong>1.\ucf54\ub4dc\uc758 \uc5ed\ud560 \/ Role of code<\/strong><\/p>\n\n\n\n<p>\u2714\ufe0fMyConnector.h : <\/p>\n\n\n\n<p>&#8212; \ud074\ub798\uc2a4\uc758 \uba64\ubc84 \ubcc0\uc218, \uba54\uc11c\ub4dc, \uc2dc\uadf8\ub110\/\uc2ac\ub86f \ub4f1\uc744 \uc815\uc758 \ud569\ub2c8\ub2e4.<br>Defines member variables, methods, signals\/slots, etc. of a class.<\/p>\n\n\n\n<p>&#8212; \uc5ec\uae30\uc5d0 \uae30\ub2a5 \uad6c\ud604\uc740 \ud558\uc9c0 \uc54a\uc558\uc2b5\ub2c8\ub2e4.<br>No functionality is implemented here.<\/p>\n\n\n\n<p>&#8212; \ub2e4\ub978 \ud30c\uc77c\uc5d0\uc11c \uc774 \ud074\ub798\uc2a4\ub97c \uc0ac\uc6a9\ud558\ub824\uba74 \uc774 \ud5e4\ub354\ub97c #include \ud574\uc57c \ud569\ub2c8\ub2e4<br>To use this class in other files, you must #include this header.<\/p>\n\n\n\n<p>&#8212; \uc774 \ucf54\ub4dc\uc5d0 \uae30\uc220\ud55c \ub0b4\uc6a9\uc744 MyConnector.cpp\uc640 Screen01.qml\uc5d0\uc11c \uae30\ub2a5\uc744 \uad6c\ud604\ud569\ub2c8\ub2e4.<br>Implement the functionality described in this code in MyConnector.cpp and Screen01.qml.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyConnector : public QObject {}\n-- QObject\uc0c1\uc18d :  signal\/slot\uc0ac\uc6a9\nQObject inheritance: using signal\/slot\n\n\nexplicit\n-- explict\ud0a4\uc6cc\ub4dc\ub97c \uc0ac\uc6a9\ud574\uc11c \uc554\uc2dc\uc801 \ud615\ubcc0\ud658\uc744\ub9c9\uc74c(\uc624\ub958 \ubc1c\uc0dd\uc744 \uc904\uc774\uae30 \uc704\ud574)\nUse the explicit keyword to prevent implicit type conversion (to reduce errors)\n \nQ_INVOKABLE\n-- \uc774 \ud0a4\uc6cc\ub4dc\ub294 C++ \uba54\uc11c\ub4dc\ub97c QML\uc5d0\uc11c \uc9c1\uc811 \ud638\ucd9c\ud560 \uc218 \uc788\uac8c \ud574\uc8fc\ub294 \ud0a4\uc6cc\ub4dc\nThis keyword allows you to call C++ methods directly from QML.\n\nQ_OBJECT\n-- QT\uc640c++\uc744 \uc5f0\uacb0\ud558\uae30 \uc704\ud55c \ub9e4\ud06c\ub85c(\ucf54\ub4dc\ub97c \uc790\ub3d9\uc73c\ub85c \uce58\ud658\ud558\uac70\ub098 \uc0bd\uc785\ud574\uc8fc\ub294 \uc9c0\uc2dc\ubb38\nMacros for connecting QT and C++ (directives that automatically replace or insert code)\n)\n\nsignals:\n  void resultReady(const QString &amp;result);\n-- \uc2dc\uadf8\ub110\uc744 \uc815\uc758 \/ define a signal\n\n<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0fMyConnector.cpp<\/p>\n\n\n\n<p>&#8212; \uc5ec\uae30\uc11c \uae30\ub2a5 \uad6c\ud604\uc744 \ud569\ub2c8\ub2e4. \/ This is where we implement the function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> MyConnector::MyConnector(QObject *parent) : QObject(parent) {}\n \n  -- \uc774 \ucf54\ub4dc\ub294 \uc0dd\uc131\uc790 \ucd08\uae30\ud654 \ucf54\ub4dc\ub85c \uc544\ub798 \ubd80\ubd84\uc758 \ucf54\ub4dc\uc640 \uac19\uc740 \ub0b4\uc6a9\uc785\ub2c8\ub2e4..\n     This code is the constructor initialization code and is the same as the code below.\n \n\n    class MyConnector {\n        public:\n            MyConnector(QObject *parent);\n    };\n\n\n\n QObject(parent) :\n \n -- \ubd80\ubaa8 \ud074\ub798\uc2a4 QObject\uc758 \uc0dd\uc131\uc790\ub97c \ud638\ucd9c\ud569\ub2c8\ub2e4.\n    Calls the constructor of the parent class QObject.\n -- \uc0c1\uc18d \ubc1b\uc744 \ub584 \uc0c1\uc18d\ubc1b\ub294 \ubd80\ubaa8\uc758 \uc0dd\uc131\uc790\ub3c4 \ucd08\uae30\ud654 \ud574\uc57c\ud569\ub2c8\ub2e4.\n\n    When inheriting, you must also initialize the constructor of the inheriting parent.\n -- \uadf8\ub798\uc11c MyConnector \uac1d\uccb4\ub97c \ub9cc\ub4e4 \ub54c, QObject\ub3c4 \ucd08\uae30\ud654 \ud569\ub2c8\ub2e4.\n    So when we create the MyConnector object, we also initialize the QObject.\n\nemit resultReady(combined);\n-- updateText\ud568\uc218 \ud638\ucd9c\uc2dc(Screen01.qml\uc5d0\uc11c \ubc84\ud2bc\ud074\ub9ad \ud560\ub54c) \uc774 resultReady(combined)\uc2dc\uadf8\ub110\uc744 \ubc1c\uc0dd\uc2dc\ud0b5\ub2c8\ub2e4.\n-- When the updateText function is called (when the button is clicked in Screen01.qml), this resultReady(combined) signal is generated.<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0fqml\/Screen01.qml :<\/p>\n\n\n\n<p>&#8212;<strong> Connections{ } &#8221; <\/strong><\/p>\n\n\n\n<p> &#8211;&gt;C++ \uac1d\uccb4\uc758 \uc2dc\uadf8\ub110\uc744 QML\uc5d0\uc11c \ucc98\ub9ac<br>Handling signals from C++ objects in QML<\/p>\n\n\n\n<p>&#8212;<strong> target : myConnector <\/strong><\/p>\n\n\n\n<p>&#8211;&gt;\uc5f0\uacb0\ud560 C++ \uac1d\uccb4 (myConnector) [myConnector.h]<br>C++ object to connect (myConnector) [myConnector.h]<br><\/p>\n\n\n\n<p>&#8212; QML UI\uc5d0\uc11c C++ \uac1d\uccb4\ub97c \uc0ac\uc6a9\ud569\ub2c8\ub2e4.<br>Use C++ objects in QML UI.<\/p>\n\n\n\n<p>&#8212; \ubc84\ud2bc \ud074\ub9ad \uc2dc updateText() \ud638\ucd9c, \uc2dc\uadf8\ub110 \ubc1c\uc0dd \uc2dc onResultReady()\ub85c \ubc18\uc751\ud569\ub2c8\ub2e4.<br>When the button is clicked, updateText() is called, and when a signal is generated, onResultReady() is responded to.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        Button {\n            id: button\n            text: qsTr(\"Button\")\n            \/\/ === \ucf54\ub4dc\ucd94\uac00:\ud074\ub9ad \uc774\ubca4\ud2b8 ===\n            \/\/ === Add code:Click Event ===\n            onClicked: myConnector.updateText(textEdit.text, textEdit2.text)\n\n        }\n        \/\/ === \ucf54\ub4dc\ucd94\uac00:\uc5f0\uacb0 ===\n        \/\/ === Add code:connection ===\n        Connections {\n            target: myConnector\n\n            \/\/ Qt QML\uc5d0\uc11c\ub294 \uc2dc\uadf8\ub110 \uc774\ub984\uc774 resultReady\uc774\uba74,\n            \/\/ QML\uc5d0\uc11c \uadf8 \uc2dc\uadf8\ub110\uc5d0 \ubc18\uc751\ud558\ub294 \ud568\uc218 \uc774\ub984\uc740 **\uc790\ub3d9\uc73c\ub85c onResultReady\uac00 \ub429\ub2c8\ub2e4.\n            \/\/ \ud30c\ub77c\uba54\ud130 \ud0c0\uc785\uacfc \uc22b\uc790\uac00 \uc77c\uce58\ud574\uc57c\ud568.\n            \/\/ In Qt QML, if the signal name is resultReady,\n            \/\/ the function that responds to that signal in QML is automatically named onResultReady.\n            \/\/ The parameter types and numbers must match.\n            \n            function onResultReady(result) {\n                text1.text = result\n            }\n        }<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0fmain.cpp :<\/p>\n\n\n\n<p>&#8212; C++\ud30c\uc77c\uacfc QML\ud30c\uc77c\uc744 \uc5f0\uacb0\ud558\ub294 \ube0c\ub9bf\uc9c0 \uc5ed\ud560.<br>Acts as a bridge connecting C++ files and QML files.<\/p>\n\n\n\n<p>&#8212; MyConnector.cpp\uc640 Screen01.qml\ud30c\uc77c\uc744 \uc5f0\uacb0\ud569\ub2c8\ub2e4.<br>Connect the MyConnector.cpp and Screen01.qml files.<\/p>\n\n\n\n<p>\u2714\ufe0fMain.qml : <\/p>\n\n\n\n<p>&#8212; \uc774 qml\ud30c\uc77c\uc5d0\uc11c qml\/Screen01.qml\ud30c\uc77c \ub85c\ub529\ud569\ub2c8\ub2e4.<br>Load the qml\/Screen01.qml file from this qml file.<\/p>\n\n\n\n<p><strong>2.\ud750\ub984 \uc815\ub9ac \/ flow summary<\/strong><\/p>\n\n\n\n<p>1) \ud5e4\ub354\uc5d0\uc11c \uc2dc\uadf8\ub110 \uc815\uc758 \ud569\ub2c8\ub2e4.<br>Define the signal in the header.<\/p>\n\n\n\n<p>2) MyConnector.cpp\uc5d0\uc11c \uba54\uc11c\ub4dc\uc640 \ud568\uaed8 \uad6c\ud604 \ud569\ub2c8\ub2e4.<br>Implement it with methods in MyConnector.cpp.<\/p>\n\n\n\n<p>3)\uba54\uc11c\ub4dc \ud638\ucd9c\uc2dc \uc2dc\uadf8\ub110\uc744 \uc2e4\ud589\ud569\ub2c8\ub2e4.<br>Executes a signal when a method is called.<\/p>\n\n\n\n<p>4)main.cpp\uc5d0\uc11c QML(Screen01.qml)\ud30c\uc77c\uacfc MyConnector.cpp\uc5f0\uacb0\uc124\uc815<br>Setting up the connection between the QML (Screen01.qml) file and MyConnector.cpp in main.cpp<\/p>\n\n\n\n<p>5)\ubc84\ud2bc \ud074\ub9ad\uc73c\ub85c \uac12\uc740 MyConnector.cpp\uc758 \ud568\uc218\ub97c \ud638\ucd9c\ud558\uba74\uc11c \uac12\uc744 \ub118\uaca8\uc8fc\uace0<br>\ud568\uc218\ub0b4\uc5d0\uc11c \uc2dc\uadf8\ub110 \uc2e4\ud589\ud55c\ub2e4.<br>When a button is clicked, the value is passed to a function in MyConnector.cpp, which then executes a signal within the function.<\/p>\n\n\n\n<p>6)\uc2dc\uadf8\ub110 \uc774\ub984\uc5d0 on&lt;\uc2dc\uadf8\ub110&gt;\uc774\ub984\uc774 \ub3d9\uc77c\ud55c \ud568\uc218\ub97c Screen02.qml\uc758 Connection\ube14\ub7ed\uc5d0\uc11c \ucc3e\uc544 \uac12\uc744 \ub118\uaca8\ub454\ub2e4.<br>Find the function with the same name as on in the Connection block of Screen02.qml and pass the value to it.<\/p>\n\n\n\n<p>7)\uacb0\uad6d\uc740 \uc2dc\uadf8\ub110\uc744 emit\uc744 \uc2e4\ud589\ud558\uba74 on&lt;\uc2dc\uadf8\ub110\uc774\ub984&gt;\uacfc \uac19\uc740 \ud568\uc218\uc5d0 \uac12\uc744 \ub118\uaca8\uc8fc\ub294\uac8c \ud575\uc2ec \ud3ec\uc778\ud2b8\uc785\ub2c8\ub2e4.<br>Ultimately, the key point is that when you emit a signal, you pass a value to a function like on.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49[QT]example3-QML+C++(1)\uc758 \ucd94\uac00 \uc124\uba85\uc785\ub2c8\ub2e4.Additional explanation of [QT]example3-QML+C++(1). 1.\ucf54\ub4dc\uc758 \uc5ed\ud560 \/ Role of code \u2714\ufe0fMyConnector.h : &#8212; \ud074\ub798\uc2a4\uc758 \uba64\ubc84 \ubcc0\uc218, \uba54\uc11c\ub4dc, \uc2dc\uadf8\ub110\/\uc2ac\ub86f \ub4f1\uc744 \uc815\uc758 \ud569\ub2c8\ub2e4.Defines member variables, methods, signals\/slots, etc. of a class. &#8212; \uc5ec\uae30\uc5d0 \uae30\ub2a5 \uad6c\ud604\uc740 \ud558\uc9c0 \uc54a\uc558\uc2b5\ub2c8\ub2e4.No functionality is implemented here. &#8212; \ub2e4\ub978 \ud30c\uc77c\uc5d0\uc11c \uc774 \ud074\ub798\uc2a4\ub97c \uc0ac\uc6a9\ud558\ub824\uba74 \uc774 \ud5e4\ub354\ub97c #include \ud574\uc57c \ud569\ub2c8\ub2e4To use this class [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,1],"tags":[],"class_list":["post-2272","post","type-post","status-publish","format-standard","hentry","category-qt","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=2272"}],"version-history":[{"count":36,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2272\/revisions"}],"predecessor-version":[{"id":2358,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2272\/revisions\/2358"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=2272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=2272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=2272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}