【问题标题】:Can't get clicked(date date) signal from Calendar无法从日历中获得点击(日期日期)信号
【发布时间】:2014-11-08 11:56:23
【问题描述】:

我是 Qt 的新手,我想将 qml 日历信号 clicked(date date) 连接到 cpp slote,如下所示: 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
                           }
                       }
           }
       }
   }
}

日历.h:

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

public slots:
    void ShowShedulerWindow() const;
};

日历.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::connect: 没有这样的信号 Calendar_QMLTYPE_14::clicked(QDate) in ..\Economist\main.cpp:24 QObject::connect: (发件人姓名: '日历') 请告诉我做错了什么?

【问题讨论】:

    标签: c++ qt user-interface


    【解决方案1】:

    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 信号连接到 C++ 插槽,前提是类型为QDateTime。我在这里为不正确的文档创建了一个错误报告:

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

    【讨论】:

    • 看起来文档仍然是错误的(Qt 5.6)。根据文档 [1] 日期 (QML) 映射到 QDate (c++)。然而,正如米奇已经提到的那样,这是不正确的并且不起作用。 [1]doc.qt.io/qt-5/qtqml-cppintegration-data.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 2022-01-09
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    相关资源
    最近更新 更多