I've recently encoutered a problem with QSignalMapper. I can't get the signal mapped (QObject*) to trigger
I currently have a qml object keyboard.qml
allocated and removed from a c++ class keyboardManager.cpp
. In keyboard.qml
I have the following signals:
signal keyboardOpening
signal keyboardOpened
signal keyboardClosing
signal keyboardClosed
and this function:
function close() {
popDelay.stop();
closing = true;
keyboardClosing();
state = "OFFSCREEN";
}
the signal
signal keyboardClosed
is triggered when all animations related to the state change are finished
In c++ I have a method and a slot
//method
void KeyboardManager::beginRemoveKeyboard(TouchTextField *target);
//slot
void KeyboardManager::endRemoveKeyboard(QObject *target);
In beginRemoveKeyboard I try to connect keyboardClosed to endRemoveKeyboard so I can free my memory and unregister all pointers properly so I did this test:
in QML:
onKeyboardClosed: console.debug("closed");
in C++:
void KeyboardManager::test() { qDebug() << "test"; }
void KeyboardManager::beginRemoveKeyboard(TouchTextField *target)
{
NewKeyboard *currentKeyboard = m_keyboards.value(target);
QSignalMapper* signalMapper = new QSignalMapper(this);
QObject::connect(currentKeyboard, SIGNAL(keyboardClosed()), signalMapper, SLOT(map()));
QObject::connect(currentKeyboard, SIGNAL(keyboardClosed()), this, SLOT(test()));
signalMapper->setMapping(this, (QObject*)target);
QObject::connect(signalMapper, SIGNAL(mapped(QObject*)), this, SLOT(endRemoveKeyboard(QObject*)));
QMetaObject::invokeMethod(currentKeyboard, "close");
}
void KeyboardManager::endRemoveKeyboard(QObject *target)
{
qDebug() << "deletion started";
TouchTextField* actualTarget = qobject_cast<TouchTextField*>(target);
NewKeyboard *currentKeyboard = m_keyboards.value(actualTarget);
actualTarget->setKeyboard(NULL);
m_keyboards.remove(actualTarget);
delete currentKeyboard;
if (sender())
delete sender(); //QSignalMapper
}
however when I called KeyboardManager::beginRemoveKeyboard and got:
[DEBUG]closed (qrc:///main.qml:24, )
[DEBUG] test (tmp/debug/moc/../../../include/keyboard/KeyboardManager.h:31, void KeyboardManager::test())
and that's all. no "deletion started" which mean that KeyboardClosed()
is triggered, test()
is called but endRemoveKeyboard(QObject*)
is not. Why and how can I achieve what I want?