鱼C论坛

 找回密码
 立即注册

用QML(QT QUICK)做动画(一)

热度 2已有 1563 次阅读2017-2-20 11:10

首先打开qt,创建一个qt quick项目,如图(因为论坛暂时图片没法上传就算了...)

然后在资源里的那个斜杠下面添加新文件,添加一个quick 2类型的文件

以下是我用几行简单的代码就可以写出一个能用上下左右键控制移动的矩形

main.qml文件:

import QtQuick 2.5

import QtQuick.Window 2.2

Window {//对象

visible: true//属性

width: 640

height: 480

title: qsTr("Hello World")

MainForm {//子对象

anchors.fill: parent

/*鼠标点击事件*/

mouseArea.onClicked: {

rect.visible=!rect.visible//设定id是rect的项目隐藏

}

}

}

下面是MainForm.ui.qml文件:

import QtQuick 2.5

Rectangle {

property alias mouseArea: mouseArea

property alias rect: rect

width: 360

height: 360


//

MouseArea {

id: mouseArea

anchors.fill: parent

}

Rect{

id:rect

x:25;y:25

opacity: 0.5//设置透明度

}

}

Rect.qml这就是我新建的那个文件:

import QtQuick 2.0
Item {

    Rectangle{

    width:20

height:20

color:"red"

    focus:true//只有设定了这个才能使用键盘控制 Keys.onUpPressed: y-=10 Keys.onDownPressed: y+=10 Keys.onLeftPressed: x-=10 Keys.onRightPressed: x+=10
}

}

注:用鼠标点击窗口内,那个小矩形还可以隐藏




路过

鸡蛋
2

鲜花

握手

雷人

刚表态过的朋友 (2 人)

发表评论 评论 (2 个评论)

回复 不二如是 2017-2-20 11:26
去Qt模块,发帖吧~

帮我补齐,动画类的缺角吧!
回复 ~风介~ 2017-2-20 23:51
Qml是一个大坑。

facelist

您需要登录后才可以评论 登录 | 立即注册

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 09:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

返回顶部