Qml中的WorkerScript

來源:互聯網
上載者:User

 

QML WorkerScript Element

The WorkerScript element enables the use of threads in QML

 

source : url

Signals : onMessage

Methods : sendMessage

 

Detailed Description

Use WorkerScript to run operations in a new thread. This is useful for running operations in the background so that the main GUI thread is not blocked.

Messages can be passed between the new thread and the parent thread using sendMessage() and the onMessage() handler.

 

WorkerScript 用於產生新的線程,並通過訊息進行通訊。

 

qml檔案中聲明 WorkerScript { id: worker; source: "dataloader.js" } id和js檔案路徑。這樣qml中就可以通過worker.sendMessage(msg);發送訊息(msg是qml中定義的一var變數),js中總是這樣定義WorkerScript.onMessage = function(msg)函數,其中msg可以含有worker msg中的多個變數或屬性。同樣js中也可以通過WorkerScript.sendMessage({src:src,data:jresult});向qml發送訊息,其中qml中onMessage負責接收。

 

另外注意:

 

WorkerScript線程內沒有全域變數,因此下面這樣用法會在msg.model.append(string );造成線程的崩潰

 

var string = "Time:";

 

WorkerScript.onMessage = function(fmUrl) {

    if (msg.action == 'appendCurrentTime') {

        var data = {'time': new Date().toTimeString()};

msg.model.append(string );

        msg.model.append(data);

        msg.model.sync();   // updates the changes to the list

    }

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.