How to connect with ButtonGroup clicked(AbstractButton) signal?
-
Documentation says that ButtonGroup emits signal clicked(AbstractButton).
My C++ code:
QObject *obj = rootObject->findChild<QObject*>("groupName"); QObject::connect((QObject *)obj, SIGNAL(clicked(QAbstractButton)), &mainWindow, SLOT(onButtonClicked()));
The code generates an error message:
QObject::connect: No such signal ButtonGroup_QMLTYPE_127::clicked(QAbstractButton)
What am I doing wrong?
Thanks! -
Documentation says that ButtonGroup emits signal clicked(AbstractButton).
My C++ code:
QObject *obj = rootObject->findChild<QObject*>("groupName"); QObject::connect((QObject *)obj, SIGNAL(clicked(QAbstractButton)), &mainWindow, SLOT(onButtonClicked()));
The code generates an error message:
QObject::connect: No such signal ButtonGroup_QMLTYPE_127::clicked(QAbstractButton)
What am I doing wrong?
Thanks!The signal sends an AbstractButton not a QAbstractButton.
-
The signal sends an AbstractButton not a QAbstractButton.
-
@ChrisW67 - I tried that but get similar error:
QObject::connect((QObject *)obj, SIGNAL(clicked(AbstractButton)), &mainWindow, SLOT(onButtonClicked()));
QObject::connect: No such signal ButtonGroup_QMLTYPE_7::clicked(AbstractButton)
@Tom-asso What about
SIGNAL(clicked(QAbstractButton*))
, I mean normally this kind of signal has a pointer as param, not object.
I don't quite know qml, this is just a guess.And there's a way to check the signals is
obj->dumpObjectInfo();
then look what's printed in the console.
-
@Tom-asso What about
SIGNAL(clicked(QAbstractButton*))
, I mean normally this kind of signal has a pointer as param, not object.
I don't quite know qml, this is just a guess.And there's a way to check the signals is
obj->dumpObjectInfo();
then look what's printed in the console.
@Bonnie - ah, that is a useful function, but I don't understand the signals information it gives. Below is what it says for the ButtonGroup (my QML gives it objectName "editModesObj").
Weird - no info on signal 'clicked()'! What is going on?OBJECT ButtonGroup_QMLTYPE_124::editModesObj SIGNALS OUT signal: destroyed(QObject*) --> QQuickColumnLayout::unnamed _q_resourceObjectDeleted(QObject*) SIGNALS IN <-- RadioButton_QMLTYPE_126::unnamed <unknown> <-- RadioButton_QMLTYPE_126::unnamed <unknown> <-- RadioButton_QMLTYPE_126::unnamed <unknown> <-- RadioButton_QMLTYPE_126::unnamed <unknown> <-- RadioButton_QMLTYPE_126::unnamed <unknown> <-- RadioButton_QMLTYPE_126::unnamed <unknown> <-- RadioButton_QMLTYPE_126::unnamed <unknown> <-- RadioButton_QMLTYPE_126::unnamed <unknown> <-- RadioButton_QMLTYPE_126::unnamed <unknown> <-- RadioButton_QMLTYPE_126::unnamed <unknown>
... and my C++ connects successfully to the ButtonGroup's destroyed() signal. My QML also declares a Slider, and my C++ can connect to its valueChanged() signal without problem. But dumpObjectInfo() for the slider does not mention the valueChanged() signal - but a signal "baselineOffsetChanged(double) - now I'm really confused!
OBJECT Slider_QMLTYPE_7::pingStepSliderObj SIGNALS OUT signal: baselineOffsetChanged(double) <functor or function pointer> --> QQuickColumnLayout::unnamed invalidateSenderItem() SIGNALS IN <-- Slider_QMLTYPE_7::pingStepSliderObj <unknown>
-
@Bonnie - ah, that is a useful function, but I don't understand the signals information it gives. Below is what it says for the ButtonGroup (my QML gives it objectName "editModesObj").
Weird - no info on signal 'clicked()'! What is going on?OBJECT ButtonGroup_QMLTYPE_124::editModesObj SIGNALS OUT signal: destroyed(QObject*) --> QQuickColumnLayout::unnamed _q_resourceObjectDeleted(QObject*) SIGNALS IN <-- RadioButton_QMLTYPE_126::unnamed <unknown> <-- RadioButton_QMLTYPE_126::unnamed <unknown> <-- RadioButton_QMLTYPE_126::unnamed <unknown> <-- RadioButton_QMLTYPE_126::unnamed <unknown> <-- RadioButton_QMLTYPE_126::unnamed <unknown> <-- RadioButton_QMLTYPE_126::unnamed <unknown> <-- RadioButton_QMLTYPE_126::unnamed <unknown> <-- RadioButton_QMLTYPE_126::unnamed <unknown> <-- RadioButton_QMLTYPE_126::unnamed <unknown> <-- RadioButton_QMLTYPE_126::unnamed <unknown>
... and my C++ connects successfully to the ButtonGroup's destroyed() signal. My QML also declares a Slider, and my C++ can connect to its valueChanged() signal without problem. But dumpObjectInfo() for the slider does not mention the valueChanged() signal - but a signal "baselineOffsetChanged(double) - now I'm really confused!
OBJECT Slider_QMLTYPE_7::pingStepSliderObj SIGNALS OUT signal: baselineOffsetChanged(double) <functor or function pointer> --> QQuickColumnLayout::unnamed invalidateSenderItem() SIGNALS IN <-- Slider_QMLTYPE_7::pingStepSliderObj <unknown>
@Tom-asso Oh, sorry my bad, I thought this function prints all signals, but actually it only prints the signals that already got connected.
You can write code to print the signals though.
Here we starti
frommeta->methodOffset()
so it only prints signals defined in its class, if start from 0, then it will also print signals from superclasses.const QMetaObject* meta = obj->metaObject(); for (int i = meta->methodOffset(); i < meta->methodCount(); ++i) { QMetaMethod method = meta->method(i); if(method.methodType() == QMetaMethod::Signal) { qDebug() << method.methodSignature(); } }
-
As @Bonnie mentioned, u are missing * in the signal connection. Its supposed to be
QObject::connect((QObject *)obj, SIGNAL(clicked(QAbstractButton*)), &mainWindow, SLOT(onButtonClicked()));
@Sivan said in How to connect with ButtonGroup clicked(AbstractButton) signal?:
As @Bonnie mentioned, u are missing * in the signal connection. Its supposed to be
QObject::connect((QObject *)obj, SIGNAL(clicked(QAbstractButton*)), &mainWindow, SLOT(onButtonClicked()));
What could have been avoided, when using the function pointer based connection style...
Edit:
@Sivan OP is using the QMLsAbstractButton
, not QtWidgetsQAbstractButton
class -
@Sivan said in How to connect with ButtonGroup clicked(AbstractButton) signal?:
As @Bonnie mentioned, u are missing * in the signal connection. Its supposed to be
QObject::connect((QObject *)obj, SIGNAL(clicked(QAbstractButton*)), &mainWindow, SLOT(onButtonClicked()));
What could have been avoided, when using the function pointer based connection style...
Edit:
@Sivan OP is using the QMLsAbstractButton
, not QtWidgetsQAbstractButton
classA cleaner solution would be to expose some of your c++ objects to the QML and call one of its slots from QML when a button is clicked.
Don't connect to QML signals from c++.
https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c
https://doc.qt.io/qt-6/qtquick-bestpractices.html#exposing-data-from-c-to-qml
https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
https://youtu.be/vzs5VPTf4QQ?t=23m20s -
A cleaner solution would be to expose some of your c++ objects to the QML and call one of its slots from QML when a button is clicked.
Don't connect to QML signals from c++.
https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c
https://doc.qt.io/qt-6/qtquick-bestpractices.html#exposing-data-from-c-to-qml
https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
https://youtu.be/vzs5VPTf4QQ?t=23m20s -
@Tom-asso Oh, sorry my bad, I thought this function prints all signals, but actually it only prints the signals that already got connected.
You can write code to print the signals though.
Here we starti
frommeta->methodOffset()
so it only prints signals defined in its class, if start from 0, then it will also print signals from superclasses.const QMetaObject* meta = obj->metaObject(); for (int i = meta->methodOffset(); i < meta->methodCount(); ++i) { QMetaMethod method = meta->method(i); if(method.methodType() == QMetaMethod::Signal) { qDebug() << method.methodSignature(); } }
-
@Sivan said in How to connect with ButtonGroup clicked(AbstractButton) signal?:
As @Bonnie mentioned, u are missing * in the signal connection. Its supposed to be
QObject::connect((QObject *)obj, SIGNAL(clicked(QAbstractButton*)), &mainWindow, SLOT(onButtonClicked()));
What could have been avoided, when using the function pointer based connection style...
Edit:
@Sivan OP is using the QMLsAbstractButton
, not QtWidgetsQAbstractButton
class