javascript - 无法将 QQuickText 分配给 QString

标签 javascript qml qstring

我正在尝试在 qml-javascript 中动态创建对象。创建对象的函数是:

function createSpriteObjects(xPos,yPos,cnt,imgsrc,jsonObject) {
    var title;
    title = jsonObject.matches[cnt].recipeName;
    var component = Qt.createComponent("FlickItem.qml");
    component.createObject(wall.contentItem, {"color":"white", "x":xPos,"y":yPos,"src":imgsrc,"opacity":1,"title":title});
}

接收文件(FlickItem.qml)有一个属性字符串标题,稍后分配给文本项的文本字段:

import QtQuick 2.0

Rectangle {
id: name
opacity: 0
property string title: ""
width:100
height:100
color:"white"

property string src: ""

Image {
   id:recipeimages
   source: src
   width:240
   height:160
}
Text {
   id: title
   anchors.bottom: recipeimages.bottom
   anchors.horizontalCenter: recipeimages.horizontalCenter
   anchors.bottomMargin: 5
   color: "white"
   font.pixelSize: 24
   text:title
}

返回以下错误:

unable to assign QQuickText to QString

有什么办法解决这个问题吗?

最佳答案

Rectangle {
id: name
//...
property string title: ""  <------ title
//...
}

Text {
   //...
   id: title   <----- title
   //...
   text:title    <---- which title ?!?!?!
}

问题是您有一个 property 绑定(bind)到 title 标识符,还有一个 id 绑定(bind)到 title 标识符。根据您的输出,您可能正在尝试从 id(而不是 property)分配文本。

更改 Text 组件的 id 应该可以解决您的问题。

关于javascript - 无法将 QQuickText 分配给 QString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15890871/

相关文章:

javascript - 如何 'unrender' MathJax方程?

javascript - 使用 for 循环而不是为每个元素创建新的 var

c++ - 从 C++ 访问现有 QML 组件

c++ - QString,去掉标签和内容?

c++ - 显示带有多个参数的 QMessageBox

javascript - 如何在没有用户交互的情况下获取 instagram access_token(新 api)?

QtQuickControls2对话框在点击外部时消失

qt - 如何在qml中使用roboto thin font

qt - 使用 Qstring::toDouble 来检查数据

javascript - document.querySelectorAll() 访问集合的子项