QML understands

QT Quick is an advanced user interface toolkit provided by QT, including perfect support for QML, QTQuick designer, and mixed programming technology of QML and C++. Qt Quick is a set of class libraries built using QML.

 

The Qml module itself does not involve graphics display, and all graphics processing is done by the Qt Quick module.

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

QMl is an efficient language for developing UI. QML (Qt Meta-Object Language, Qt Meta Object Language) is a declarative programming language, and it is an integral part of the Qt framework. The main function of QML is to allow developers to quickly and easily develop user interfaces, which include desktop applications, mobile devices, and embedded interfaces. Moreover, QML can also be seamlessly integrated with JavaScript to develop and use, that is, JavaScript files can be used directly in QML code.

# view.qml
import QtQuick 2.0

Rectangle {
    width: 200
    height: 200
    color: "green"

    Text {
        text: "Hello World"
        anchors.centerIn: parent
    }
}

Create a QML project

I just want to test QML-related content and hope to quickly display the interface effect. At this time, I can create a Qt Quick Ul project. The Qt QuickUl project only contains QML and JavaScript code, without adding any C++ code.

 

Create a Qt Quick project

 

// main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
 
Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
}

Every QML has one and only one root element, similar to XML documents. The root element is the QML element defined in the QML document, in this case a Window object.

#include <QtQml>
import QtQml 2.14

qmake .pro document:

QT += qml

QML Basic Types

QML object types are objects that have properties, signals, methods, etc.

  • bool:true/false
Item {
      focus: true
      clip: false
  }
  • double
Item {
      property double number: 32155.2355
  }
  • enumeration:<Type>.<value>
Text { horizontalAlignment: Text.AlignRight }
  • int:e.g. 0, 10, or -20。
Item { width: 100; height: 200 }
  • list
import QtQuick 2.0

  Item {
      width: 100; height: 100

      states: [
          State { name: "activated" },
          State { name: "deactivated" }
      ]

      Component.onCompleted: {
          console.log("Name of first state:", states[0].name)
          for (var i = 0; i < states.length; i++)
              console.log("state", i, states[i].name)
      }
  }

If the list contains only one object, the square brackets can be omitted:

import QtQuick 2.0

  Item {
      width: 100; height: 100
      states: State { name: "activated" }
  }
  • real : a number with a decimal point
Item { width: 100.45; height: 150.82 }
  • string
Text { text: "Hello world!" }
  • url
Image { source: "pics/logo.png" }
Image {
      source: "pics/logo.png"

      Component.onCompleted: {
          // This prints 'false'. Although "pics/logo.png" was the input string,
          // it's been converted from a string to a URL, so these two are not the same.
          console.log(source == "pics/logo.png")

          // This prints 'true' as Qt.resovledUrl() converts the string into a
          // URL with the correctly resolved path
          console.log(source == Qt.resolvedUrl("pics/logo.png"))

          // This prints the absolute path, e.g. "file:///path/to/pics/logo.png"
          console.log(source.toString())
      }
  }
  • var : A generic property type that can refer to any data type.
Item {
      property var aNumber: 100
      property var aBool: false
      property var aString: "Hello world!"
      property var anotherString: String("#FF008800")
      property var aColor: Qt.rgba(0.2, 0.3, 0.4, 0.5)
      property var aRect: Qt.rect(10, 10, 10, 10)
      property var aPoint: Qt.point(10, 10)
      property var aSize: Qt.size(10, 10)
      property var aVector3d: Qt.vector3d(100, 100, 100)
      property var anArray: [1, 2, 3, "four", "five", (function() { return "six"; })]
      property var anObject: { "foo": 10, "bar": 20 }
      property var aFunction: (function() { return "one"; })
  }
  • date:“YYYY-MM-DDThh:mm:ss.zzzZ”
MyDatePicker { minDate: "2000-01-01 0:0"; maxDate: "2020-12-31 23:59" }
  • point : describe the x and y attributes, "x,y"
CustomObject { myPointProperty: "0,20" }
CustomObject { myPointProperty: Qt.point(0, 20) }
  • rect:x, y, width and height。“x, y, width x height”
CustomObject { myRectProperty: "50,50,100x100" }
CustomObject { myRectProperty: Qt.rect(50, 50, 100, 100) }
Rectangle {
      width: childrenRect.width
      height: childrenRect.height

      Rectangle { width: 100; height: 100 }
  }
  • size:width和height。“width x height”
Image { sourceSize: "150x50" }
Image { sourceSize: Qt.size(150, 50) }
Column {
      Image { id: image; source: "logo.png" }
      Text { text: image.sourceSize.width + "," + image.sourceSize.height }
  }

QML object types

  • Component: (Component) A component is a reusable, encapsulated QML type with a well-defined interface.
  • QtObject
  • Binding
  • Connections
  • Timer

quick component

Such as the table is a component provided by Qt Quick Controls 1.1

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓ 

Guess you like

Origin blog.csdn.net/m0_73443478/article/details/131705202