QT之DisConnect

一些代码:

A类:

//A.h
#pragma once
#include "qobject.h"
class A:public QObject 
{
    Q_OBJECT

public:
    A(void);
    ~A(void);
public slots:
    virtual void aSlots();
    virtual void aSlots0();
};

A.cpp
#include "A.h"
A::A(void)
{
}
A::~A(void)
{
}
void A::aSlots()
{
}
void A::aSlots0()
{
}

 

B类:
B。h
#pragma once
#include "a.h"
class B :
    public A
{
    Q_OBJECT
signals:
    void callBack();

public:
    B(void);
    ~B(void);

public slots:
    void aSlots();
    void aSlots0();
};

B.cpp
#include "B.h"
B::B(void)
{

}

B::~B(void)
{
}

void B::aSlots()
{
    emit callBack();
}

void B::aSlots0()
{
}

 

D类:
D。h
#pragma once
#include "a.h"
class D :
    public A
{
    Q_OBJECT

public:
    D(void);
    ~D(void);

    public slots:
        void aSlots();
        void aSlots0();
};


D.cpp
#include "D.h"
D::D(void)
{
}

D::~D(void)
{
}

void D::aSlots()
{
}

void D::aSlots0()
{
}

 

F类:
F。h
F.cpp#pragma once
#include "qobject.h"
#include "A.h"
class F:public QObject
{
    Q_OBJECT
public:
    F(void);
    ~F(void);
signals:
    void testS();
    void testS0();
    public slots:
        void reCallBack();
private:
       A *a;

};

F.cpp
#include "A.h"
#include "B.h"
#include "D.h"
#include "F.h"

F::F(void)
{
    bool b = false;
    a = new B();
    b = QObject::connect(a, SIGNAL(callBack()), this, SLOT(reCallBack()));
    b = QObject::connect(this, SIGNAL(testS()), a, SLOT(aSlots()));
    b = QObject::connect(this, SIGNAL(testS0()), a, SLOT(aSlots0()));
    emit testS();//执行,把b对象的连接断开,连接到d中
    emit testS0();//不会执行,因b已经断开
    emit testS();//执行d相应的槽
}
F::~F(void)
{
}

void F::reCallBack()
{
    bool b = false;
    //b = QObject::disconnect(a);
    b = this->disconnect(a);
    a = new D();
    b = QObject::connect(this, SIGNAL(testS()), a, SLOT(aSlots()));
}

 各个方法设置断点,直接使用用F来运行,可以看到效果。

参与的内容:

关于信号与槽的研究

DisConnect:

bool QObject::disconnect(const QObject * sender, const char * signal, const QObject * receiver, const char *method) [static]

1.    Disconnect everything connected to an object's signals:

取消某个对像的所有的信号连接:

disconnect(myObject,0,0,0);

equivalent to the non-static overloaded function

等价于:

myObject->disconnect();

2.    Disconnect everything connected to a specific signal:

取消某个信号与它对应槽的所有连接:

disconnect(myObject, SIGNAL(mySignal()),0,0);

equivalent to the non-static overloaded function

等价于:

myObject->disconnect(SIGNAL(mySignal()));

3.    Disconnect a specific receiver:

断开某个接收对象的连接:

disconnect(myObject,0, myReceiver,0);

equivalent to the non-static overloaded function

等价于:

myObject->disconnect(myReceiver);

 

bool QObject::disconnect(const QObject * sender, PointerToMemberFunction signal, const QObject * receiver,PointerToMemberFunction method) [static]

1.    Disconnect everything connected to an object's signals:

disconnect(myObject,0,0,0);

2.    Disconnect everything connected to a specific signal:

disconnect(myObject,&MyObject::mySignal(),0,0);

3.    Disconnect a specific receiver:

disconnect(myObject,0, myReceiver,0);

4.    Disconnect a connection from one specific signal to a specific slot:

QObject::disconnect(lineEdit,&QLineEdit::textChanged,

label,  &QLabel::setText);

0 may be used as a wildcard, meaning "any signal", "any receiving object", or "any slot in the receiving object", respectively.

The sender may never be 0. (You cannot disconnect signals from more than one object in a single call.)

If signal is 0, it disconnects receiver and method from any signal. If not, only the specified signal is disconnected.

If receiver is 0, it disconnects anything connected to signal. If not, slots in objects other than receiver are not disconnected.

If method is 0, it disconnects anything that is connected to receiver. If not, only slots named method will be disconnected, and all other slots are left alone. The method must be 0 if receiver is left out, so you cannot disconnect a specifically-named slot on all objects.

 

 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值