Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML signals/slots: can't pass QObject* to slot
Qt 6.11 is out! See what's new in the release blog

QML signals/slots: can't pass QObject* to slot

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 4.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pandemorra
    wrote on last edited by
    #1

    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_OBJECT

    Q_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_OBJECT

    Q_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?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      Looks like you reposted Main.cpp instead of Test.qml.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pandemorra
        wrote on last edited by
        #3

        I am sorry. Real file content is:

        Test.qml

        @
        import QtQuick 1.1
        import org.test 1.0

        Rectangle {
        width: 100
        height: 62

        Component.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: parent

        signal sendData(variant data)

        onClicked: sendData(TestClass)
        }

        }
        @

        1 Reply Last reply
        0
        • P Offline
          P Offline
          pandemorra
          wrote on last edited by
          #4

          All project files: "Test.7z":http://dl.dropbox.com/u/12676091/Test.7z

          1 Reply Last reply
          0
          • F Offline
            F Offline
            favoritas37
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pandemorra
              wrote on last edited by
              #6

              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));

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved