Solved Close connection qobject
-
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 {}???
-
@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.