• 3420阅读
  • 1回复

[讨论]外部文件动态加载的组件,只能通过connections连接信号吗? [复制链接]

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

只看楼主 倒序阅读 楼主  发表于: 2015-09-14
我通过外部文件动态加载了组件,可是通过connect方式连接信号和槽函数的时候,始终提示方法未定义。还请各位大侠支招哪。代码如下:
import QtQuick 2.4import QtQuick.Window 2.2import QtQuick.Controls 1.2;import QtQuick.Controls.Styles 1.2Window {    width:520;    height: 440;    visible: true    Rectangle{    width:320;    height: 240;    color:"#EEEEEE";    id:rootItem;    property var colorPickerShow: false;    Text{    id:coloredText;    anchors.horizontalCenter: parent.horizontalCenter;    anchors.top: parent.top;    anchors.topMargin: 4;    text: "hello QT";    font.pixelSize: 32;    }    Button{        id:ctrlButton;        text:"show";        anchors.left: parent.left;        anchors.leftMargin: 4;        anchors.bottom: parent.bottom;        anchors.bottomMargin: 4;        onClicked: {        if(rootItem.colorPickerShow)        {            redLoader.sourceComponent=undefined;            blueLoader.source="";            rootItem.colorPickerShow=false;            ctrlButton.text="show";        }        else        {            redLoader.source="ColorPicker.qml";           redLoader.item.colorPicked.connect(onPickRed);            blueLoader.source="ColorPicker.qml";            blueLoader.item.colorPicked.connect(onPickBlue);            redLoader.focus=true;            rootItem.colorPickerShow=true;            ctrlButton.text="Hide";        }        }    }    Loader{    id:redLoader;    anchors.left: ctrlButton.right;    anchors.leftMargin: 4;    anchors.bottom: ctrlButton.bottom;    KeyNavigation.right: blueLoader;    KeyNavigation.tab: blueLoader;    onLoaded: {    if(item!=null)    { item.color="red";item.focus=true;}    }    onFocusChanged: {    if(item!=null)    {     item.focus=focus;    }    }    }    Loader{    id:blueLoader;    anchors.left: redLoader.right;    anchors.leftMargin: 4;    anchors.bottom: parent.bottom;    anchors.bottomMargin: 4;    KeyNavigation.left: redLoader;    KeyNavigation.tab: redLoader;    onLoaded: {    if(item!=null)    {        item.color="blue";    }    }    onFocusChanged: {    if(item!=null)    {        item.focus=focus;    }    }    }//    Connections{//        target: blueLoader.item;//   onColorPicked:{onPickBlue}//    }    function onPickBlue(clr)    {        coloredText.color=clr;        if(!blueLoader.focus)        {            blueLoader.focus=true;            redLoader.focus=false;        }     }    function onPickRed(clr)    {          coloredText.color=clr;            if(!redLoader.focus)            {                redLoader.focus=true;                redLoader.focus=false;            }    }    }
}ColorPicker.qml文件内容如下:import QtQuick 2.0
Rectangle {id:colorpicker;    width: 50;    height: 30;

    signal colorPicked(color clr)    function configureBorder()    {        colorpicker.border.width=colorpicker.focus?2:0;        colorpicker.border.color=colorpicker.focus?"#black":"green";    }    MouseArea    {        anchors.fill: parent;        onClicked: {colorpicker.colorPicked(colorpicker.color);        mouse.accepted=true;        colorpicker.focus=true;        configureBorder();        }    }    Keys.onReturnPressed: {colorpicker.colorPicked(colorpicker.color);    event.accepted=true;    }    Keys.onSpacePressed: {colorpicker.coloPicked(colorpicker.color);    event.accepted=true;    }    onFocusChanged: { configureBorder();}    Component.onCompleted: configureBorder();}
离线石头轩

只看该作者 1楼 发表于: 2015-09-14
么办法了,我只好吧信号与槽函数的实现放在Connections中实现了。代码如下:

import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Controls 1.2;
import QtQuick.Controls.Styles 1.2
Window {
    width:520;
    height: 440;
    visible: true
    Rectangle{
    width:320;
    height: 240;
    color:"#EEEEEE";
    id:rootItem;
    property var colorPickerShow: false;
    Text{
    id:coloredText;
    anchors.horizontalCenter: parent.horizontalCenter;
    anchors.top: parent.top;
    anchors.topMargin: 4;
    text: "hello QT";
    font.pixelSize: 32;
    }
    Button{
        id:ctrlButton;
        text:"show";
        anchors.left: parent.left;
        anchors.leftMargin: 4;
        anchors.bottom: parent.bottom;
        anchors.bottomMargin: 4;
        onClicked: {
        if(rootItem.colorPickerShow)
        {
            redLoader.sourceComponent=undefined;
            blueLoader.source="";
            rootItem.colorPickerShow=false;
            ctrlButton.text="show";
        }
        else
        {
            redLoader.source="ColorPicker.qml";
           //redLoader.item.colorPicked.connect(onPickRed);
            blueLoader.source="ColorPicker.qml";
            //blueLoader.item.colorPicked.connect(onPickBlue);
            redLoader.focus=true;
            rootItem.colorPickerShow=true;
            ctrlButton.text="Hide";
        }
        }
    }
    Loader{
    id:redLoader;
    anchors.left: ctrlButton.right;
    anchors.leftMargin: 4;
    anchors.bottom: ctrlButton.bottom;
    KeyNavigation.right: blueLoader;
    KeyNavigation.tab: blueLoader;
    onLoaded: {
    if(item!=null)
    { item.color="red";item.focus=true;}
    }
    onFocusChanged: {
    if(item!=null)
    {
     item.focus=focus;
    }
    }
    }
    Loader{
    id:blueLoader;
    anchors.left: redLoader.right;
    anchors.leftMargin: 4;
    anchors.bottom: parent.bottom;
    anchors.bottomMargin: 4;
    KeyNavigation.left: redLoader;
    KeyNavigation.tab: redLoader;
    onLoaded: {
    if(item!=null)
    {
        item.color="blue";
    }
    }
    onFocusChanged: {
    if(item!=null)
    {
        item.focus=focus;
    }
    }
    }
    Connections{
        target: blueLoader.item;
   onColorPicked:{ coloredText.color=clr;
       if(!blueLoader.focus)
       {
           blueLoader.focus=true;
           redLoader.focus=false;
       }}
    }
    Connections{
        target: redLoader.item;
   onColorPicked:{ coloredText.color=clr;
       if(!redLoader.focus)
       {
           redLoader.focus=true;
           redLoader.focus=false;
       }}
    }
    function onPickBlue(clr)
    {
        coloredText.color=clr;
        if(!blueLoader.focus)
        {
            blueLoader.focus=true;
            redLoader.focus=false;
        }
     }
    function onPickRed(clr)
    {
          coloredText.color=clr;
            if(!redLoader.focus)
            {
                redLoader.focus=true;
                redLoader.focus=false;
            }
    }
    }

}
这样就么有异常了。

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