0

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?

dev-masih
  • 4,188
  • 3
  • 33
  • 55
Nyashes
  • 652
  • 4
  • 22
  • Do you solve the problem if you put the line `signalMapper->setMapping(this, (QObject*)target);` after `QObject::connect(signalMapper, SIGNAL(mapped(QObject*)), this, SLOT(endRemoveKeyboard(QObject*)));`? – Tarod Nov 10 '15 at 08:35
  • I just tried QObject::connect(signalMapper, SIGNAL(mapped(QObject*)), this, SLOT(endRemoveKeyboard(QObject*))); signalMapper->setMapping(this, (QObject*)target); still not working – Nyashes Nov 10 '15 at 11:17
  • Try to set `signalMapper` as a member variable, not a local variable. Maybe it's due to the pointer is deleted when `KeyboardManager::beginRemoveKeyboard` ends. – Tarod Nov 10 '15 at 11:37
  • still not working. I decided to create a class that basically do the same thing as QSignalMapper and it works now. Thanks for your help, at least you tried too. – Nyashes Nov 10 '15 at 13:18
  • OK, but... Sorry :( anyway, thanks to you and happy coding! – Tarod Nov 10 '15 at 16:18

0 Answers0