2016-05-16 55 views
0

QML提供了一個date type,似乎可以自由轉換爲C++ QDate classQML Date typeDate類型是JS Date type的擴展。如何更新QML本機`日期`類型?

調用上的date屬性set方法顯然是允許的,因爲沒有錯誤(例如「方法丟失」),則拋出:

// in a QML object declaration 
property date myDate: new Date() 
// in a slot somewhere in the object 
date.setYear(2002) 

然而,日期數據不改變;在打電話setYear(或任何其他set方法)之前和之後打印日期導致完全相同的日期字符串被打印兩次。

在上面提供的鏈接中,QML文檔似乎沒有多少提到date類型。

回答

1

它似乎(和空氣dex的答案似乎證實)的date類型不能直接修改這種方式。這是奇怪,因爲有沒有錯誤嘗試調用方法觸發。

所以解決方案是簡單地聲明日期對象,必須稍後修改爲var而不是date

1

讓我向你解釋一下什麼是錯的評論如下爲例:

class.hpp

#ifndef CLASS_HPP 
#define CLASS_HPP 

#include <QObject> 
#include <QDateTime> 

class DateCpp : public QObject 
{ 
    Q_OBJECT 
    public: 
     DateCpp(); 

    protected: 
     // QML dates are QDateTime in C++, not QDate. 
     Q_PROPERTY(QDateTime fooDate 
        READ getDate 
        WRITE setDate 
        NOTIFY fooDateChanged) 
     QDateTime date; 
     QDateTime getDate() const; 
     void setDate(const QDateTime & d); 

    signals: 
     void dateChanged(); 
}; 

#endif 

class.cpp

#include "class.hpp" 

DateCpp::DateCpp() : QObject(), date() {} 

QDateTime DateCpp::getDate() const 
{ 
    return this->date; 
} 

void DateCpp::setDate(const QDateTime & newDate) 
{ 
    this->date = newDate; 

    /* 
    * You have to tell QML that the property value has changed. 
    * For this, use the NOTIFY signal of the property. If you do not emit it, 
    * the property will not by updated on the QML side. 
    */ 
    emit fooDateChanged(); 
} 

的main.cpp

#include <QApplication> 
#include <QQmlApplicationEngine> 
#include <QtQml> 
#include "class.hpp" 

int main(int argc, char ** argv) 
{ 
    QApplication a(argc, argv); 

    // Do not forget to declare your object to QML. ;-) 
    qmlRegisterType<DateCpp>("Foobar", 3, 14, "DateQML"); 

    QQmlApplicationEngine engine; 
    engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); 

    return a.exec(); 
} 

main.qml

import QtQuick 2.6 
import Foobar 3.14 

Rectangle { 
    id: rect 

    // A QML date (treated as a QDateTime by the engine). 
    property date rectDate 

    /* 
    * Binded to myDate.fooDate, but variables are at different memory spaces. 
    * Will be changed together nontheless. 
    */ 
    property date rd2: myDate.fooDate 

    /* 
    * Just like rd2 but it is the same object in memory, a kind of pointer 
    * on DateCpp::date. 
    */ 
    property alias rd3: myDate.fooDate 

    // No "=" but ":" for property declarations. 
    property date aDateProp: Date.now() 

    DateQML { 
     id: myDate 

     // It is a kind of slot connected to the fooDate Q_PROPERTY's NOTIFY signal. 
     onFooDateChanged: { 
      console.log("The foodate is: " + myDate.fooDate); 

      // Will trigger a signal and "onRectDateChanged" will be executed. 
      rect.rectDate = myDate.fooDate; 
     } 
    } 

    MouseArea { 
     anchors.fill: parent 
     onClicked: { 
      /* 
      * It is JavaScript. "date d" is consequently wrong here. 
      * d will be a QML date (treated as a QDateTime by the engine). 
      */ 
      var d = Date.now(); 
      d.setYear(2002); 

      // Calls the fooDate Q_PROPERTY's WRITE method, i.e. DateCpp::setDate();. 
      myDate.fooDate = d; 

      /* 
      * If DateCpp::setDate(); is not called here, you can still copy 
      * the property to modify what you want: 
      * 
      * var d2 = myDate.fooDate; 
      * d2.setYear(2025); 
      * myDate.fooDate = d2; 
      */ 
      myDate.fooDate.setYear(2025); 
     } 
    } 

    onRectDateChanged: { 
     console.log("The QML property has changed: " + rect.rectDate); 
    } 
} 

TL; DR:

  • 您正在處理QDateTime秒。
  • 使用NOTIFY信號告訴系統該值已更改。
  • date d在JavaScript中聲明日期。
  • QML屬性聲明中沒有任何「=」,但是「:」。
+0

感謝您提供完整的可構建/可運行的示例。對於您的第一個TL; DR點,您是否說QML文檔是錯誤的?它指出:「當與C++集成時,請注意從C++傳入QML的任何QDate值都會自動轉換爲日期值,反之亦然。」在頁面上沒有提及'QDateTime'。對於第三個TL; DR點,我意識到我沒有正確地指出我的兩行代碼的上下文;我已經修正它表明'date'聲明實際上是一個QML屬性聲明,而'setYear'調用在JS上下文中。 –

+0

http://doc.qt.io/qt-5/qtqml-cppintegration-data。html說: 「* QML引擎提供QDateTime值和JavaScript Date對象之間的自動類型轉換。*」 我的不好。 QML基本類型'日期'是'QDate'。它與JS日期無關。不過,使用QML'Date'存在隱式轉換。這些日期是JS日期,所以它們是'QDateTime'。 就我個人而言,我總是使用C++端的'QDateTime'屬性和QML端的'date'屬性,並且我從來沒有遇到關於它的問題。 –

+0

您的編輯存在錯誤。你的日期myDate是一個QML屬性,但你不能在QML屬性中使用「'='」,但是「':'」。看我的編輯。 ;-) –