QML signals/slots: can't pass QObject* to slot
-
This simple program demonstrates the problem
Classes.h
@
#ifndef CLASS_H
#define CLASS_H#include <QObject>
#include <QMetaType>
#include <QUuid>class ClassA : public QObject
{
Q_OBJECTQ_DISABLE_COPY(ClassA)
Q_PROPERTY(QString text READ text CONSTANT)
QString m_text;
public:
explicit ClassA(QObject *parent = 0) :
QObject(parent),
m_text(QUuid::createUuid().toString()) {}QString text() const {return m_text;}
};Q_DECLARE_METATYPE(ClassA*)
// ===========================================================
class ClassB : public QObject
{
Q_OBJECTQ_DISABLE_COPY(ClassB)
Q_PROPERTY(QString text READ text CONSTANT)
Q_PROPERTY(ClassA* classA READ classA CONSTANT)QString m_text;
ClassA* m_classA;public:
explicit ClassB(QObject *parent = 0) :
QObject(parent),
m_text(QUuid::createUuid().toString()),
m_classA(new ClassA(this)) {}QString text() const {return m_text;}
ClassA* classA() const {return m_classA;}
};Q_DECLARE_METATYPE(ClassB*)
#endif // CLASS_H
@Main.cpp
@
#include <QtGui/QApplication>#include <QtDeclarative>
#include "Classes.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);qRegisterMetaType<ClassA*>();
qmlRegisterType<ClassA>("org.test", 1, 0, "ClassA");
qmlRegisterType<ClassB>("org.test", 1, 0, "ClassB");QDeclarativeView view;
view.rootContext()->setContextProperty("TestClass", new ClassB());
view.setSource(QUrl("Test.qml"));
view.show();return a.exec();
}
@Test.qml
@
#include <QtGui/QApplication>#include <QtDeclarative>
#include "Classes.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);qRegisterMetaType<ClassA*>();
qmlRegisterType<ClassA>("org.test", 1, 0, "ClassA");
qmlRegisterType<ClassB>("org.test", 1, 0, "ClassB");QDeclarativeView view;
view.rootContext()->setContextProperty("TestClass", new ClassB());
view.setSource(QUrl("Test.qml"));
view.show();return a.exec();
}
@After running and a few mouse clicks, the console displays
@
ClassA(0x9f89440)
{c5cb21ae-8851-4cb2-bc7f-26ffe2a5ff05}
QVariant(ClassA*)
undefined
QVariant(ClassA*)
undefined
QVariant(ClassA*)
undefined
@I can not understand why there QVariant(ClassA*) instead of ClassA(0x9f89440)
Do I need someway cast it to ClassA?
-
I am sorry. Real file content is:
Test.qml
@
import QtQuick 1.1
import org.test 1.0Rectangle {
width: 100
height: 62Component.onCompleted: {
console.debug(TestClass.classA)
console.debug(TestClass.classA.text)
mouseArea.sendData.connect(printData)
}function printData(data) {
console.debug(data.classA)
console.debug(data.classA.text)
}MouseArea {
id: mouseArea
anchors.fill: parentsignal sendData(variant data)
onClicked: sendData(TestClass)
}}
@ -
All project files: "Test.7z":http://dl.dropbox.com/u/12676091/Test.7z
-
Have you tried registering the QML type as the class it self? not the pointer of the class.
This means you have to change the following for both classes:
@
qRegisterMetaType<ClassA*>();
Q_DECLARE_METATYPE(ClassA*)
@
to
@
qRegisterMetaType<ClassA>();
Q_DECLARE_METATYPE(ClassA)
@At least in one of my projects, i have done it that way and it works fine.
-
My classes extends QObject, so they have not public copy constructor
compilation fail with error:
In file included from ../Test/main.cpp:3:
In file included from /usr/include/qt4/QtDeclarative/QtDeclarative:3:
In file included from /usr/include/qt4/QtCore/QtCore:4:
In file included from /usr/include/qt4/QtCore/qabstractitemmodel.h:45:
In file included from /usr/include/qt4/QtCore/qvariant.h:48:
/usr/include/qt4/QtCore/qmetatype.h:142:16: error: calling a private constructor of class 'ClassA'
return new T(*static_cast<const T*>(t));