我是Qt的新手,我想将点击的qml日历信号(日期日期)连接到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
}
}
}
}
}
}
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 :: connect:在.. \ Economist \ main.cpp中没有这样的信号Calendar_QMLTYPE_14 :: clicked(QDate):24 QObject :: connect :(发件人姓名:'calendar') 请告诉我我做错了什么?
答案 0 :(得分: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类型,我认为这种类型不正确,因为该类型确实等同于{{1} }。不知何故,您仍然可以将发出基本日期类型的QML信号连接到C ++插槽,前提是类型为QDate
。我在这里为错误的文档创建了一个错误报告:
Calendar's signals are documented as emitting the basic date type