Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. properly formatting disconnect()
Forum Updated to NodeBB v4.3 + New Features

properly formatting disconnect()

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 375 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    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...

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #4

      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);
      
      1 Reply Last reply
      2
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by Christian Ehrlicher
        #2

        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.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        mzimmersM 1 Reply Last reply
        3
        • Christian EhrlicherC Christian Ehrlicher

          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.

          mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #3

          @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.

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #4

            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);
            
            1 Reply Last reply
            2
            • mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by
              #5

              @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.

              1 Reply Last reply
              0
              • mzimmersM mzimmers has marked this topic as solved on

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved