【发布时间】:2015-04-15 23:45:25
【问题描述】:
我用信号连接了一个插槽。但现在我想暂时断开它们。
这是我的班级声明的一部分:
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相同的参数。 -
我看了disconnect的文档发现
static bool disconnect(const QMetaObject::Connection &);那我怎么错了? -
您使用 QObject::disconnect() 的示例是正确的,除了该示例在预期引用的位置传递了一个指针 - 所以它甚至不应该编译!
disconnect(myConnect)工作。
标签: qt signals-slots