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. Close connection qobject
Forum Updated to NodeBB v4.3 + New Features

Close connection qobject

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 4 Posters 8.0k Views 1 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.
  • J Offline
    J Offline
    Jeronimo
    wrote on last edited by
    #1

    I have the follow code:

    void MainWindow::escucharMouse{
    QObject::connect(&mouselog::instance(), &mouselog::mouseEvent,
                         [](){
        });
    }
    

    I call it with this signal and slot:
    connect(&this->verMouse,SIGNAL(timeout()),this,SLOT(escucharMouse()));

    It's possible destroy the connection.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      What is the question ?
      Are you looking for disconnect ?
      http://doc.qt.io/qt-5/qobject.html#disconnect

      J 1 Reply Last reply
      2
      • Pradeep KumarP Offline
        Pradeep KumarP Offline
        Pradeep Kumar
        wrote on last edited by Pradeep Kumar
        #3

        Adding to @mrjj

        disconnect usage.
        disconnect(this->verMouse,SIGNAL(timeout()),this,SLOT(escucharMouse()));

        Is escucharMouse() is a slot?.

        Pradeep Kumar
        Qt,QML Developer

        J 1 Reply Last reply
        1
        • Pradeep KumarP Pradeep Kumar

          Adding to @mrjj

          disconnect usage.
          disconnect(this->verMouse,SIGNAL(timeout()),this,SLOT(escucharMouse()));

          Is escucharMouse() is a slot?.

          J Offline
          J Offline
          Jeronimo
          wrote on last edited by
          #4

          @Pradeep-Kumar yes

          1 Reply Last reply
          0
          • mrjjM mrjj

            Hi
            What is the question ?
            Are you looking for disconnect ?
            http://doc.qt.io/qt-5/qobject.html#disconnect

            J Offline
            J Offline
            Jeronimo
            wrote on last edited by Jeronimo
            #5

            @mrjj i connect one instance of one class of qobject and i connect with the upside way.. I dont know if it is the best way to connect calling the function using one slot. it's only to show my mouse events. But i tried to disconnect this qobject but not seems to work.

            1 Reply Last reply
            0
            • BjornWB Offline
              BjornWB Offline
              BjornW
              wrote on last edited by BjornW
              #6

              When connecting to a lambda, you need to save the returned QMetaObject::Connection. From the docs:

              QMetaObject::Connection QObject::connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
              

              So something like.... (Can't compile right now)

              void MainWindow::escucharMouse()
              {
                    auto connection = QObject::connect(&mouselog::instance(), &mouselog::mouseEvent, [](){ });
                    ...
                    QObject::disconnect(connection);      
              }
              
              

              EDIT: By "need to save" I mean that if you wish to disconnect a connection, you need to save the return value. If you're not going to, just leave it!

              J 1 Reply Last reply
              2
              • BjornWB BjornW

                When connecting to a lambda, you need to save the returned QMetaObject::Connection. From the docs:

                QMetaObject::Connection QObject::connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
                

                So something like.... (Can't compile right now)

                void MainWindow::escucharMouse()
                {
                      auto connection = QObject::connect(&mouselog::instance(), &mouselog::mouseEvent, [](){ });
                      ...
                      QObject::disconnect(connection);      
                }
                
                

                EDIT: By "need to save" I mean that if you wish to disconnect a connection, you need to save the return value. If you're not going to, just leave it!

                J Offline
                J Offline
                Jeronimo
                wrote on last edited by Jeronimo
                #7

                @BjornW ok

                1 Reply Last reply
                0
                • BjornWB Offline
                  BjornWB Offline
                  BjornW
                  wrote on last edited by
                  #8

                  If you want to disconnect elsewhere you need to save the connection object inside your class.

                  Again, I did not compile this. But it should give you an idea.

                  class MainWindow : public QMainWindow
                  {
                  ...
                  private:
                     QMetaObject::Connection m_connection;
                  };
                  
                  ...
                  
                  void MainWindow::escucharMouse()
                  {
                        m_connection = QObject::connect(&mouselog::instance(), &mouselog::mouseEvent, [](){ });
                  }
                  void MainWindow::stopEscucharMouse()
                  {
                        QObject::disconnect(m_connection ); 
                  }
                  

                  On a side note, today I learned that escuchar means listen. Which reminded me of the song "Equador" (by Sash) where they sing "Escuchame". Funny how things connect. And disconnect :).

                  J 1 Reply Last reply
                  3
                  • BjornWB BjornW

                    If you want to disconnect elsewhere you need to save the connection object inside your class.

                    Again, I did not compile this. But it should give you an idea.

                    class MainWindow : public QMainWindow
                    {
                    ...
                    private:
                       QMetaObject::Connection m_connection;
                    };
                    
                    ...
                    
                    void MainWindow::escucharMouse()
                    {
                          m_connection = QObject::connect(&mouselog::instance(), &mouselog::mouseEvent, [](){ });
                    }
                    void MainWindow::stopEscucharMouse()
                    {
                          QObject::disconnect(m_connection ); 
                    }
                    

                    On a side note, today I learned that escuchar means listen. Which reminded me of the song "Equador" (by Sash) where they sing "Escuchame". Funny how things connect. And disconnect :).

                    J Offline
                    J Offline
                    Jeronimo
                    wrote on last edited by Jeronimo
                    #9

                    @BjornW said in Close connection qobject:

                    m_connection

                    thx a lot!!

                    1 Reply Last reply
                    1
                    • BjornWB Offline
                      BjornWB Offline
                      BjornW
                      wrote on last edited by
                      #10

                      Good luck :)

                      J 1 Reply Last reply
                      2
                      • BjornWB BjornW

                        Good luck :)

                        J Offline
                        J Offline
                        Jeronimo
                        wrote on last edited by
                        #11

                        @BjornW your help me a lot thx but i think when someone use one hook for mouse events for example it's impossible to close this connection. thx again!

                        1 Reply Last reply
                        1
                        • BjornWB Offline
                          BjornWB Offline
                          BjornW
                          wrote on last edited by
                          #12

                          Post your code please. I don't understand. It should always be possible to close the connection.

                          J 2 Replies Last reply
                          1
                          • BjornWB BjornW

                            Post your code please. I don't understand. It should always be possible to close the connection.

                            J Offline
                            J Offline
                            Jeronimo
                            wrote on last edited by
                            #13

                            @BjornW maybe can be because in my main window class i defined with this way
                            class MainWindow : public QObject {

                            }???

                            mrjjM 1 Reply Last reply
                            0
                            • J Jeronimo

                              @BjornW maybe can be because in my main window class i defined with this way
                              class MainWindow : public QObject {

                              }???

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @Jeronimo
                              Actually all QWidgets are QObject too so it should not matter.

                              1 Reply Last reply
                              1
                              • BjornWB Offline
                                BjornWB Offline
                                BjornW
                                wrote on last edited by
                                #15

                                Do you inherit QMainWindow?

                                class MainWindow : public QMainWindow
                                
                                1 Reply Last reply
                                1
                                • BjornWB BjornW

                                  Post your code please. I don't understand. It should always be possible to close the connection.

                                  J Offline
                                  J Offline
                                  Jeronimo
                                  wrote on last edited by
                                  #16

                                  @BjornW i only did one function to connect:
                                  void MainWindow::escucharMouse()
                                  {
                                  m_connection = QObject::connect(&mouselog::instance(), &mouselog::mouseEvent, { });
                                  }
                                  Other for close:
                                  void MainWindow::stopEscucharMouse()
                                  {
                                  QObject::disconnect(m_connection );
                                  }
                                  i defined in headers:
                                  class MainWindow : public QMainWindow
                                  {
                                  ...
                                  public
                                  private:
                                  QMetaObject::Connection m_connection;
                                  public slots:
                                  void escucharMouse();
                                  void apagarMouse();
                                  and slots to call these functions (two buttons)
                                  };

                                  and in the slots i call the function one to open other to close like:

                                  void MainWindow::slot1(){
                                  m_connection = QObject::connect(&mouselog::instance(), &mouselog::mouseEvent, { });
                                  }

                                  void MainWindow::slot1(){
                                  QObject::disconnect(m_connection );
                                  }
                                  i'm using one hook to capture the events of my mouse.
                                  seems not to close connection and still capture events.

                                  1 Reply Last reply
                                  0
                                  • BjornWB Offline
                                    BjornWB Offline
                                    BjornW
                                    wrote on last edited by
                                    #17

                                    You have two definitions of slot1()?

                                    J 1 Reply Last reply
                                    1
                                    • BjornWB BjornW

                                      You have two definitions of slot1()?

                                      J Offline
                                      J Offline
                                      Jeronimo
                                      wrote on last edited by Jeronimo
                                      #18

                                      @BjornW i tried two different public slots like:

                                      connect(&this->verMouse,SIGNAL(timeout()),this,SLOT(escucharMouse()));
                                      connect(&this->verMouse,SIGNAL(timeout()),this,SLOT(stopEscucharMouse()));
                                      
                                      void MainWindow::escucharMouse()
                                      {
                                            m_connection = QObject::connect(&mouselog::instance(), &mouselog::mouseEvent, [](){ });
                                      }
                                      void MainWindow::stopEscucharMouse()
                                      {
                                            QObject::disconnect(m_connection ); 
                                      }
                                      

                                      with headers in private for metaobject::connection and slots in public slots.

                                      But now how can call one slot or other i call them for example with this way:

                                      escucharMouse(); -> to connect
                                      stopEscucharMouse(); -> dont listen more
                                      

                                      I put two different slots

                                      Works to connect but to disconnect not seems to work.
                                      inherit of qobject

                                      1 Reply Last reply
                                      0
                                      • J Offline
                                        J Offline
                                        Jeronimo
                                        wrote on last edited by
                                        #19

                                        i solved my problem but the connection still thx all

                                        1 Reply Last reply
                                        1
                                        • BjornWB Offline
                                          BjornWB Offline
                                          BjornW
                                          wrote on last edited by
                                          #20

                                          This does not seem right:

                                          connect(&this->verMouse,SIGNAL(timeout()),this,SLOT(escucharMouse()));
                                          connect(&this->verMouse,SIGNAL(timeout()),this,SLOT(stopEscucharMouse()));
                                          

                                          You connect the same signal. And why does "verMouse" have a a timeout? What is it?

                                          Anyway, since you say you solved your problem...

                                          J 2 Replies Last reply
                                          1

                                          • Login

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