Close connection qobject



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


  • Qt Champions 2016

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



  • Adding to @mrjj

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

    Is escucharMouse() is a slot?.





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



  • 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!



  • @BjornW ok



  • 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 :).



  • @BjornW said in Close connection qobject:

    m_connection

    thx a lot!!



  • Good luck :)



  • @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!



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



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

    }???


  • Qt Champions 2016

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



  • Do you inherit QMainWindow?

    class MainWindow : public QMainWindow
    


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



  • You have two definitions of slot1()?



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



  • i solved my problem but the connection still thx all



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



  • @BjornW i need to use different signals to close the connection? i put timeout because these slots will repeat each time i say. I'm interesting to solve this issue because it's very useful too.



  • @BjornW verMouse.setInterval(50); with this i was trying to stop the signal verMouse.stop() documentation https://doc.qt.io/qt-4.8/qtimer.html#stop



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

    You have connected the same signal to both escuchar/stopEscuchar! This makes no sense. The order in which the slots are called is undefined in this case.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.