main.qml代码如下:
import QtQuick 1.1Rectangle{
id:_bornwidth: 400;height: 200color: "white"property variant object;Rectangle{id:monthwidth: 120;height: 20color: "#575757"Text {id: brithdaytext: "生日:" + "-月-日"color: "white"}
function showCalendar(){var component = Qt.createComponent("Calendar.qml");if (component.status === Component.Ready) {var bQml = component.createObject(_born);bQml.xClicked.connect(doSomething); // 实现了两个qml组件之间的通信,传递数据}
}
}
function doSomething(msg){console.log(msg);brithday.text = "生日:"+msg}
MouseArea {x:0;y:0width: 80;height: 20onClicked: month.showCalendar()}
}
Calendar.qml源码如下:
import QtQuick 1.1Rectangle {width: 160height: 120id:born_Calendarvisible: trueproperty variant tempsignal xClicked(string msg)//与主的qml组件通信的信号ListModel {id:m_CalendarListElement {name: "1 月"}ListElement {name: "2 月"}ListElement {name: "3 月"}ListElement {name: "4 月"}ListElement {name: "5 月"}ListElement {name: "6 月"}ListElement {name: "7 月"}ListElement {name: "8 月"}ListElement {name: "9 月"}ListElement {name: "10 月"}ListElement {name: "11 月"}ListElement {name: "12 月"}}
ListModel {id:d_CalendarListElement {name: "1 日"}ListElement {name: "2 日"}ListElement {name: "3 日"}ListElement {name: "4 日"}ListElement {name: "5 日"}ListElement {name: "6 日"}ListElement {name: "7 日"}ListElement {name: "8 日"}ListElement {name: "9 日"}ListElement {name: "10 日"}ListElement {name: "11 日"}ListElement {name: "12 日"}ListElement {name: "13 日"}ListElement {name: "14 日"}ListElement {name: "15 日"}ListElement {name: "16 日"}ListElement {name: "17 日"}ListElement {name: "18 日"}ListElement {name: "19 日"}ListElement {name: "20 日"}ListElement {name: "21 日"}ListElement {name: "22 日"}ListElement {name: "23 日"}ListElement {name: "24 日"}ListElement {name: "25 日"}ListElement {name: "26 日"}ListElement {name: "27 日"}ListElement {name: "28 日"}ListElement {name: "29 日"}ListElement {name: "30 日"}ListElement {name: "31 日"}}
Component {id: delegateColumn {id: wrapperText {id: nameTexttext: namefont.pointSize: 16color: wrapper.PathView.isCurrentItem ? "white" : "black"// //up// Item{// width: nameText.width// height: nameText.height/3// MouseArea{// anchors.fill: parent// onClicked: {// view.decrementCurrentIndex();// }// }// }// //down// Item{// width: nameText.width// height: nameText.height/3// MouseArea{// anchors.fill: parent// onClicked: {// view.incrementCurrentIndex();// }// }// }}
}
}
//标题viewRectangle {id:titlex:0;y:0width: born_Calendar.width;height: 25color:"#474747"Text{id: nameTexttext: "Your born"font.pointSize: 16color: "#1E90FF"}
Rectangle {id:line_viewanchors.top: nameText.bottomx:0;y:20width: born_Calendar.width;height: 2color:"#1E90FF"}
}
//生日日期,某个月份Rectangle {id:m_Rectx:0;y:25width: born_Calendar.width/2;height: 70color:"#474747"PathView {id:viewanchors.fill: parentmodel: m_Calendardelegate: delegatepathItemCount: 3preferredHighlightBegin: 0.5preferredHighlightEnd: 0.5path: Path {startX: 40; startY: 0PathLine { x: 40; y: 25; }PathLine { x: 40; y: 45; }PathLine { x: 40; y: 65; }}
onCurrentIndexChanged: {console.log("view.currentIndex="+view.currentIndex)//根据月份不同,对应的日期可选范围发生变化if(1 == view.currentIndex){d_Calendar = d_Calendar.remove(30)d_Calendar = d_Calendar.remove(29)}if(3 == view.currentIndex||5 == view.currentIndex||8 == view.currentIndex||10 == view.currentIndex){if(d_Calendar.count == 31){d_Calendar = d_Calendar.remove(30)}
}if(0 == view.currentIndex||2 == view.currentIndex||4 == view.currentIndex||6 == view.currentIndex||7 == view.currentIndex||9 == view.currentIndex||11 == view.currentIndex){if(d_Calendar.count == 30){d_Calendar = d_Calendar.insert(30,{name: "31 日"})}
if(d_Calendar.count == 29){d_Calendar = d_Calendar.insert(29,{name: "30 日"})d_Calendar = d_Calendar.insert(30,{name: "31 日"})}
}
}
}
}
//生日日期,某一天Rectangle {id:d_Rectx:born_Calendar.width/2;y:25width: born_Calendar.width/2;height: 70color:"#474747"PathView {id:view1anchors.fill: parentmodel: d_Calendardelegate: delegatepathItemCount: 3preferredHighlightBegin: 0.5preferredHighlightEnd: 0.5path: Path {startX: 40; startY: 0PathLine { x: 40; y: 25; }PathLine { x: 40; y: 45; }PathLine { x: 40; y: 65; }}
onCurrentIndexChanged: {console.log("view1.currentIndex="+view1.currentIndex)}
}
}
//下面另外一条分割线:横向线Rectangle {id:split_line_Hanchors.bottom: confirm.topx:0;y:20width: born_Calendar.width;height: 1color:"white"}
//确定按钮Rectangle {id: confirmx:0;y:90width: born_Calendar.width/2;height: 35color: "#474747"Text{id: textx:10;y:10text: "Confirm"font.pointSize: 16color: "#1E90FF"//anchors.centerIn: confirm.Center+15}
MouseArea{anchors.fill: parentonClicked: {// console.log("您的生日是:"+(view.currentIndex+1)+"月"+(view1.currentIndex+1)+"日")// Qt.quit()born_Calendar.xClicked((view.currentIndex+1)+"月"+(view1.currentIndex+1)+"日")//往信号内传入数据born_Calendar.destroy()//销毁当前qml组件}
}
}
//下面另外一条分割线:纵向线Rectangle {id:split_line_Vanchors.left: confirm.rightx:born_Calendar.width/2;y:90width: 1;height: confirm.heightcolor:"white"}
//取消按钮Rectangle {id: concelx:born_Calendar.width/2+1;y:90width: born_Calendar.width/2-1;height: 35color: "#474747"Text{id: text1x:16;y:10text: "Concel"font.pointSize: 16color: "#1E90FF"}
MouseArea{anchors.fill: parentonClicked: {// Qt.quit()born_Calendar.destroy()//销毁当前qml组件}
}
}
}