Qt 5有没有DatePicker控件?

时间:2014-01-16 04:08:33

标签: datepicker qml qtquick2

我正在为QtQuick 2.0编写我的第一个QML / Javascript应用程序。 我需要放置一个 DatePicker 控件,但我还没有在 QtQuick.Controls 下找到任何控件 - 实际上并没有 - 。

我开始相信没有办法在QML中调用'native'DatePicker。我必须实现一个还是存在一个?

3 个答案:

答案 0 :(得分:7)

好吧,我必须自己控制。它被称为 Datepicker

Datepicker example

打算以这种方式使用:

import QtQuick.Controls 1.1

ApplicationWindow {
    id: main

    Datepicker {
        id: myDate
        activeWindow: main
        width: 200
    }
}

它假设您正在使用Window对象,并且需要父引用将日期选择器显示在正确的位置(它在新窗口中显示日历)。

您可以从以下位置下载源代码: https://bitbucket.org/camolin3/datepicker

这是第一个版本,需要做好大量工作,但这是一个起点。

答案 1 :(得分:7)

万一其他人偶然发现这一点,到目前为止QtQuick.Controls中有一个Calendar元素。这应该使这种Datepicker的实施变得更加容易:http://qt-project.org/doc/qt-5/qml-qtquick-controls-calendar.html

答案 2 :(得分:1)

** DatePicker的代码。试试这个**

*TextField {
    id: textDate
    x: 10
    y: 42
    width: 175
    height: 33
    placeholderText: qsTr("Text Field")
    text:Qt.formatDate(cal.selectedDate, "dd-MM-yyyy")
    font.bold: true
    font.family:"times new roman"
    font.pointSize: 12
}
Button {
    id: button
    x: 198
    y: 42
    width: 25
    height: 29
    Image {
        x: -4
        y: -4
        id: img
        width: 36
            height: 44
            source: "/Images/calendar-512.png"
    }
    onClicked:{
        cal.visible=true
    }
}
Calendar{
           id:cal
           x: 10
           y: 82
           width: 220
           height: 205
           visible: false
           selectedDate: new Date()
           onClicked:  {
               textDate.text=Qt.formatDate(cal.selectedDate, "dd-MM-yyyy");
                cal.visible=false
           }
}*