Qt 属性别名自引用?这个QML代码在做什么?
所以我正试图让Malit框架和键盘插件在windows上运行。可以找到原始来源,并将其删除 我对它有很多不同的问题,但是我这里的问题是关于在下面的QML代码中使用QML属性Qt 属性别名自引用?这个QML代码在做什么?,qt,qml,qtquick2,Qt,Qml,Qtquick2,所以我正试图让Malit框架和键盘插件在windows上运行。可以找到原始来源,并将其删除 我对它有很多不同的问题,但是我这里的问题是关于在下面的QML代码中使用QML属性别名。在我看来,标题别名指的是键盘标题。文本,但同时键盘标题的文本被设置为别名标题?对我来说似乎是循环的。这里到底发生了什么 /* * This file is part of Maliit plugins * * Copyright (C) 2012-2013 Canonical Ltd * * Contact:
别名。在我看来,标题
别名
指的是键盘标题。文本
,但同时键盘标题
的文本
被设置为别名
标题
?对我来说似乎是循环的。这里到底发生了什么
/*
* This file is part of Maliit plugins
*
* Copyright (C) 2012-2013 Canonical Ltd
*
* Contact: maliit-discuss@lists.maliit.org
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
* Neither the name of Nokia Corporation nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
import QtQuick 2.5
Item {
property alias layout: main.model
property variant event_handler
property bool area_enabled // MouseArea has no id property so we cannot alias its enabled property.
property alias title: keyboard_title.text
width: layout.width
height: layout.height
visible: layout.visible
Connections {
target: layout
onTitleChanged: {
console.debug("title:" + layout.title)
title_timeout.start()
}
}
BorderImage {
id: background
anchors.fill: parent
source: layout.background
border.left: layout.background_borders.x
border.top: layout.background_borders.y
border.right: layout.background_borders.width
border.bottom: layout.background_borders.height
}
Repeater {
id: main
model: layout
anchors.fill: parent
Item {
x: key_reactive_area.x
y: key_reactive_area.y
width: key_reactive_area.width
height: key_reactive_area.height
BorderImage {
x: key_rectangle.x
y: key_rectangle.y
width: key_rectangle.width
height: key_rectangle.height
border.left: key_background_borders.x
border.top: key_background_borders.y
border.right: key_background_borders.width
border.bottom: key_background_borders.height
source: key_background
Text {
anchors.fill: parent
text: key_text
font.family: key_font
font.pointSize: key_font_size
color: key_font_color
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
visible: (key_text.length != 0)
}
Image {
anchors.centerIn: parent
source: key_icon
visible: (key_icon.length != 0)
}
}
MouseArea {
property real start_x
property real start_y
Timer {
id: gesture_timeout
interval: 500
}
enabled: area_enabled
anchors.fill: parent
hoverEnabled: true
onEntered: event_handler.onEntered(index)
onExited: event_handler.onExited(index)
onPressed: {
start_x = mouse.x
start_y = mouse.y
gesture_timeout.start()
event_handler.onPressed(index)
}
onReleased: event_handler.onReleased(index)
onPressAndHold: event_handler.onPressAndHold(index)
// TODO: Move logic into EventHandler because gestures should depend on style?
// Hide keyboard on flick-down gesture (but only if there is an event_handler)
// or switch to left/right layout:
onPositionChanged: {
if (event_handler
&& gesture_timeout.running
&& (mouse.y - start_y > (layout.height * 0.3))) {
maliit.hide()
} else if (event_handler
&& gesture_timeout.running
&& (mouse.x - start_x > (layout.width * 0.2))) {
maliit.selectLeftLayout()
} else if (event_handler
&& gesture_timeout.running
&& (start_x - mouse.x > (layout.width * 0.2))) {
maliit.selectRightLayout()
}
}
}
}
}
// Keyboard title rendering
// TODO: Make separate component?
Item {
anchors.centerIn: parent
opacity: title_timeout.running ? 1.0 : 0.0
Behavior on opacity {
PropertyAnimation {
duration: 300
easing.type: Easing.InOutQuad
}
}
Timer {
id: title_timeout
interval: 1000
}
// TODO: Make title background part of styling profile.
BorderImage {
anchors.centerIn: parent
// Manual padding of text:
width: keyboard_title.width * 1.2
height: keyboard_title.height * 1.2
//anchors.fill: keyboard_title
source: layout.background
z: 1000 // Move behind Text element but in front of rest.
border.left: layout.background_borders.x
border.top: layout.background_borders.y
border.right: layout.background_borders.width
border.bottom: layout.background_borders.height
}
Text {
id: keyboard_title
anchors.centerIn: parent
text: title;
z: 1001
// TODO: Make title font part of styling profile.
font.pointSize: 48
color: "white"
}
}
}
它用于将属性公开给另一个QML:
Keyboard {
layout: maliit_layout
event_handler: maliit_event_handler
area_enabled: !maliit_extended_layout.visible
title: maliit_layout.title
}
这允许与另一个文件中的键盘\u title.text
交互,并创建可读的自定义组件
是的,有一个循环,如果设置title
,则设置keyboard\u title.text
,如果设置keyboard\u title.text
,则设置title。很好
注意:
我用别名大量使用C++代码。它允许在不找到子对象的情况下更改其属性,这可能是真正的快捷方式。在我看来,代码最初类似于:
import QtQuick 2.5
Item {
property alias layout: main.model
property variant event_handler
property bool area_enabled
property string title
...
Item
{
...
Text {
id: keyboard_title
anchors.centerIn: parent
text: title;
...
}
}
}
然后,写这篇文章的人认为“嘿,使用别名而不是创建新的字符串属性可能是个好主意!”并继续编写当前代码,但忘记删除行text:title
,当然可以,但实际上没有用。设置title
时,您正在设置keyboard\u title.text
,反之亦然。因为文件很长,所以它可以强调相关性。一个评论就足够了。我认为这只是一个干净的方法。所以在这个例子中,文本:title;行为与文本相同:,也就是说它没有初始值?但是,是否有必要让读者(fx me)清楚地知道,它是通过title属性别名设置的?确切地说<调用组件时,将在更高级别上设置代码>标题
。请注意,我在回答中输入的代码来自malit-keyboard.qml文件,该文件位于malit github repo的同一文件夹中,该代码将标题设置为malit\u layout.title
。因此,实际上它直接将keyboard\u title.text
设置为malit\u布局。title
如果没有text:title代码>在键盘标题中
。OP询问的是这句话的用处,而不是别名。