Lua 기반 확장 기능 만들기
Qt Creator Lua 스크립트를 사용하여 확장할 수 있습니다. 포함된 Lua 엔진은 Lua 5.4.6을 기반으로 합니다.
Lua 확장 기능 작성
새로운 Lua 확장 기능을 만들려면, ‘ File ’ > ‘ New Project ’ > ‘ Library ’ > Qt Creator Lua Plugin를 선택합니다.
새 확장 기능을 테스트하려면 프로젝트를 실행하십시오. Application Output 에 “Hello from Lua!”가 표시되어야 합니다.
확장 기능 위치
Lua 확장 기능은 Qt Creator 의 일반적인 플러그인 위치 중 어디에나 배치할 수 있습니다. Qt Creator 가 플러그인을 검색하는 폴더를 확인하려면, 환경 변수 QT_LOGGING_RULES 를 qtc.extensionsystem*=true 로 설정하십시오. 이렇게 하면 Qt Creator 가 시작될 때 플러그인 검색 경로를 나열하는 플러그인 관리자의 디버그 출력이 활성화됩니다.
검색 경로를 추가하려면, Qt Creator 를 시작할 때 -pluginpath 명령줄 인수를 사용하십시오.
Lua 확장 기능 사양
Lua 확장 기능은 해당 확장 기능이 위치한 폴더와 동일한 이름을 가진 Lua 스크립트로 구성됩니다. 이는 확장 기능이 로드되기 위해 필수적입니다.
이 스크립트는 표시 이름, 공급업체, 저작권 등 확장 기능의 사양을 정의합니다.
--- MyExtension.lua return { Id = "myextension", Name = "MyExtension", Version = "1.0.0", CompatVersion = "1.0.0", Vendor = "My Company", VendorId = "mycompany", Category = "Tests", Description = "Describe what your extension does in a sentence.", LongDescription = [[ Tell users more about your extension. This text is rendered as Markdown. ]], Dependencies = { { Name = "Lua", Version = "15.0.0", Required = true } }, setup = function() print("Hello from Lua!") end, printToOutputPane = true, } --[[@as QtcPlugin]]
Setup 함수
설정 함수는 확장 프로그램이 로드될 때 호출됩니다. 이 함수에서 확장 프로그램의 기능을 설정할 수 있습니다. 사양 파일은 매우 제한된 권한으로 파싱되므로, 실제 기능을 구현하는 모듈을 require해야 합니다.
--- MyExtension.lua return { Name = "MyExtension", Version = "1.0.0", ..., --- This is the setup function that is called when the extension is loaded. --- It requires the 'init' module and calls the setup function from the returned table. setup = function() require 'init'.setup() end, }
--- init.lua function setup() print("Hello from Lua!") end -- Returns a table with a single field 'setup' that points to the setup function. return { setup = setup }
비동기 작업
일부 내장 연산은 비동기적으로 작동합니다. 이를 처리하려면 Async 모듈을 사용하십시오.
local a = require 'async' local u = require 'Utils' a.sync(function() print("Lets wait for 5 seconds ...") a.wait(u.waitms(5000)) print("... done!") end)
대화형 도움말
에디터에서 .lua 파일을 처음 열면 Lua Language Server를 다운로드하라는 메시지가 표시됩니다. 이 서버는 문맥에 따른 도움말과 자동 완성 기능을 제공하므로 매우 유용합니다.
Qt Creator Lua API
Qt Creator 의 Lua API는 ` require ` 함수를 사용하여 임포트할 수 있는 여러 모듈을 통해 Lua 확장 모듈에서 사용할 수 있습니다. C++ 확장 모듈은 추가 모듈을 제공할 수 있습니다. 그 예로, Language Server 클라이언트를 생성하기 위한 모듈을 제공하는 LanguageServer 확장 모듈이 있습니다.
Qt Creator 설치 디렉터리에서 Lua 모듈에 대한 API 문서 파일을 찾을 수 있습니다. macOS에서는 다음 위치에서 확인할 수 있습니다. Qt Creator.app/Contents/Resources/lua/meta.
Qt Creator 의 핵심 기능에 접근하고 상호 작용할 수 있습니다. | |
Qt Creator 에서 사용자 인터페이스 액션을 생성할 수 있습니다. | |
async/await Lua API를 사용하여 비동기 작업을 처리합니다. | |
인터넷에서 데이터를 가져옵니다. | |
사용자 인터페이스를 만듭니다. | |
Language Server 클라이언트를 등록합니다. | |
사용자에게 메시지를 표시합니다. | |
외부 프로세스를 실행합니다. | |
Qt 기능에 액세스합니다. | |
Qt Creator 에 접근하고 확장합니다. | |
설정을 읽고 씁니다. | |
단순 유형에 액세스합니다. | |
텍스트 편집기에 접근하고 조작합니다. | |
일반 유틸리티 함수 및 클래스. |
C++을 사용하여 Lua API 확장하기
Lua 인터페이스에 기능을 추가하려면 Lua 엔진에 새 모듈을 등록해야 합니다.
#include <lua/luaengine.h> class MyCppExtension final : public ExtensionSystem::IPlugin { Q_OBJECT Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "MyCppExtension.json") public: MyCppExtension() {} private: void initialize() final { // The registered function will be called when the Lua module 'MyCppExtension' is required. // The returned table will be returned from the require call in Lua. ::Lua::registerProvider("MyCppExtension", [](sol::state_view lua) -> sol::object { sol::table result = lua.create_table(); result["myFunction"] = [](int a, int b) { return a + b; }; return result; }); } };
그러면 다음과 같이 Lua 스크립트에서 ` MyCppExtension.myFunction `에 접근할 수 있습니다:
local MyCppExtension = require 'MyCppExtension' --- MyCppExtension is now a table with a single field 'myFunction', as it is returned from the --- C++ function registered via 'Lua::registerProvider(...)'. print(MyCppExtension.myFunction(1, 2))
C++ 기능을 등록하는 방법에 대한 자세한 내용은 sol2를 참조하십시오.
예제
언어 서버
Qt Creator 의 LuaLanguageClient 플러그인은 사용자 정의 언어 서버 클라이언트를 등록할 수 있도록 지원합니다. 이 기능을 사용하는 방법에 대한 예제는 Qt Creator 확장 프로그램인 "Lua Language Server" 및 "Rust Language Server"에서 확인할 수 있습니다.
‘C++ 기반 플러그인 만들기’도 참조하십시오 .
Copyright © The Qt Company Ltd. and other contributors. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.