本文介绍了无法连接基类的信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试连接一个BASE类信号时,Qt告诉我该信号不存在于DERIVED类。

When I try to connect a BASE class signal, Qt tells me the signal does not exists on the DERIVED class.

为什么?我如何告诉连接使用BASE类?

Why is that? How can I tell connect to use the BASE class?

namespace MyNamespace 
{

void register(Derived* derived)
{
    // ERROR MSG
    // QObject::connect: No such signal MyNamespace::Derived::baseSignal()

    QObject::connect( derived, SIGNAL(baseSignal()), 
                      foo,     SLOT(fooSlot())); 
}

class Base : public QObject
{
    Q_OBJECT

signals:
    void baseSignal();
};

class Derived : public QObject,
                public Base
{
    Q_OBJECT

signals:
    void derivedSignal();
};

} // namespace MyNamespace


推荐答案

在这种形式下会更好:

namespace MyNamespace 
{

class Base : public QObject
{
    Q_OBJECT

signals:
    void baseSignal();
};

class Derived : public Base
{
    Q_OBJECT

signals:
    void derivedSignal();
};

void registerX( Derived* derived )
{
    QObject::connect( derived, SIGNAL(baseSignal()), foo, SLOT(fooSlot()));
}

} // namespace MyNamespace

href =http://stackoverflow.com/questions/8578657/qobject-multiple-inheritance>此帖表示您不应继承QObject多次。

As this post says you shall not inherit QObject multiple times.

这篇关于无法连接基类的信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 18:28