我通过外部文件动态加载了组件,可是通过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();}