断开信号失败

Disconnecting signals fails

在我的代码中,我想重新路由信号槽连接,即首先将槽连接到信号 1,在重新路由后它应该只连接到信号 2。为此我使用了 disconnect(this),其中 this 指的是拥有插槽的 class(它在 class 函数中)。此命令应从 class 断开所有来自外部的信号。不幸的是,我得到 false 作为 disconnect() 的 return 值。为什么我不能断开信号?是否有可能获得更多信息?
奇怪的是,在我下面的示例中,断开连接和重新连接也不起作用,我从 disconnect.
得到了相同的错误代码 如果我从 on_PushButton2_clicked()-函数中删除行 connect(this, &MainWindow::writeLine, class1, &TXClass::emit_signal);disconnect() 仍然是 returns "false",但我得到了预期的结果。

示例代码:
Mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    class1 = new TXClass("Class 1");
    class2 = new TXClass("Class 2");
    connect(this, SIGNAL(writeLine()), class1, SLOT(emit_signal()));
    connect(class1, SIGNAL(signal(QString)), this, SLOT(newText(QString)));
}

MainWindow::~MainWindow()
{
    delete class1;
    delete class2;
    delete ui;
}

void MainWindow::newText(QString text)
{
    ui->lineEdit->setText(text);
}

void MainWindow::on_pushButton_clicked()
{
    emit writeLine();
}

void MainWindow::on_pushButton_2_clicked()
{
    qDebug() << "Disconnect result: " << disconnect(this);
    connect(this, &MainWindow::writeLine, class2, &TXClass::emit_signal);
    connect(this, &MainWindow::writeLine, class1, &TXClass::emit_signal);
    connect(class2, &TXClass::signal, this, &MainWindow::newText);
    //The onliest signal I want to get now is from class2.
}

MainWindow.h:

#include <QDebug>
#include <txclass.h>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

signals:
    void writeLine(void);
public slots:
    void on_pushButton_clicked();
    void newText(QString text);
    void on_pushButton_2_clicked();

private:
    Ui::MainWindow *ui;

    TXClass *class1, *class2;
};

#endif // MAINWINDOW_H

TXClass.cpp:

#include "txclass.h"

TXClass::TXClass(QString name)
{
    TXClass::name = name;
}


void TXClass::emit_signal()
{
    emit signal(name);
}

TXClass.h:

#ifndef TXCLASS_H
#define TXCLASS_H
#include <QObject>

class TXClass : public QObject
{
    Q_OBJECT
private:
    QString name;
signals:
    void signal(QString string);
public slots:
    void emit_signal(void);
public:
    TXClass(QString name);
};

#endif // TXCLASS_H

我已经检查了 QObject::disconnect 是如何实现的,但我不知道如果您只指定接收者这应该如何工作。 QMetaObjectPrivate::disconnect will return immediately with false when sender is not specified. This means that second part of QObject::disconnect will no set res to true. The only other place you could get true out of is call to QInternal::activateCallbacks. But it doesn't look 就像它是特定于实例的,而不是一些全局的东西(我承认我不知道这到底是想做什么 :/)。

我认为行之有效的解决方案是这样的:

void MainWindow::second()
{
    qDebug() << "Disconnect result: " << class1->disconnect();

    connect(this, &MainWindow::writeLine, class2, &TXClass::emit_signal);
    connect(this, &MainWindow::writeLine, class1, &TXClass::emit_signal);
    connect(class2, &TXClass::signal, this, &MainWindow::newText);
    //The onliest signal I want to get now is from class2.
}