热度 2
首先打开qt,创建一个qt quick项目,如图(因为论坛暂时图片没法上传就算了...)
然后在资源里的那个斜杠下面添加新文件,添加一个quick 2类型的文件
以下是我用几行简单的代码就可以写出一个能用上下左右键控制移动的矩形
main.qml文件:
import QtQuick 2.5
import QtQuick.Window 2.2Window {//对象
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
}
}
注:用鼠标点击窗口内,那个小矩形还可以隐藏
小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)
GMT+8, 2024-3-29 09:14
Powered by Discuz! X3.4
© 2001-2023 Discuz! Team.