otashlanov
otashlanov

Reputation: 340

Can't get clicked(date date) signal from Calendar

I am new in Qt, I want to connect qml Calendar signal clicked(date date) to cpp slote like this: 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();
}

And I have: QObject::connect: No such signal Calendar_QMLTYPE_14::clicked(QDate) in ..\Economist\main.cpp:24 QObject::connect: (sender name: 'calendar') Please tell what i'm doing wrong?

Upvotes: 1

Views: 783

Answers (1)

Mitch
Mitch

Reputation: 24416

QML's Date type "extends the JS Date object with locale aware functions". The JavaScript Date object itself represents a point in time (e.g. 01/01/2014 10:30:00). In order to express that in C++, we need an object capable of storing date and time. In Qt, that's QDateTime.

Thus, the signal that you connect to emits a QDateTime object:

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

After answering this, I realised that Calendar's signals are documented as emitting a "basic" date type, which I don't think is correct, as that type is indeed equivalent to QDate. Somehow, you can still connect QML signals emitting a basic date type to C++ slots providing the type is QDateTime. I've created a bug report for the incorrect documentation here:

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

Upvotes: 2

Related Questions