c++ - 如何处理黑莓10中自定义控件的触摸事件

标签 c++ qml blackberry-10 blackberry-cascades touch-event

我有一个名为 customContextMenu.qml 的自定义控件,它的每行都有一个图像和标签。我想一起处理该图像和标签上的点击/触摸。我该怎么做?

目前,我在每个包含图像和标签的容器中添加了 onTouch() ,但它不起作用。我在我的 main.qml 中使用 customContextMenu 控件。

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();
              }
            }
          }
        }
      }
    ]
  }
}

最佳答案

您需要对 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.
    }
}

关于c++ - 如何处理黑莓10中自定义控件的触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18440489/

相关文章:

c++ - native c++ 编译器和 .net c++ 编译器有什么区别?

c++ - 如何将指针 vector 转换为 C++ 中的链表?

c++ - 为什么 msgrcv() 将垃圾字符输入缓冲区?

android - 无法在带有 Qt 5.2 的 Samsung Galaxy Tab 3 7"上运行标准的 Hello World Qt Quick 2.0 项目

c++ - 我在C++中创建可除数为30的数字的函数

c++ - Qt/QML : How do I "switch to" or "open" another QML file without restarting my app?

c++ - 使用别名和 C++ 集成在 .qml 中实现 .ui 文件的逻辑

android - 我可以将 BlackBerry inApp 支付集成到我的 android 应用程序中吗?

c++ - 黑莓 10 : How to correspond incoming http replies to their previous http request counterparts?

android - 在 Blackberry 上使用带有 native 代码的 Android 应用程序