QSignalMapper不会触发映射

时间:2015-11-09 15:30:33

标签: c++ qt qml qsignalmapper

我最近遇到了QSignalMapper的问题。我无法将信号映射(QObject *)触发

我目前有一个qml对象keyboard.qml已分配并从c ++类keyboardManager.cpp中删除。在keyboard.qml我有以下信号:

signal keyboardOpening
signal keyboardOpened
signal keyboardClosing
signal keyboardClosed

和这个功能:

function close() {
    popDelay.stop();
    closing = true;
    keyboardClosing();
    state = "OFFSCREEN";
}

信号

signal keyboardClosed
当与状态更改相关的所有动画完成时触发

在c ++中,我有一个方法和一个插槽

//method
void KeyboardManager::beginRemoveKeyboard(TouchTextField *target);
//slot
void KeyboardManager::endRemoveKeyboard(QObject *target);

在beginRemoveKeyboard中我尝试将keyboardClosed连接到endRemoveKeyboard,这样我就可以释放内存并正确注销所有指针,所以我做了这个测试:

在QML中

onKeyboardClosed: console.debug("closed");

在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
}

然而,当我调用KeyboardManager :: beginRemoveKeyboard并得到:

[DEBUG]closed (qrc:///main.qml:24, )
[DEBUG] test (tmp/debug/moc/../../../include/keyboard/KeyboardManager.h:31, void KeyboardManager::test())

就是这样。没有“删除已开始”,这意味着KeyboardClosed()已被触发,test()被调用但endRemoveKeyboard(QObject*)未被调用。为什么以及如何实现我的目标?

0 个答案:

没有答案