我用一个信号连接一个插槽.但现在我想暂时断开它们.
这是我的班级声明的一部分:
class frmMain : public QWidget
{
...
private:
QTimer *myReadTimer;
...
private slots:
void on_btnDownload_clicked();
...
};
Run Code Online (Sandbox Code Playgroud)
在构造函数中frmMain,我连接myReadTimer一个插槽,以便ReadMyCom每5秒调用一次:
myReadTimer=new QTimer(this);
myReadTimer->setInterval(5000);
connect(myReadTimer,SIGNAL(timeout()),this,SLOT(ReadMyCom()));
Run Code Online (Sandbox Code Playgroud)
但是,在插槽中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
}
Run Code Online (Sandbox Code Playgroud)
我在Stackoverflow中搜索并获得了一些答案,比如调用QObject析构函数.但我不知道如何使用它.
我也试过用disconnect,比如:
QMetaObject::Connection myConnect;
myConnect=connect(myReadTimer,SIGNAL(timeout()),this,SLOT(ReadMyCom()));
...
disconnect(& …Run Code Online (Sandbox Code Playgroud) 我知道您只应该在标头中声明一个函数,并避免定义它,因为如果有多个源文件包含此标头,则链接器将告诉您有重复的符号。
我也知道建议在标头中声明一个类并在源文件中实现成员函数
但是,这是我的问题:我尝试在标头中定义整个类(包括成员函数的所有实现),然后发现当我将此标头包含在两个源文件中时,链接程序没有错误。
这是我的header.h文件
class ctr
{
public:
ctr();
ctr(char *s);
int showname(){return 0;}
private:
char *name;
};
Run Code Online (Sandbox Code Playgroud)
在其他两个文件中,我包含header.h
//file1.cpp
#include header.h
//file2.cpp
#include header.h
Run Code Online (Sandbox Code Playgroud)
然后编译它们 g++ file1.cpp file2.cpp
那么谁能告诉我为什么正常的函数定义会给我一个错误,但是类定义可以吗?