C
在应用程序中运行Qt Quick Ultralite
启动并进入Qt Quick Ultralite事件循环
硬件初始化的方式和时机由应用程序开发人员决定。通常是在调用 `Qul::initPlatform()` 之前先调用 `Qul::initHardware()` 来完成初始化,但开发人员也可以选择不调用该函数,而是自行执行硬件初始化。
要启动应用程序的图形界面部分,您需要调用 `Qul::initPlatform()`,在 `main()` 或任务函数中创建一个 `Qul::Application ` 对象,然后调用 `Qul::Application::exec()` 函数。
Qul::initPlatform() 会初始化Qt Quick Ultralite 平台组件,当您调用Qul::Application::exec() 时,事件循环便会启动。
或者,可以通过定期调用Qul::Application::update() 来执行Qt Quick Ultralite 事件循环。
裸机
例如,如果 QML 中定义的主项名为 MainScreen,则裸机环境下的 main.cpp 可能包含以下内容:
#include "MainScreen.h" #include <qul/application.h> #include <qul/qul.h> int main() { Qul::initHardware(); Qul::initPlatform(); Qul::Application app; static MainScreen item; app.setRootItem(&item); while (true) { uint64_t now = Qul::Platform::getPlatformInstance()->currentTimestamp(); // <handle timers> uint64_t nextUpdate = app.update(); if (nextUpdate > now) { // Device can go to sleep until next update is due // enterLowPowerMode(nextUpdate - now); } } return 1; }
FreeRTOS
Main.cpp 示例:
#include "MainScreen.h" #include <qul/application.h> #include <qul/qul.h> #include <platforminterface/log.h> #include <FreeRTOS.h> #include <task.h> #ifndef QUL_STACK_SIZE #error QUL_STACK_SIZE must be defined. #endif static void Qul_Thread(void *argument); // extern const HeapRegion_t xHeapRegions[]; // HeapRegion for FreeRTOS Heap_5 int main() { Qul::initHardware(); // vPortDefineHeapRegions(xHeapRegions); // Initialize FreeRTOS Heap_5 Qul::initPlatform(); if (xTaskCreate(Qul_Thread, "QulExec", QUL_STACK_SIZE, 0, 4, 0) != pdPASS) { Qul::PlatformInterface::log("Task creation failed!.\r\n"); configASSERT(false); } vTaskStartScheduler(); // Should not reach this point return 1; } static void Qul_Thread(void *argument) { Qul::Application app; static MainScreen item; app.setRootItem(&item); while (true) app.update(); }
注意: FreeRTOS Heap_5 必须在调用Qul::initPlatform() 之前进行初始化。
初始化文本缓存
裸机
要为应用程序配置文本缓存,请在 `main.app` 中调用相应的 `Qul::Application ` 构造函数,如下例所示:
#include "MainScreen.h" #include <qul/application.h> #include <qul/qul.h> int main() { Qul::initHardware(); Qul::initPlatform(); Qul::ApplicationConfiguration appConfig; appConfig.setTextCacheEnabled(true); appConfig.setTextCacheSize(128 * 1024); Qul::Application app(appConfig); static MainScreen item; app.setRootItem(&item); while (true) { uint64_t now = Qul::Platform::getPlatformInstance()->currentTimestamp(); // <handle timers> uint64_t nextUpdate = app.update(); if (nextUpdate > now) { // Device can go to sleep until next update is due // enterLowPowerMode(nextUpdate - now); } } return 1; }
FreeRTOS
Main.cpp 示例:
#include "MainScreen.h" #include <qul/application.h> #include <qul/qul.h> #include <platforminterface/log.h> #include <FreeRTOS.h> #include <task.h> #ifndef QUL_STACK_SIZE #error QUL_STACK_SIZE must be defined. #endif static void Qul_Thread(void *argument); // extern const HeapRegion_t xHeapRegions[]; // HeapRegion for FreeRTOS Heap_5 int main() { Qul::initHardware(); // vPortDefineHeapRegions(xHeapRegions); // Initialize FreeRTOS Heap_5 Qul::initPlatform(); if (xTaskCreate(Qul_Thread, "QulExec", QUL_STACK_SIZE, 0, 4, 0) != pdPASS) { Qul::PlatformInterface::log("Task creation failed!.\r\n"); configASSERT(false); } vTaskStartScheduler(); // Should not reach this point return 1; } static void Qul_Thread(void *argument) { Qul::ApplicationConfiguration appConfig; appConfig.setTextCacheEnabled(true); appConfig.setTextCacheSize(64 * 1024); Qul::Application app(appConfig); static MainScreen item; app.setRootItem(&item); while (true) app.update(); }
在某些 Qt 许可证下可用。
了解更多。