`
美丽的小岛
  • 浏览: 296965 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

QT之DisConnect

    博客分类:
  • QT
 
阅读更多

一些代码:

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.

 

 

分享到:
评论
1 楼 美丽的小岛 2014-09-30  
主要是理清哪两个对象之间的关系,是信号与所有槽的关系或者是槽与所有信号的关系;
另外是某个对象的所有信号对应槽的关系或是某个对象的槽对应所有信号的关系。

相关推荐

    qt 段错误 解决方案

    移植qt应用程序时 会出现段错误 主要介绍怎么解决该错误

    Trojan-Qt5-Windows.zip

    [Bug Fix] Fix privoxy port not release after disconnect [Bug Fix] No need to reconnect in order for PAC to take effect [Bug Fix] No checking http port if httpMode is not enabled 这是一个只有bug修复...

    Qt自定义日历

    m_widget->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::SubWindow); QHBoxLayout* layout = new QHBoxLayout; layout->setMargin(8); layout->setSpacing(0); m_calendarWidget...

    基于QT的飞机大战游戏设计与实现.zip

    通过键盘的WSAD方向键来控制战机实现上下左右移动,用到的技术是监听键盘按键,整个过程分为 keyPressEvent 和keyReleaseEvent,表示键盘按下和键盘释放信号,通过qt的connect和disconnect函数来将主时钟和战机的...

    QT使用注意

    具体效果还需要信号与槽所在线程是否为同一线程与否有不同的表现,disconnect可断开关联关系;  7) 支持信号与槽机制时,需要继承自QObject类或其子类并且在类声明开始时添加Q_OBJECT宏;  8) 自动关联信号与槽,...

    一次:连接Qt slotfunctorsignal并在执行后立即断开连接

    一次:连接Qt slotfunctorsignal并在执行后立即断开连接

    浅谈pyqt5中信号与槽的认识

    信号(Signal)和槽(Slot)是Qt中的核心机制,也是PyQt变成中对象之间进行通信的机制 在pyqt5中,每一个QObject对象和pyqt中所有继承自QWidget的控件都支持信号和槽 当信号发射时,连接槽函数将会自动执行,pyqt5中...

    Redis Desktop Manager 0.9.8

    RedisDesktopManager 0.9.8 版本已发布,Redis Desktop Manager(RedisDesktopManager, RDM)是一个快速、简单、支持跨平台的 Redis 桌面管理工具,基于 Qt 5 开发,支持通过 SSH Tunnel 连接。 该版本包含一些值得...

    Giveda_GObject_c++11_v1.5

    为了解决Qt信号槽(需要依赖moc机制和moc工具)的缺陷,我创造了本软件。 利用本软件,c++开发者可以在不依赖c++编译器之外的任何工具的前提下,实现c++中类与类之间的解耦合(class A对象与class B对象之间的解耦合...

    TCP-IP详解卷3:TCP事务协议

    17.15 PRU_DISCONNECT请求和 unp_disconnect函数 204 17.16 PRU_SHUTDOWN请求和unp_shutdown 函数 205 17.17 PRU_ABORT请求和unp_drop函数 206 17.18 其他各种请求 207 17.19 小结 209 第18章 Unix域协议:I/O和描述...

    kylin-nm

    kylin-nm is a Qt based applet and uses some interface provided by NetworkManager. It provides a GUI for users to connect or disconnect wired or wireless network which managed by NetworkManager. ...

    Enhanced Host Controller Interface Specification for Universal Serial Bus

    Enhanced Host Controller Interface Specification for Universal Serial Bus <br>1. INTRODUCTION...............................................................................................1 1.1...

Global site tag (gtag.js) - Google Analytics