从另一个qml文件动态加载qml组件并访问该组件';s qml类型属性

从另一个qml文件动态加载qml组件并访问该组件';s qml类型属性,qml,qqmlcomponent,Qml,Qqmlcomponent,我在StackOverflow中研究了与此主题相关的其他问题,但这对我没有帮助。我是QML/Javascript新手,我查阅了有关这个问题的QML文档,但没有帮助 下面是一个文件“SmallWindow.qml” Item { ... property var statusColour: calStatusColour(() property Component devViewNameComponent: devNameComp function calS

我在StackOverflow中研究了与此主题相关的其他问题,但这对我没有帮助。我是QML/Javascript新手,我查阅了有关这个问题的QML文档,但没有帮助

下面是一个文件“SmallWindow.qml”

Item
    {
    ...
    property var statusColour: calStatusColour(()
    property Component devViewNameComponent: devNameComp
    function calStatusColour()
    {
        var color = "black" //Here, colour will be changed based on some status.
    }

    Row
    {
        Component{
            id:devNameComp

        Rectangle
        {
            id:statusRect
            width: parent.width * 0.2
            height: parent.height
            color: statusColour.color
            Text
                {
                id: viewName
                anchors.centerIn: parent
                text: statusText == 0 ? "TrueText" : "FalseText"
                font.pixelSize: 20
                }   
        }  
                } //end of component

    Rectangle
    {...}

    }
}
我有另一个文件“FileDetailWindow.qml”。在此文件中,在函数“showDetailWindow”中,我希望访问并更改 devViewNameComponent的(来自SmallWindow.qml)视图名的宽度。我无法访问viewName,也不确定使用组件是否正确

Item 
{
    ...

    //This function is called from another qml file
    function showDetailWindow()
    {

    if (detailsWindow.devViewNameComponent.status  == Component.Ready)
    {
       var compDevName = detailsWindow.devViewNameComponent.createObject(detailsWindow)
       if (compDevName == null) {
           console.log("Error creating object");
         }

   //Here I want to access and set the viewName's width dynamically when this function is called like below
   //Other things about statusRect and ViewName can be same. 
   //below is wrong usage (detailsWindow.devViewNameComponent.viewName) and it does not work
        if (detailsWindow.devViewNameComponent.viewName.paintedWidth > 75)             
             detailsWindow.devViewNameComponent.viewName.width = detailsWindow.devViewNameComponent.statusRect.width *0.75;       
       else
             detailsWindow.devViewNameComponent.viewName.width= detailsWindow.devViewNameComponent.viewNamepaintedWidth;                       
   }
}

    SmallWindow
    {
      id: detailsWindow
      visible: true;
      ...
    }
}
编辑1:我想修复“showDetailWindow()”中文本(id:viewName)的大小,因为viewName文本的长度将动态更改

如您所见,viewName文本位于矩形内(id:statusRect),statusRect的宽度和高度不会改变,而其颜色会根据函数CalStatusColor()而改变


当前的问题是,如果viewName长度大于statusRect,则viewName文本超出statusRect之外,并且我希望在statusRect矩形的宽度内缩短viewName文本。例如,如果文本超过statusRect Rectangle的长度,则需要像“NameLengthRapped…”一样包装文本。

我只是根据自己的需要做了一些变通,这样动态变化的文本将在不使用任何组件的情况下包装在其矩形内

首先,我删除了组件的东西。然后我更改了文本(id:viewName) 如下所示,调用wrapDevName()函数


请提供一个运行的最小示例。我没有看到执行showDetailWindow()的触发器。如果在方法的第一行包含一个console.log(“called”),您会得到任何输出吗?@Mitch:实际上,它是现有项目的一部分。由于我是QML新手,我发现很难将其复制到一个小的工作示例中。@Th.Thielemann:实际上,在第三个不同的QML文件中单击按钮时会调用“showDetailWindow()”,这与上面提到的“FileDetailWindow.QML”和“SmallWindow.QML”不同。是的,我在“showDetailWindow()”中获得控制台输出,这没有问题。
Text {
 id:viewName
 ...
 text: wrapDevName()
}

function wrapDevName()
{
  if (statusText == 0)
      return ""
  else
    {
//15 is calculated by trial and error of running the application such that it does not exceed the length of its rectangle
      var maxTextLength = statusRect.width/15 
      var devName = dev.getName()

      if (devName.length > maxTextLength)
           return devName.substring(0,maxTextLength) + "..."
      else
          return devName
    }
}