翻譯|實施案例|編輯:鮑佳佳|2020-09-18 12:21:29.850|閱讀 752 次
概述:此示例包含一個系統UI和三個示例應用程序,總共產生了四個單獨的QML應用程序。每個應用程序都放置在其自己的單獨目錄中。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Qt(發音為“ cute”,而不是“ cu-tee”)是一個跨平臺框架,通常用作圖形工具包,它不僅創建CLI應用程序中非常有用。而且它也可以在三種主要的臺式機操作系統以及移動操作系統(如Symbian,Nokia Belle,Meego Harmattan,MeeGo或BB10)以及嵌入式設備,Android(Necessitas)和iOS的端口上運行。現在我們為你提供了免費的試用版。趕快點擊下載Qt最新試用版吧>>
通過Intent進行通信的三個應用程序和一個系統UI。
此示例包含一個系統UI和三個示例應用程序(“Red Intents”,“Green Intents”和“Blue Intents”),總共產生了四個單獨的QML應用程序。
每個應用程序都放置在其自己的單獨目錄中,如下所述。由于所有應用程序和系統UI都使用基于QtQuickControls 2的UI,因此其組件位于共享目錄中。
運行示例
假設appman可執行文件在您的路徑中,則可以按以下方式運行系統UI:
examples/applicationmanager/intents$ appman --builtin-apps-manifest-dir ./apps system-ui.qml
添加-o "ui: { style: material }"將使示例的外觀更好看。
如下圖所示:
有關這些和其他命令行選項的信息,您可以運行appman --help或查看配置文檔。
應用實施
所有應用程序(紅色,綠色和藍色)都是相同的,它們main.qml只是實例化共享的IntentsApplicationWindow組件。
import "../../shared" "../../shared" IntentsApplicationWindow { }{ }
IntentsApplicationWindow組件其UI內容是通過實例化該IntentsUIPage組件來定義的,該組件也可以共享。這個UI組件沒有任何特定于意圖的代碼,因此實際的發送是在附加到IntentsUIPage請求信號的信號處理程序中完成的:
onRequest: { { var request = var request = IntentClient.sendIntentRequest(intentId, applicationId, parameters) request.onReplyReceived.connect(function() { { intentPage.setResult(request.requestId, request.succeeded, request.succeeded ? ? request.result : : request.errorMessage) })}) }}
在使用在UI中選擇的參數調用IntentClient :: sendIntentRequest之后,示例代碼會將函數對象連接到請求的ReplyReceived信號。結果放置在UI 的“ request”字段中。
另外,它為應用程序定義了所有必需的IntentHandlers,例如:
IntentHandler { { intentIds: "rotate-window" onRequestReceived: { { rotationAnimation.start() request.sendReply({ "done": "done": true })}) }} }}
這些intent處理程序并不復雜,每個intent處理程序僅觸發一個也在此文件中定義的基本動畫,因此對于rotate-window意圖而言,如下:
RotationAnimation on on rotation { { id: rotationAnimation running: false duration: 500; from: 0; to: 360 }}
在QML中,僅實現IntentHandlers是不夠的,因為應用程序管理器需要了解有關哪個應用程序支持哪些意圖的信息。在應用程序運行之前,此信息必須對應用程序管理器可用,以便于根據intent請求自動啟動應用程序。對于應用程序管理器中的所有其他應用程序配置,這都是通過應用程序的清單文件完成的info.yaml:
將紅色應用定義了三個可用的intent:
intents:: - id: rotate-window- id: rotate-window name:: en: Rotate Red: Rotate Red - id: scale-window- id: scale-window name:: en: Scale Red: Scale Red - id: blink-window- id: blink-window name:: en: Blink Red: Blink Red
此外,紅色應用程序獲得了該call-blue功能,這是藍色應用程序中某些intent所必需的。
capabilities: 'call-blue': 'call-blue'
綠色應用程序定義只有兩個可用的意圖。需要注意的是,即使該應用程序有一個IntentHandler的blink-window intents通過共享IntentsApplicationWindow組件,此處理程序不會被調用,因為這個intents ID沒有通過info.yaml清單在系統中注冊:
intents:: - id: rotate-window- id: rotate-window name:: en: Rotate Green: Rotate Green - id: scale-window- id: scale-window name:: en: Scale Green: Scale Green
藍應用程序最復雜的intents定義。除了處理與Red應用程序相同的三個blue-window-privateIntent之外,它還注冊具有屬性的Intent visibility: private。只能從注冊它們的同一應用程序中請求私有意圖,因此在這種情況下,只有Blue可以成功請求該blue-window-private意圖。此外,rotate-window僅具有此call-blue功能的應用程序可以請求該意圖:此處,紅色應用程序具有所需的功能,而綠色應用程序則沒有。
intents:: - id: rotate-window- id: rotate-window name:: en: Rotate Blue: Rotate Blue requiredCapabilities: [ 'call-blue' ]: [ 'call-blue' ] - id: scale-window- id: scale-window name:: en: Scale Blue: Scale Blue - id: blink-window- id: blink-window name:: en: Blink Blue: Blink Blue - id: blue-window-private- id: blue-window-private name:: en: Blue Private Intent: Blue Private Intent visibility: private: private系統用戶界面實現
除了用于啟動和停止應用程序的左側欄之外,系統界面還具有兩個用于處理意圖機制的特殊功能:
在系統界面中處理intent
intents不僅可以在應用程序中處理,而且可以在系統UI中處理。由于系統UI始終在運行,因此我們不需要依賴info.yaml清單文件來定義支持的intents,而是可以直接將所需的元數據聲明為IntentServerHandler組件的屬性。該IntentServerHandler實際上是源自IntentHandler,所以它的工作方式為應用端計數器部分相同的:它只會用所需的屬性來定義所有元數據(例如names,icon...)。
IntentServerHandler { { intentIds: "rotate-window" names: { "en": { "en": "Rotate System UI" } } visibility: IntentObject.Public onRequestReceived: { { rotationAnimation.start() request.sendReply({ "wasRequestedBy": "wasRequestedBy": request.requestingApplicationId })}) }} }}
處理程序回調與應用程序中的回調幾乎相同。這里唯一值得注意的區別是,我們可以訪問requestingApplicationId來標識請求的來源。出于安全原因,該數據不適用于應用程序中的intents處理程序。
消除意圖請求的歧義該示例實現了一個UI,使用戶可以選擇如何消除傳入的intents請求的歧義。在此處完成IntentServer的消歧請求的注冊:
Connections { { target: IntentServer function onDisambiguationRequest(requestId, potentialIntents) { requestId, potentialIntents) { disambiguationDialog.add(requestId, potentialIntents) }} }}
由于我們希望在系統中并行intents請求的數量方面保持靈活性,因此我們僅將傳入請求添加到隊列(allRequests)中。將Dialog消耗該隊列是一個相當標準的QtQuickControls 2對話框,讓你展示你的應用程序可能選擇從IntentServer :: disambiguationRequested()信號來一個漂亮的UI。在按下Ok或Cancel之后,對話框將通知IntentServer用戶選擇:
onAccepted: { { IntentServer.acknowledgeDisambiguationRequest(currentRequest.requestId, currentRequest.intents[handlingApplications.currentIndex]); showNext() }} onRejected: { { IntentServer.rejectDisambiguationRequest(currentRequest.requestId) showNext() }}
本篇文章中的內容你都學會了嗎?如果這篇文章沒能滿足你的需求、點擊獲取更多文章教程!現在立刻下載Qt免費試用吧!更多Qt類開發工具QtitanRibbon、QtitanChart、QtitanNavigation、QtitanDocking、QtitanDataGrid在線訂購現直降1000元,歡迎咨詢慧都獲取更多優惠>>
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自: