我有一个名为customContextMenu.qml
的自定义控件,每行都有一个图像和标签。我想处理该图像上的点击/触摸并一起标记。我该怎么做?
目前,我在包含图片和标签的每个容器中添加onTouch()
,但它不起作用。我在customContextMenu
中使用main.qml
控件。
Container {
id: testPageCustomMBoxContainer
objectName: "testPageCustomMBoxContainer"
background:dropdownBack.imagePaint
attachedObjects: [
ImagePaintDefinition {
id: dropdownBack
repeatPattern: RepeatPattern.Fill
imageSource: "asset:///images/dropdown_bg.png"
}
]
layout: StackLayout {
orientation: LayoutOrientation.TopToBottom
}
maxWidth: 200.0
maxHeight: 180.0
horizontalAlignment: HorizontalAlignment.Right
verticalAlignment: VerticalAlignment.Top
Container {
id: testPageCustomMBoxRetestContainer
objectName: "testPageCustomMBoxRetestContainer"
preferredWidth:200.0
preferredHeight:90.0
layout: StackLayout {
orientation: LayoutOrientation.LeftToRight
}
leftPadding: 10.0
topPadding: 10.0
bottomPadding: 10.0
rightPadding: 20.0
ImageView {
imageSource: "asset:///images/retest.png"
scalingMethod: ScalingMethod.AspectFit
verticalAlignment: VerticalAlignment.Center
layoutProperties: StackLayoutProperties {
spaceQuota: 1.0
}
}
Label {
text: "Retest"
horizontalAlignment: HorizontalAlignment.Right
verticalAlignment: VerticalAlignment.Center
textFormat: TextFormat.Plain
layoutProperties: StackLayoutProperties {
spaceQuota: 3.0
}
}
onTouch: {
var retestPage = retestPage.createObject();
testNavigationPane.push(retestPage);
}
attachedObjects: [
ComponentDefinition {
id:retestPage
source: "main.qml"
}
]
}
Container {
id: testPageCustomMBoxHelpContainer
objectName: "testPageCustomMBoxHelpContainer"
preferredWidth: 200.0
preferredHeight: 90.0
layout: StackLayout {
orientation: LayoutOrientation.LeftToRight
}
leftPadding: 10.0
topPadding: 5.0
bottomPadding: 15.0
rightPadding: 20.0
ImageView {
imageSource: "asset:///images/help.png"
scalingMethod: ScalingMethod.AspectFit
verticalAlignment: VerticalAlignment.Center
layoutProperties: StackLayoutProperties {
spaceQuota: 1.0
}
}
Label {
text: "Help"
horizontalAlignment: HorizontalAlignment.Right
verticalAlignment: VerticalAlignment.Center
textFormat: TextFormat.Plain
layoutProperties: StackLayoutProperties {
spaceQuota: 3.0
}
onTouch: {
var helpPage = helpPage.createObject();
testNavigationPane.push(helpPage);
}
attachedObjects: [
ComponentDefinition {
id: helpPage
source: "helpPage.qml"
Page {
paneProperties: NavigationPaneProperties {
backButton: ActionItem {
onTriggered: {
testNavigationPane.pop();
}
}
}
}
}
]
}
}
答案 0 :(得分:2)
您需要对Touched或LongPressed等内容使用手势处理程序。只有在用户手指向下或类似的情况下尝试执行某些操作时,才会使用onTouch事件。以下是显示两者之间差异的代码示例:
//Touched event - only fires if user touches and lets go of element.
//This is the preferred method
gestureHandlers: [
TapHandler {
onTapped: {
//Code goes here
}
}
]
//Display active image if touched
//This would be used for something such as changing a background color of a button when pressed.
onTouch: {
if (event.touchType == TouchType.Down) {
//Do something on down touch - e.g. change background color
} else if (event.touchType == TouchType.Up || event.touchType == TouchType.Cancel) {
//Users finger has been lifted OR has left the element.
}
}