我用信号连接一个插槽。但现在我想暂时断开它们的连接。
这是我的班级声明的一部分:
class frmMain : public QWidget
{
...
private:
QTimer *myReadTimer;
...
private slots:
void on_btnDownload_clicked();
...
};
在构造函数中frmMain
,我连接myReadTimer
有一个插槽,以便ReadMyCom
每 5 秒调用一次:
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
, like:
QMetaObject::Connection myConnect;
myConnect=connect(myReadTimer,SIGNAL(timeout()),this,SLOT(ReadMyCom()));
...
disconnect(& myConnect);
但它仍然不起作用。那么有人可以帮助我如何做到这一点吗?