How to use a custom Qt type with a QML signal?

Franz

I've created a custom type inside my Qt 5.2 qml application

class Setting : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString key READ key WRITE setKey)
    Q_PROPERTY(QVariant value READ value WRITE setValue)

public:
    Setting(QObject * parent = 0);

    QString key() const;
    void setKey(QString const & key);

    QVariant value() const;
    void setValue(QVariant const & value);

private:
    QString m_key;
    QVariant m_value;
};

and registered it in my main function:

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    qmlRegisterType<Setting>("Setting", 1,0, "Setting");

    // ...
}

In my application I would like to have a button that has a signal with my custom type as a parameter:

import Setting 1.0

Button
{
    signal settingChanged(string, Setting)

    // ...
}

Every time I hit the button the signal should be emitted.

If I try to connect my qml signal with my C++ slot

QObject::connect(myButton, SIGNAL(settingChanged(QString, Setting)), this, SLOT(settingChanged(QString, Setting)));

Qt says

QObject::connect: No such signal Button_QMLTYPE_41_QML_45::settingChanged(QString, Setting) in ...

If I remove my custom type from the signal definition

Button
{
    signal settingChanged(string)

    // ...
}

and modify the connect call

QObject::connect(myButton, SIGNAL(settingChanged(QString)), this, SLOT(settingChanged(QString)));

the connect call worked.

I could use my custom qml type Setting inside the qml part of my application. But what I do wrong if I would like to use it with my signal settingChanged?

Thanks!

Franz

After I've readed the Qt documentation very carefully, I've found the answer here

http://qt-project.org/doc/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#connecting-to-qml-signals

The documentation says

When a QML object type is used as a signal parameter, the parameter should use var as the type, and the value should be received in C++ using the QVariant type.

So, the signal definition inside the qml description of Setting has to be replaced with var.

import Setting 1.0

Button
{
    signal settingChanged(string, var)

    // ...
}

To connect the qml signal with the C++ slot, the code has to be

Object::connect(myButton, SIGNAL(settingChanged(QString, QVariant)), this, SLOT(settingChanged(QString, QVariant)));

At this point the connection worked!

To make the whole thing running, the QVariant has to be casted to the right type inside the slot. In my case to a Setting type.

void Foo::settingChanged(QString name, QVariant const & var)
{
    Setting * setting = qobject_cast<Setting*>(var.value<QObject*>());

    // ...
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set property type of qml signal?

From Dev

How to set property type of qml signal?

From Dev

Qt-how to connect a function to a signal in qml with python? using QQmlApplicationEngine

From Dev

QML signal QT slot with QQuickView

From Dev

How to pass enumerator type as a signal paramater in Qt

From Dev

How to register a class for use it in a QWebChannel signal in Qt

From Dev

how to use a custom signal with QStateMachine addtransition

From Dev

How to catch C++ signal in QML signal handler after type registration?

From Dev

How to catch C++ signal in QML signal handler after type registration?

From Dev

How to connect the signal valueChanged from QLineEdit to a custom slot in Qt

From Dev

How can I return the date in a custom format in this Qt/QML plasmoid?

From Dev

How can I return the date in a custom format in this Qt/QML plasmoid?

From Dev

Qt custom QPushButton clicked signal

From Dev

How to register singleton qml type with std function for a callback in Qt?

From Dev

How to use QML_IMPORT_PATH with Qt Cmake Project?

From Dev

How to use/ deploy Qt Sensors Qml app to my device

From Dev

How to use Custom type for @PathParam?

From Dev

How to use a custom type in mongoose?

From Dev

How to use Qt properties with custom classes?

From Dev

QML: Type Error with custom QObject

From Dev

"TypeError: native Qt signal is not callable" with custom slots

From Dev

Is it possible to disconnect all slots from a signal in Qt5 QML?

From Dev

Sending a signal to a QML item from C++ (Qt5)

From Dev

QT QML signal does not get to a Canvas when defined in style: ButtonStyle

From Dev

QT5 C++ Signal to QML Slot not working

From Dev

How to embedd a Qt widget in QML?

From Dev

How to use SIGFPE with signal?

From Dev

How to use custom type annotations in Java

From Dev

What is the use of the ui.qml files in Qt5 (QML)?

Related Related

  1. 1

    How to set property type of qml signal?

  2. 2

    How to set property type of qml signal?

  3. 3

    Qt-how to connect a function to a signal in qml with python? using QQmlApplicationEngine

  4. 4

    QML signal QT slot with QQuickView

  5. 5

    How to pass enumerator type as a signal paramater in Qt

  6. 6

    How to register a class for use it in a QWebChannel signal in Qt

  7. 7

    how to use a custom signal with QStateMachine addtransition

  8. 8

    How to catch C++ signal in QML signal handler after type registration?

  9. 9

    How to catch C++ signal in QML signal handler after type registration?

  10. 10

    How to connect the signal valueChanged from QLineEdit to a custom slot in Qt

  11. 11

    How can I return the date in a custom format in this Qt/QML plasmoid?

  12. 12

    How can I return the date in a custom format in this Qt/QML plasmoid?

  13. 13

    Qt custom QPushButton clicked signal

  14. 14

    How to register singleton qml type with std function for a callback in Qt?

  15. 15

    How to use QML_IMPORT_PATH with Qt Cmake Project?

  16. 16

    How to use/ deploy Qt Sensors Qml app to my device

  17. 17

    How to use Custom type for @PathParam?

  18. 18

    How to use a custom type in mongoose?

  19. 19

    How to use Qt properties with custom classes?

  20. 20

    QML: Type Error with custom QObject

  21. 21

    "TypeError: native Qt signal is not callable" with custom slots

  22. 22

    Is it possible to disconnect all slots from a signal in Qt5 QML?

  23. 23

    Sending a signal to a QML item from C++ (Qt5)

  24. 24

    QT QML signal does not get to a Canvas when defined in style: ButtonStyle

  25. 25

    QT5 C++ Signal to QML Slot not working

  26. 26

    How to embedd a Qt widget in QML?

  27. 27

    How to use SIGFPE with signal?

  28. 28

    How to use custom type annotations in Java

  29. 29

    What is the use of the ui.qml files in Qt5 (QML)?

HotTag

Archive