2014-11-08 79 views
1

我在Qt的新的,我想连接QML日历信号点击(日期为准),以CPP斯鲁特这样的: main.qml:不能获得点击(日期日期)信号从日历

ApplicationWindow { 
    title: qsTr("MoneyInTheBank") 
    visible: true 
    width: 335 
    height: 500 
    color: "#333" 

    Item{ 
     x: 5 
     y: 9 
     width: 325 
     height: 240 

     Calendar{ 
      id: calendar 
      objectName: "calendar" 
      x: 4 
      y: 5 
      width: 318 
      height: 230 
      weekNumbersVisible: true 

      style: CalendarStyle { 
       gridVisible: false 
         dayDelegate: Rectangle { 
          gradient: Gradient { 
           GradientStop { 
            position: 0.00 
            color: styleData.selected ? "#111" : (styleData.visibleMonth && styleData.valid ? "#444" : "#666"); 
           } 
           GradientStop { 
            position: 1.00 
            color: styleData.selected ? "#444" : (styleData.visibleMonth && styleData.valid ? "#111" : "#666"); 
           } 
           GradientStop { 
            position: 1.00 
            color: styleData.selected ? "#777" : (styleData.visibleMonth && styleData.valid ? "#111" : "#666"); 
           } 
          } 

          Label { 
           text: styleData.date.getDate() 
           anchors.centerIn: parent 
           color: styleData.valid ? "white" : "grey" 
          } 

          Rectangle { 
           width: parent.width 
           height: 1 
           color: "#555" 
           anchors.bottom: parent.bottom 
          } 

          Rectangle { 
           width: 1 
           height: parent.height 
           color: "#555" 
           anchors.right: parent.right 
          } 
         } 
      } 
     } 
    } 
} 

Calendar.h:

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

public slots: 
    void ShowShedulerWindow() const; 
}; 

Calendar.cpp

MyCalendar::MyCalendar() 
{ 
} 

void MyCalendar::ShowShedulerWindow() const 
{ 
    QMessageBox msgBox; 
    msgBox.setText("Button pushed"); 
    msgBox.exec(); 
} 

的main.cpp

#include "Calendar.h" 

int main(int argc, char *argv[]) 
{ 
    QApplication app(argc, argv); 
    QQmlApplicationEngine engine; 

    QQmlComponent qComponent(&engine, 
     QUrl(QStringLiteral("qrc:/main.qml"))); 
    QObject *qObject = qComponent.create(); 

    QObject *qobjCalendar = qObject->findChild<QObject*>("calendar"); 
    if(qobjCalendar) 
    { 
     MyCalendar *objCalendar = new MyCalendar(); 
     QObject::connect(qobjCalendar, SIGNAL(clicked(QDate)), objCalendar, SLOT(ShowShedulerWindow())); 
    } 

    return app.exec(); 
} 

而且我有: 的QObject ::连接:没有这样的信号Calendar_QMLTYPE_14 ::点击(QDATE)在.. \经济学家\ main.cpp中:24 的QObject ::连接:(发件人名称: '日历' ) 请告诉我做错了什么?

回答

2

QML的Date类型“使用区域感知函数扩展JS Date对象”。 JavaScript Date object本身代表一个时间点(例如01/01/2014 10:30:00)。为了在C++中表达,我们需要一个能够存储日期和时间的对象。在Qt中,这是QDateTime

因此,在连接信号到发射QDateTime对象:

QObject::connect(qobjCalendar, SIGNAL(clicked(QDateTime)), objCalendar, SLOT(ShowShedulerWindow())); 

回答此之后,我意识到Calendar's signals被记录为发射“基本” date类型,我不我认为是正确的,因为那种类型确实相当于QDate。不知何故,您仍然可以将发出基本日期类型的QML信号连接到提供类型为QDateTime的C++插槽。我创建了一个错误报告,在这里不正确的文件:

Calendar's signals are documented as emitting the basic date type

+0

非常感谢您! – otashlanov 2014-11-08 12:27:53

+0

它看起来像文档仍然是错误的(Qt 5.6)。根据文档[1]日期(QML)映射到QDate(C++)。但是这是不正确的,并且不起作用,正如Mitch已经提到的那样。 [1] http://doc.qt.io/qt-5/qtqml-cppintegration-data.html – user2494129 2016-05-24 19:05:27