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