QML中基础控件有哪些以及对应的示例代码

您所在的位置:网站首页 qt中的控件 QML中基础控件有哪些以及对应的示例代码

QML中基础控件有哪些以及对应的示例代码

#QML中基础控件有哪些以及对应的示例代码| 来源: 网络整理| 查看: 265

在 QML 中,常用的基础控件包括:

1、Text(文本):用于显示单行或多行文本。

import QtQuick 2.0 Text { text: "Hello, World!" font.pixelSize: 24 color: "red" x: 10 y: 10 }

2、Rectangle(矩形):用于绘制矩形。

import QtQuick 2.0 Rectangle { width: 100 height: 100 color: "red" x: 10 y: 10 }

3、Image(图片):用于显示图片。

import QtQuick 2.0 Image { source: "image.png" width: 100 height: 100 x: 10 y: 10 }

4、Button(按钮):用于触发事件。

import QtQuick 2.0 Button { text: "Click me" onClicked: console.log("Button clicked") x: 10 y: 10 }

5、TextField(文本框):用于输入和显示单行文本。

import QtQuick 2.0 TextField { text: "Input text here" width: 200 height: 30 x: 10 y: 10 }

6、TextArea(多行文本框):用于输入和显示多行文本。

import QtQuick 2.0 TextArea { text: "Input text here" width: 200 height: 100 x: 10 y: 10 }

7、ComboBox(下拉框):用于选择一个选项的下拉框。

import QtQuick 2.0 ComboBox { model: ["Option 1", "Option 2"] currentIndex: 0 width: 100 height: 30 x: 10 y: 10 }

8、Slider(滑动条):用于选择一个范围内的数值的一个滑动条。

import QtQuick 2.0 Slider { value: 50 width: 100 height: 30 x: 10 y: 10 }

9、ListView

ListView是一个高性能的组件,用于显示大量数据的列表。它会根据提供的模型数据自动生成视图项,并支持滚动、动画和键盘导航等功能。

示例代码:

ListModel { id: fruitModel ListElement { name: "Apple" } ListElement { name: "Banana" } ListElement { name: "Cherry" } ListElement { name: "Date" } } ListView { anchors.fill: parent model: fruitModel delegate: Text { text: name font.pointSize: 24 color: "black" padding: 10 } }

10、GridView

GridView是另一个高性能的组件,用于显示大量数据的网格布局。它会自动生成视图项,并支持滚动、动画和键盘导航等功能。

示例代码:

GridView { width: 200 height: 200 cellWidth: 50 cellHeight: 50 model: 25 delegate: Rectangle { width: 50 height: 50 color: "green" border.color: "black" Text { text: index + 1 color: "white" anchors.centerIn: parent } } }

11、TabView

TabView是一个用于显示多个标签页的组件。每个标签页可以包含任意数量的子控件。

示例代码:

TabView { width: 300 height: 200 Tab { title: "Tab 1" Rectangle { color: "red" anchors.fill: parent } } Tab { title: "Tab 2" Rectangle { color: "green" anchors.fill: parent } } Tab { title: "Tab 3" Rectangle { color: "blue" anchors.fill: parent } } }

12、Repeater

Repeater是一个非常有用的组件,可以重复地创建其他QML元素。它包含一个模板项,它会被实例化多次,每次实例化会产生一个独立的对象。

示例代码:

Column { Repeater { model: 5 Rectangle { height: 20 width: 50 color: "green" border.color: "black" Text { text: index + 1 color: "white" anchors.centerIn: parent } } } }

13、GridView

GridView是另一个高性能的组件,用于显示大量数据的网格布局。它会自动生成视图项,并支持滚动、动画和键盘导航等功能。

示例代码:

GridView { width: 200 height: 200 cellWidth: 50 cellHeight: 50 model: 25 delegate: Rectangle { width: 50 height: 50 color: "green" border.color: "black" Text { text: index + 1 color: "white" anchors.centerIn: parent } } }

14、TabView

TabView是一个用于显示多个标签页的组件。每个标签页可以包含任意数量的子控件。

示例代码:

TabView { width: 300 height: 200 Tab { title: "Tab 1" Rectangle { color: "red" anchors.fill: parent } } Tab { title: "Tab 2" Rectangle { color: "green" anchors.fill: parent } } Tab { title: "Tab 3" Rectangle { color: "blue" anchors.fill: parent } } }

以上是一些常用的QML基础控件及示例代码,可以作为学习和使用的参考。当然,QML还有许多其他的高级控件和组件,例如Canvas、Path、ShaderEffect等,也非常值得学习和掌握。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3