[Interest] QML and C++ types mapping

STEFANI Mathieu Mathieu.STEFANI at supinfo.com
Wed Mar 14 00:50:57 CET 2012


Investigating with the QMetaObject and QMetaMethod, I figured out that the correct
corresponding signal was clicked(QDateTime) and not clicked(QDate).

By using QDateTime instead of QDate, it just works as expected.

From: interest-bounces+mathieu.stefani=supinfo.com at qt-project.org [mailto:interest-bounces+mathieu.stefani=supinfo.com at qt-project.org] On Behalf Of STEFANI Mathieu
Sent: lundi 12 mars 2012 12:57
To: interest at qt-project.org
Subject: [Interest] QML and C++ types mapping

Hi,

These last days, I'm playing with QML, by making a Calendar application (or at least
trying to).

However, I'm facing an issue : I want to emit a clicked(date d) from my QML
file and connect it to a private slot in my C++ view (a subclass of QDeclarativeView).

But QObject tells me that such a signal does not exist. I made a simple test-case
and indeed, I can't connect my signal. Here is the code :

#include <QtGui/QApplication>
#include <QDeclarativeView>
#include <QGraphicsObject>
#include <QDate>
#include <QDebug>
class MyView : public QDeclarativeView
{
    Q_OBJECT
public:
    MyView(QWidget *parent = 0) : QDeclarativeView(parent) {
        setSource(QUrl::fromLocalFile("main.qml"));
        QObject *root = rootObject();
        connect(root, SIGNAL(clicked(QDate)), this, SLOT(onClicked(QDate)));
    }
private slots:
    void onClicked(const QDate &d) {
        qDebug() << Q_FUNC_INFO << d;
    }
};
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyView v;
    v.show();

    return a.exec();
}
#include "main.moc"

And the corresponding QML file :

import QtQuick 1.1
Item {
    id: root
    width: 500
    height: 500
    signal clicked(date d)
    Rectangle {
        id: innerRect
        color: "lightblue"
        width: 100
        height: 30
        anchors { verticalCenter: parent.verticalCenter; horizontalCenter: parent.horizontalCenter }
        Text {
            text: "Click me!"
            anchors { verticalCenter: parent.verticalCenter; horizontalCenter: parent.horizontalCenter }
        }
        MouseArea {
            anchors.fill: parent
            onClicked: {
                root.clicked(new Date())
            }
        }
    }
}

If I change from date to string in QML and from QDate to QString in C++,  it
just perfectly works.
Since I'm pretty new to QML, I don't know if it's a bug or not.

Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120313/ccc735d3/attachment.html>


More information about the Interest mailing list