properly formatting disconnect()
-
Hi all -
I am having trouble understanding the docs on how to properly code a disconnect. As the docs show, there are many different flavors of this call, but most of my efforts don't even pass the Clang test, let alone compile. This one compiles and runs, but the disconnect fails:
// main.cpp QObject::connect(&timer, &Timer::timeout, activityModel, &ActivityModel::requestUpdate); // ActivityModel class ActivityModel : public QAbstractListModel { QObject *m_timerObject = nullptr; public slots: void ActivityModel::requestUpdate() { m_timerObject = QObject::sender(); ... void ActivityModel::processGetResponse() { bool b = this->disconnect(m_timerObject, SIGNAL(timeout()), this, SLOT(requestUpdate())); ...Can someone explain to me what I'm doing wrong here, and, if one of the other disconnect() calls is to be preferred here?
Thanks...
-
If you're doing this connect:
QObject::connect(&timer, &Timer::timeout, activityModel, &ActivityModel::requestUpdate);you can do the disconnect with the same parameters:
QObject::disconnect(m_timerObject, &Timer::timeout, this, &ActivityModel::requestUpdate);Alternatively you can store the connection result in a class member e.g.
QMetaObject::Connection connection_handle, set it at your connection site:activityModel->connection_handle = QObject::connect(&timer, &Timer::timeout, activityModel, &ActivityModel::requestUpdate);and then disconnect it via the handle:
QObject::disconnect(this->connection_handle); -
You can not mix string-based and pmf style - when you connect with pmf style you also have to disconnect with pmf style.
Also your example looks like a usage for a singleShot timer. -
You can not mix string-based and pmf style - when you connect with pmf style you also have to disconnect with pmf style.
Also your example looks like a usage for a singleShot timer.@Christian-Ehrlicher said in properly formatting disconnect():
You can not mix string-based and pmf style - when you connect with pmf style you also have to disconnect with pmf style.
OK, that's good to know. Here's what I had to do; there must be a simpler way, but I couldn't find it:
const QMetaMethod &mmSignal = m_timerObject->metaObject()->method(m_timerObject->metaObject()->indexOfMethod(SLOT(requestUpdate()))); const QMetaMethod &mmSlot = this->metaObject()->method(this->metaObject()->indexOfSlot(SLOT(requestUpdate()))); bool b = disconnect(m_timerObject, mmSignal, this, mmSlot);Also your example looks like a usage for a singleShot timer.
It is.
Thanks for any suggestions on the above.
-
If you're doing this connect:
QObject::connect(&timer, &Timer::timeout, activityModel, &ActivityModel::requestUpdate);you can do the disconnect with the same parameters:
QObject::disconnect(m_timerObject, &Timer::timeout, this, &ActivityModel::requestUpdate);Alternatively you can store the connection result in a class member e.g.
QMetaObject::Connection connection_handle, set it at your connection site:activityModel->connection_handle = QObject::connect(&timer, &Timer::timeout, activityModel, &ActivityModel::requestUpdate);and then disconnect it via the handle:
QObject::disconnect(this->connection_handle); -
@Chris-Kawa thanks for that suggestion. I modified your first option (with some help from copilot):
class ScheduleModel : public QAbstractListModel { Q_OBJECT MessageManager *messageManager; void handler() { messageManager = qobject_cast<MessageManager *>(sender()); QObject::disconnect(messageManager, &MessageManager::connectedToHost, this, &ScheduleModel::requestUpdate); ...And that seems to work.
-
M mzimmers has marked this topic as solved on