• 3334阅读
  • 4回复

[提问]通过组件文件动态创件组件始终失败 [复制链接]

上一主题 下一主题
离线石头轩
 

只看楼主 倒序阅读 楼主  发表于: 2015-09-23
通过组件文件动态创建组件,定义了一个单独的方法,然后通过button的onClicked信号处理器调用,始终提示方法未定义,这是怎么回事哪?请解答
import QtQuick 2.4import QtQuick.Window 2.2import QtQuick.Controls 1.2;Window {    visible: true   width:360;   height: 300Rectangle{id:rootItem;width: parent.width;height:parent.height;property int count: 0;property Component component: null;
Text{id:coloredText;width: 200;text:"control your propation,keep on smile";anchors.centerIn: parent;font.pixelSize: 18wrapMode:Text.WordWrap;color:"red";}function changeTextColor(clr){ coloredText.color=clr;}function createColorPicker(clr){    if(rootItem.component==null)    {        rootItem.component=Qt.createComponent("ColorPicker.qml");    }    var colorPicker;    if(rootItem.component.status==Component.Ready)    {        colorPicker=rootItem.component.createObject(rootItem,{"color":clr,"x":rootItem.count*55,"y":10});        colorPicker.colorPicked.connect(rootItem.changeTextColor());    }    rootItem.count++;}Button{id:add;text:"add";anchors.left: parent.left;anchors.leftMargin: 4;anchors.bottom: parent.bottom;anchors.bottomMargin: 4;onClicked: {    createColorPicker(Qt.rgba(Math.random(),Math.random(),Math.random(),1)); }}
}
}
离线tylan

只看该作者 1楼 发表于: 2015-09-23
代码弄成这样,怎么看?
离线石头轩

只看该作者 2楼 发表于: 2015-09-23
解决了,button信号处理器连接的方法是父级对象的方法,所以要加上父级对象。
onClicked: {
rootItem.createColorPicker(Qt.rgba(Math.random(),Math.random(),Math.random(),1));

}
离线石头轩

只看该作者 3楼 发表于: 2015-09-23
回 tylan 的帖子
tylan:代码弄成这样,怎么看? (2015-09-23 17:16) 

直接减下来,贴到你的开发环境中就行了,
这个也不是我整的,我贴了代码进去他就成这样了,不关我的事啊
离线石头轩

只看该作者 4楼 发表于: 2015-09-23
回 tylan 的帖子
tylan:代码弄成这样,怎么看? (2015-09-23 17:16) 

this is mycode


import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Controls 1.2;
Window {
    visible: true
   width:360;
   height: 300
Rectangle{
id:rootItem;
width: parent.width;
height:parent.height;
property int count: 0;
property Component component: null;

property int yCount:0;
Text{
id:coloredText;
width: 200;
text:"control your propation,keep on smile";
anchors.centerIn: parent;
font.pixelSize:18
wrapMode:Text.WordWrap;
color:"red";
}
function changeTextColor(clr)
{
coloredText.color=clr;
}
function  setCommpentX()
{
    if (rootItem.count*55>rootItem.width)
    {
       count=0;
        ycount++;
    }
return count*55+1;

}
function createColorPicker(clr)
{
    if(rootItem.component==null)
    {
        rootItem.component=Qt.createComponent("ColorPicker.qml");
    }
    var colorPicker;
    if(rootItem.component.status==Component.Ready)
    {
        colorPicker=rootItem.component.createObject(
                    rootItem,
                    {"color":clr,"x":setCommpentX(),"y":10*yCount}
                    );
        colorPicker.colorPicked.connect(rootItem.changeTextColor);
    }
    rootItem.count++;
}
Button{
id:add;
text:"add";
anchors.left: parent.left;
anchors.leftMargin: 4;
anchors.bottom: parent.bottom;
anchors.bottomMargin: 4;
onClicked: {
    rootItem.createColorPicker(Qt.rgba(Math.random(),Math.random(),Math.random(),1)); }
}

}

}
快速回复
限100 字节
 
上一个 下一个