Qt5 official demo collections 32 -- Qt Quick Examples, qt5examples
All articles in this series can be viewed here in http://blog.csdn.net/cloud_castle/article/category/2123873
Link to Qt5 official demo release set 31 -- StocQt
Because the QML thread is involved in this Example, it is found that it belongs to the Qt Quick Example series. There are 19 demos in this series, covering multiple elements in Qt Quick. If you are free, let's take an article in this series. I believe this is a great journey ~
Well, when writing applications, especially User Interface programs, multithreading is often a topic that cannot be avoided. It takes more than 3 seconds for the to die. It is estimated that many users plan to Ctrl + Alt + Delete...
Therefore, Qt also facilitates the compilation of multithreading. In Widgets, we usually use QThread to complete multithreading. What about QML that emphasizes user experience, I have to mention WorkerScript.
WorkScript contains an Attribute source to declare a js file, which is used to open up a new thread to process those time-consuming computations. For more details, refer to Manual.
Running the program is a familiar selection interface:
1) Threaded ListModel
This example is simple and describes how to use WorkScript to update the ListModel in a new thread.
The program consists of two files:
Timedisplay. qml:
Import QtQuick 2.0 Rectangle {color: "white" width: 200 height: 300 ListView {anchors. fill: parent model: listModel delegate: Component {Text {text: time }}listmodel {id: ListModel} WorkerScript {id: worker source: "dataloader. js "// declare js processing functions }//! [0] Timer {// 2 seconds interval timer id: Timer interval: 2000; repeat: true running: true triggeredOnStart: true onTriggered: {var msg = {'action ': 'appendcurrenttime', 'model': listModel}; // here, the ListModel object is used as the parameter of sendMessage (). worker is not allowed for other QObject object types. sendMessage (msg); // call sendMessage () to run the computing of WorkerScript }}//! [0]}
Dataloader. js:
//! [0] WorkerScript. onMessage = function (msg) {// The format of the processing function is as follows: WorkerScript. onMessage = function ().... if (msg. action = 'appendcurrenttime') {var data = {'time': new Date (). toTimeString ()}; msg. model. append (data); // note that the model here declares msg by the msg parameter. model. sync (); // This function is designed for WorkScript and is used to save WorkScript modifications to ListModel }}//! [0]
The above is the implementation of a basic WorkScript. Here is a slightly more complex example:
2) WorderScript
This example uses multithreading to calculate Pascal's Triangle in the background, and intentionally uses an algorithm that is not very optimized (which can be directly obtained through the binary model, I don't quite remember), so that we can understand the background computing and the interface is still responsive.
As for what is Pascal's Triangle, I'm sure you will not be unfamiliar with it. In fact, it is what we mean by Yang Hui's Triangle:
That is to say, the value of each small square is equal to the sum of the values of the two squares above it.
In our program, the number of layers in the square is 64 layers. If we use this accumulate method to obtain the value of any square, it is naturally a relatively time-consuming operation.
First, let's look at the running effect:
As you can see, when I select the square of 54th rows and 41 columns, the result below becomes "Loading... ", but the interface is not stuck, we can still move back or choose a new square.
Workerscript. qml:
Import QtQuick 2.0 Rectangle {width: 320; height: 480 //! [1] WorkerScript {id: myWorker source: "workerscript. js "onMessage: {// note that this onMessage is different from the onMessage in js. Here it is used to update the interface display, instead of executing the computation. Similar to the signal slot, it responds to the sendMessage if (messageObject. row = rowSpinner. value & messageObject. column = columnSpinner. value) {// Not an old result if (messageObject. result =-1) resultText. text = "Column must be <= Row"; else resultText. text = messageObject. result ;}}}//! [1] Row {y: 24 spacing: 24 anchors. horizontalCenter: parent. horizontalCenter //! [0] Spinner {// custom Spinner Control id: rowSpinner label: "Row" onValueChanged: {// call the thread function resultText to change the value. text = "Loading... "; myWorker. sendMessage ({row: rowSpinner. value, column: columnSpinner. value });}}//! [0] Spinner {id: columnSpinner label: "Column" onValueChanged: {resultText. text = "Loading... "; myWorker. sendMessage ({row: rowSpinner. value, column: columnSpinner. value}) ;}}text {id: resultText y: 180 width: parent. width horizontalAlignment: Text. alignHCenter wrapMode: Text. wordWrap font. pixelSize: 32} Text {text: "Pascal's Triangle Calculator" anchors {horizontalCenter: parent. horizontalCenter; bottom: parent. bottom; bottomMargin: 50 }}}
Workerscript. js:
// Will be initialized when WorkerScript {} is instantiated var cache = new Array (64); // a two-dimensional Array of 64 x for (var I = 0; I <64; I ++) cache [I] = new Array (64); function triangle (row, column) {// define the validity of the columns of the triangle function. if (cache [row] [column]) // if the value of this square is calculated in the cache, return cache [row] [column] if (column <0 | column> row) // The number of rows must be greater than or equal to the number of columns return-1; if (column = 0 | column = row) // the left and right sides are both 1 return 1; return Triangle (row-1, column-1) + triangle (row-1, column); // otherwise, this function is called recursively to calculate the square value }//! [0] WorkerScript. onMessage = function (message) {// Calculate result (may take a while, using a naive algorithm) // original note: it may take some time, because a naive algorithm is used ~ Var calculatedResult = triangle (message. row, message. column); // Send result back to main thread WorkerScript. sendMessage ({row: message. row, // similar to the signal slot, the returned data responds to column: message in onMessage of WorkerScript. column, result: calculatedResult });}//! [0]
Finally, the implementation of the Spinner is pasted, because Qt Quick Controls does not seem to provide similar Controls. If you want to design similar Controls, refer ~ :
Spinner. qml:
import QtQuick 2.0Rectangle { width: 64 height: 64 property alias value: list.currentIndex property alias label: caption.text Text { id: caption text: "Spinner" anchors.horizontalCenter: parent.horizontalCenter } Rectangle { anchors.top: caption.bottom anchors.topMargin: 4 anchors.horizontalCenter: parent.horizontalCenter height: 48 width: 32 color: "black" ListView { id: list anchors.fill: parent highlightRangeMode: ListView.StrictlyEnforceRange preferredHighlightBegin: height/3 preferredHighlightEnd: height/3 clip: true model: 64 delegate: Text { font.pixelSize: 18; color: "white"; text: index; anchors.horizontalCenter: parent.horizontalCenter } } Rectangle { anchors.fill: parent gradient: Gradient { GradientStop { position: 0.0; color: "#FF000000" } GradientStop { position: 0.2; color: "#00000000" } GradientStop { position: 0.8; color: "#00000000" } GradientStop { position: 1.0; color: "#FF000000" } } } }}
We recommend qt5 books on qt quick.
Go to qt-project.org to see Official Information
Qt502 for windows 32bit (mingw 47) qt502 for windows 32bit (vs 2010) is different?
Qt5.0.2 for windows 32bit (mingw 4.7) is a binary dynamic link library version compiled using the mingw32 Environment + gcc4.7.
Qt5.0.2 for windows 32bit (vs 2010) is the version for VS2010 generated using VS2010 configuration and windows sdk7.0 compilation tool.
Mingw4.7 comes with the compiler. In vs2010, VS2010 must be installed. In fact, you can install windowssdk without vs, but you need a configuration tool set that replaces vs2010.
What level of your C ++? If you are not a newbie, I suggest you check the configuration items, that is, whether the qt path is configured in the compiling environment configuration of qtcreator.
If you are a newbie, I advise you to give up qt for the time being. You do not have enough C ++ skills and it is very difficult to use Qt.