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.

 

 

你可能感兴趣的:(connect)