QSignalSpy error: No such signal when connecting a signal to a QSignalSpy

208 Views Asked by At

I am trying to test a Qt application that uses QML. Specifically, I want to test the connection between a signal emitted by a QML object and a slot in a C++ class.

Here's the relevant code I'm working with:

QQuickView view;
view.setSource(QUrl("qrc:/QML/monitoring/MonitoringMenu.qml")); // Path to the QML file
QQuickView view2;
view2.setSource(QUrl("qrc:/QML/monitoring/MonitoringAction.qml")); // Path to the QML file
QObject *object = view.rootObject();
QObject *object2 = view2.rootObject();

// Get the InspirationPausePanel object created in the QML file
QQuickItem *InspirationPausePanel = static_cast<QQuickItem*>(object->children().at(2));
Q_ASSERT(InspirationPausePanel != nullptr);

// Before clicking, check that the visible property is set to false
QCOMPARE(InspirationPausePanel->property("visible").toBool(), false);

// Get the InspirationPauseItem object created in the QML file
QQuickItem *InspirationPauseItem = static_cast<QQuickItem*>(object2->children().at(0));
Q_ASSERT(InspirationPauseItem != nullptr);

QObject::connect(object2, SIGNAL(actionClicked()), object2, SLOT(onActionClicked()));

// Connect the "actionClicked" signal of InspirationPauseItem to a QSignalSpy
QSignalSpy spy(object2, SIGNAL(actionClicked));
QVERIFY2(spy.isValid(), "signal not connected");

// Simulate a mouse click on the InspirationPauseItem object
QMetaObject::invokeMethod(InspirationPauseItem, "click");

// Check that the visible property of InspirationPausePanel is set to true
QCOMPARE(InspirationPausePanel->property("visible").toBool(), true);`

However, when I run the test, I get the following error:

QWARN  : MainwindowTest::clickButtonTest() QSignalSpy: No such signal: 'actionClicked'
FAIL!  : MainwindowTest::clickButtonTest() 'spy.isValid()' returned FALSE. (signal not connected)
..`/FHF_App/mainwindowtest.cpp(76) : failure location

It seems like the actionClicked signal is not being recognized. Please, can anyone help me figure out what I'm doing wrong?

1

There are 1 best solutions below

1
On

For those who also stumbled upon this question, I want to say that Everything is much simpler. You just need the signal from C++ to have QVariant arguments. For example:

QSignalSpy spy(object2, SIGNAL(actionClicked(QVariant)));

My signal is declared like this:

signal actionClicked(var actionMode)