QML学习:Rectangle,Text,TextEdit,Flickable,Flipable元素
本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.
参考文档<<Qt及Qt Quick开发实战精解.pdf>>
环境:
主机:WIN7
开发环境:Qt
Rectangle元素:
代码:
import QtQuick 2.0
Item
{
Rectangle
{
color: "blue"
width: 50
height: 50
border.color: "green"
border.width: 10
radius: 20
}
}
运行效果:

说明:
border属性设置边框颜色和宽度
radius设置四角圆角的半径
Text元素:
代码:
import QtQuick 2.0
Item
{
Rectangle
{
color: "blue"
width: 50
height: 50
border.color: "green"
border.width: 10
radius: 20
}
Text
{
//文本
text: "Hello JDH!"
//字体
font.family: "Helvetica"
//字大小
font.pointSize: 24
//颜色
color: "red"
}
}
运行效果:

TextEdit元素:
代码:
import QtQuick 2.0
Item
{
Rectangle
{
color: "blue"
width: 50
height: 50
border.color: "green"
border.width: 10
radius: 20
}
Text
{
//文本
text: "Hello JDH!"
//字体
font.family: "Helvetica"
//字大小
font.pointSize: 24
//颜色
color: "red"
}
TextEdit
{
width: 240
text: "This is TextEdit"
font.pointSize: 10
focus: true
x : 20
y : 40
}
}
运行效果:

说明:
focus属性设置焦点为输入框.
Flickable元素:
它可以将子元素设置在一个可以拖拽和弹动的界面上,使得子项目的视图可以滚动.
比如一张大图片,窗口显示不全,则可以用拖动它查看不同的部分.
代码1:
import QtQuick 2.0
Flickable
{
id: flick
width: 300
height: 200
//可拖拽内容大小
contentWidth: image.width
contentHeight: image.height
Image
{
id: image
source: "pics/1.jpg"
}
}
代码2:
利用clip属性,将大于Flickable窗口的部分隐藏.
图片可被拖动,用来显示未显示的部分.
import QtQuick 2.0
Rectangle
{
width: 480
height: 320
color: "blue"
Flickable
{
id: flick
width: 300
height: 200
//可拖拽内容大小
contentWidth: image.width
contentHeight: image.height
//隐藏大于显示窗口的部分
clip: true;
Image
{
id: image
source: "pics/1.jpg"
}
}
}
运行效果:

代码3:
实现滚动条功能:
import QtQuick 2.0
Rectangle
{
width: 480
height: 320
color: "blue"
Flickable
{
id: flick
width: 300
height: 200
//可拖拽内容大小
contentWidth: image.width
contentHeight: image.height
//隐藏大于显示窗口的部分
clip: true;
Image
{
id: image
source: "pics/1.jpg"
}
}
//竖滚动条
Rectangle
{
id: scrollbar_vertical
anchors.right: flick.right
//当前可视区域的位置.为百分比值,0-1之间
y: flick.visibleArea.yPosition * flick.height
width: 10
//当前可视区域的高度比例,为百分比值,0-1之间
height: flick.visibleArea.heightRatio * flick.height
color: "black"
}
//横滚动条
Rectangle
{
id: scrollbar_horizontal
anchors.bottom: flick.bottom
//当前可视区域的位置.为百分比值,0-1之间
x: flick.visibleArea.xPosition * flick.width
height: 10
//当前可视区域的宽度比例,为百分比值,0-1之间
width: flick.visibleArea.widthRatio * flick.width
color: "black"
}
}
运行效果:

Flipable元素:
可以实现翻转效果
代码:
import QtQuick 2.0
Flipable
{
id: flip
width: 300
height: 200
//定义属性
property bool flipped: false
//正面图片
front:Image
{
source: "pics/1.jpg"
anchors.centerIn: parent
}
//背面图片
back:Image
{
source: "pics/2.jpg"
anchors.centerIn: parent
}
//旋转设置,延Y轴旋转
transform: Rotation
{
id: rotation
origin.x:flip.width / 2
origin.y:flip.height / 2
axis.x: 0
axis.y: 1
axis.z: 0
angle: 0
}
//状态改变
states:State
{
name: "back"
PropertyChanges
{
target: rotation;angle:180
}
when:flip.flipped
}
//转换方式
transitions: Transition
{
NumberAnimation
{
target:rotation
properties: "angle"
duration:4000
}
}
//鼠标区域
MouseArea
{
anchors.fill: parent
onClicked: flip.flipped = !flip.flipped
}
}
效果图:
正面: 背面:


分享到:
相关推荐
在QML(Qt Quick)中,`QmlTextEdit` 是一个用于显示和编辑文本的组件。这个`QmlTextEditScrollBar`可能是一个自定义实现,它为`QmlTextEdit`添加了滚动条功能,因为默认的`QmlTextEdit`在文本内容过多时可能不会...
Window { visible: true width: 640 height: 480 title: qsTr(Hello World) Rectangle{ id:rectangle ... text: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
本篇文章将聚焦于QML(Qt Meta Language)小程序中的输入元素,探讨如何创建和管理自定义组件,尤其是行编辑框(LineEdit)和文本编辑框(TextEdit),以及它们在用户交互过程中的焦点切换。 QML是Qt框架的一部分,...
### QML元素详细介绍 #### 一、QML概述 QML(Qt Meta Language)是一种用于构建用户界面的声明性语言,它与JavaScript紧密结合,能够轻松地实现动态效果和复杂的交互逻辑。QML的设计理念旨在简化界面开发过程,...
QML学习文档 QML(Qt Meta Language or Qt Modeling Language)是一种描述语言,主要用于描述用户界面效果。它可以与JavaScript结合使用,以实现更复杂的效果和逻辑实现。在QML中,基本的可视化项是Item,它是所有...
- **可视元素介绍**:QML提供了丰富的可视元素,包括但不限于矩形(Rectangle)、文本(Text)、图像(Image)等。这些元素构成了QML用户界面的基础。 - **示例**:在上面的程序中,`Window`和`MainForm`是可视元素的例子...
- 为了将文本居中显示,我们使用`anchors.centerIn: parent`将`Text`元素的中心锚定在其父元素(即`Rectangle`)的中心。 2. **实现菜单栏**: - 在构建更复杂的用户界面时,菜单栏是必不可少的。在QML中,可以...
3. **创建文本编辑器**:要构建一个文本编辑器,我们需要集成一个可编辑的文本区域,这可能涉及到使用`TextEdit`或`TextInput`元素。这些元素提供了文本输入和编辑的功能,可以与QML的其他元素交互。 4. **美化文本...
1. **基础元素的使用:**如Rectangle、Text、Image等,用于构建基本的UI元素。 2. **状态机和行为:**利用State和Transition,实现UI元素在不同状态间的平滑过渡。 3. **数据绑定:**通过Binding,将属性值与计算...
博客了解QML控件元素(一)课件,包含Window、Rectangle、Button、Text/Label、TextEdit控件的简要使用说明:博客地址:https://blog.csdn.net/zeor0/article/details/108703499
1. **Item布局**:使用QML的`Rectangle`或`Item`作为基础元素,定义表格的边界和背景色。然后,使用`Column`和`Row`布局管理器组织单元格。 2. **Repeater**:为了动态生成表格行和列,我们可以利用`Repeater`组件...
1. **BasicShapes**: 展示了基本图形元素的用法,如Rectangle、Ellipse等,以及如何调整其样式和属性。 2. **Controls**: 展示了各种内置控件的使用,如Button、TextEdit、ListView等,以及自定义控件的方法。 3. **...