PDA

View Full Version : How to get the current date out of a Date Tumbler



cleopa
30th August 2019, 00:43
Hello All,

I am a newbie in QML and I am trying to set the time through a Tumbler. The code for the Tumbler is


import QtQuick 2.12
import QtQuick.Window 2.2
import QtQuick.Controls 2.12

Rectangle {
width: frame.implicitWidth + 10
height: frame.implicitHeight + 10

function formatText(count, modelData) {
var data = count === 12 ? modelData + 1 : modelData;
return data.toString().length < 2 ? "0" + data : data;
}

FontMetrics {
id: fontMetrics
}

Component {
id: delegateComponent

Label {
text: formatText(Tumbler.tumbler.count, modelData)
opacity: 1.0 - Math.abs(Tumbler.displacement) / (Tumbler.tumbler.visibleItemCount / 2)
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pixelSize: fontMetrics.font.pixelSize * 1.25
}
}

Frame {
id: frame
padding: 0
anchors.centerIn: parent

Row {
id: row

Tumbler {
id: hoursTumbler
model: 12
delegate: delegateComponent
}

Tumbler {
id: minutesTumbler
model: 60
delegate: delegateComponent
}

Tumbler {
id: amPmTumbler
model: ["AM", "PM"]
delegate: delegateComponent
}
}
}
}


I am calling the time tumbler from another page


import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.0
import QtQuick.Particles 2.0
import QtQuick.Layouts 1.0
import QtQuick.Controls 1.3
import Timebar 3.14
//import "ChangeLanguageAndRegion.qml"


Window {
id: changedateandtime
visible: true
visibility: Window.FullScreen
color: "#000000"

Item {
id: timeChangeTumbler_item
anchors.horizontalCenter: parent.horizontalCenter
anchors.horizontalCenterOffset: -40
anchors.verticalCenter: parent.verticalCenter
anchors.verticalCenterOffset: 30
height: 200
width: 180

TimeTumbler {
id: timeTumbler
anchors.rightMargin: 0
anchors.bottomMargin: -1
anchors.leftMargin: 0
anchors.topMargin: 1
// border.width: 0
}
}
Item {
id: doneandcancel
anchors.horizontalCenter: parent.horizontalCenter
y: 420
width: 120
height: 50

Image {
id: donetickmark
anchors.left: parent.left
anchors.top: parent.top
anchors.bottom: parent.bottom
width: parent.width/3
fillMode: Image.PreserveAspectFit
source: "resources/TickmarkIcon.png"
}
Button {
id: donebutton
anchors.left: passkey.left
anchors.right: passkey.right
anchors.bottom: passkey.bottom
anchors.top: passkey.top
anchors.topMargin: -20
anchors.leftMargin: -20
opacity: 0

onClicked: {
//save changes
var hour = timeTumbler.currentIndex;
mainLoader.source = 'Dashboard_2.qml';
}
}

Image {
id: cancelcross
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: parent.bottom
width: parent.width/3
fillMode: Image.PreserveAspectFit
source: "resources/CrossIcon.png"
}
Button {
id: cancelbutton
anchors.left: log.left
anchors.right: log.right
anchors.bottom: log.bottom
anchors.top: log.top
anchors.topMargin: -20
anchors.rightMargin: -20
opacity: 0

onClicked: {

mainLoader.source = 'Dashboard_2.qml';
}

}

}

When I press on the Done button I would like to get the hour from the hour column of the tumbler but I don't get anything. I am using the id : timeTumbler to access it but how can I get to the hours column and retrieve the index ?

anda_skoa
30th August 2019, 11:33
The internal object of TimeTumbler are not directly addressable from outside, but you can "forward" those properties that you need.



Rectangle {
property alias hoursIndex: hoursTumbler.currentIndex

Tumbler {
id: hoursTumbler
}
}


or maybe even more appropriate the hour value


Rectangle {
readonly property int hours: hoursTumbler.currentIndex + 1

Tumbler {
id: hoursTumbler
}
}


Cheers,
_

cleopa
30th August 2019, 20:16
Thank you!