我最近遇到了 QSignalMapper 的问题。我无法让信号映射 (QObject*) 触发
我目前有一个keyboard.qml
从 c++ 类中分配和删除的 qml 对象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*)
不是。为什么以及如何实现我想要的?