qt5学习笔记

qml:学习笔记
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
设置支持高分辨率。
The QCoreApplication class provides an event loop for Qt applications without UI。

engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
The macro generates the data for a QString out of str at compile time if the compiler supports it. Creating a QString from it is free in this case, and the generated string data is stored in the read-only segment of the compiled object file.

The QUrl class provides a convenient interface for working with URLs.
Rectangle
Paints a filled rectangle with an optional border
Image:: asynchronous
Setting asynchronous to true is useful where maintaining a responsive user interface is more desirable than having images immediately visible.
import QtQuick 2.0 as Quick

import QmlProject 1.1
BusyIndicator
A busy indicator.

Provides a menu component for use as a context menu, popup menu, or as part of a menu bar.

ExclusiveGroup provides a way to declare several checkable controls as mutually exclusive.
import "../mycomponents"
Column is a type that positions its child items along a single column. It can be used as a convenient way to vertically
Slider
Used to select a value by sliding a handle along a track
C:\android\android-ndk-r10e\toolchains\arm-linux-androideabi-4.9\prebuilt\windows-x86_64\bin\arm-linux-androideabi-gdb.exe

Specifies how to add formatted text to a scene

 Keys.onDigit1Pressed: font.pixelSize += 1
    Keys.onDigit2Pressed: font.italic = !font.italic
    Keys.onDigit3Pressed: font = otherText.font

Item {
    property var theArray: []
    property var theDate: new Date()
    Component.onCompleted: {
        for (var i = 0; i < 10; i++)
            theArray.push("Item " + i)
        console.log("There are", theArray.length, "items in the array")
        console.log("The time is", theDate.toUTCString())
    }
}
 
Rectangle {
    property color previousColor
    property color nextColor
    onNextColorChanged: console.log("The next color will be: " + nextColor.toString())
    nextColor: "red"
    color: nextColor
    MouseArea {
        anchors.fill: parent
        onClicked: nextColor = "yellow"
    }
}
Rectangle {
    // 只声明,不初始化
    property list<Rectangle> siblingRects
    // 声明并且初始化
    property list<Rectangle> childRects: [
        Rectangle { color: "red" },
        Rectangle { color: "blue"}
    ]
    MouseArea {
        anchors.fill:parent
        onClicked: {
            for (var i = 0; i < childRects.length; i++)
                console.log("color", i, childRects[i].color)
        }
    }
}
// 内部函数访问内部属性
    function setInternalColor() {
        color = "#111111"
    }

Text {
    //声明一个默认属性
    default property var someText
    text: "Hello, " + someText.text
    color: "red"
}

 默认属性则可以直接书写,去掉方括号,在写重用的QML组件式比较有用,例如将一个QmL外部资源封装好,内部具体的item,有子对象去填充
 rect.color = Qt.rgba(Math.random(), Math.random(),Math.random(),1);

   signal activated(real xPosition, real yPosition)
    signal deactivated
Connections QML Type
Describes generalized connections to signals

Rectangle {
    id: relay
    signal messageReceived(string person, string notice)
    Component.onCompleted: {
        relay.messageReceived.connect(sendToPost)
        relay.messageReceived.connect(sendToTelegraph)
        relay.messageReceived.connect(sendToEmail)
        relay.messageReceived("Tom", "Happy Birthday")
    }
    function sendToPost(person, notice) {
        console.log("Sending to post: " + person + ", " + notice)
    }
    function sendToTelegraph(person, notice) {
        console.log("Sending to telegraph: " + person + ", " + notice)
    }
    function sendToEmail(person, notice) {
        console.log("Sending to email: " + person + ", " + notice)
    }
}

 signal send()
    onSend: console.log("Send clicked")
 
ListView {
     anchors.fill: parent
     model: fruitModel

     Component.onCompleted: {
         fruitModel.append(
               [{"name":"spikes","value":"7mm"},
                {"name":"color","value":"green"}]);
           fruitModel.get(0).attributes.get(1).value; // == "green"
      }
     Timer {
                 id: timer
                 interval: 2000; repeat: true
                 running: true
               //  When a timer is started, the first trigger is usually after the specified interval has elapsed.
                 triggeredOnStart: true
                 onTriggered: {
                     fruitModel.append({"cost": 5.95, "name":"Jackfruit"});
                     console.log(fruitModel.get(0).cost);
                     fruitModel.get(0).cost = 10.95;
                 }
             }
     ListModel {
          id: fruitModel
          ListElement {
              name: "Apple"
              cost: 2.45
          }
      }
 
     delegate: Row {
         Text { text: "Fruit: " + name }
         Text { text: "Cost: $" + cost }
     }
 }

 ListView {
    width: 240; height: 320
    model: ListModel {
        id: listModel
        Component.onCompleted: {
            for (var i = 0; i < 10; i++)
                listModel.append({"Name": "Item " + i})
        }
    }
    delegate: Text { text: index + "  " + Name }
}

  Keys.onSpacePressed: height = width * 3

  The Qt object is a global object with utility functions, properties and enums.

import "factorial.js" as MathFunctions
import "script.js" as MyScript
 Component.onCompleted: {
        mouseArea.clicked.connect(MyScript.jsFunction)
    }
posted @ 2019-10-28 08:17  countryboy666  阅读(236)  评论(0编辑  收藏  举报