我将一个槽与一个信号连接起来。但现在我想暂时断开它们之间的连接。
这是我的类声明的一部分:
class frmMain : public QWidget
{
...
private:
QTimer *myReadTimer;
...
private slots:
void on_btnDownload_clicked();
...
};
在frmMain
的构造函数中,我将myReadTimer
与一个槽连接,以便每5秒调用ReadMyCom
函数。myReadTimer=new QTimer(this);
myReadTimer->setInterval(5000);
connect(myReadTimer,SIGNAL(timeout()),this,SLOT(ReadMyCom()));
但是,在on_btnDownload_clicked
函数中,我不希望myReadTimer
在on_btnDownload_clicked
的作用域中发出任何信号。因此,我想在on_btnDownload_clicked
开始时断开它们的连接,并在最后重新连接它们。就像这样:
void frmMain::on_btnDownload_clicked()
{
//some method to disconnect the slot & singal
...//the code that I want myReadTimer to leave me alone
//some method to reconnect the slot & singal
}
我在 Stackoverflow 上搜索并得到一些答案,比如调用 QObject
析构函数。但是我不知道如何使用它。
我还尝试使用 disconnect
,例如:
QMetaObject::Connection myConnect;
myConnect=connect(myReadTimer,SIGNAL(timeout()),this,SLOT(ReadMyCom()));
...
disconnect(& myConnect);
但它仍然无法工作。那么有人能帮我如何做吗?
disconnect
的文档吗?它和connect
接受相同的参数。 - Retired Ninjastatic bool disconnect(const QMetaObject::Connection &);
。那么我错在哪里了? - ctxrrdisconnect(myConnect)
是正确的。 - user2365669