[Interest] QML and C++ types mapping

STEFANI Mathieu Mathieu.STEFANI at supinfo.com
Mon Mar 12 18:57:18 CET 2012


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/20120312/5350b85b/attachment.html>


More information about the Interest mailing list