有人可以向我解释为什么在基类插槽中没有调用覆盖的方法,而是我有一个基本版本的方法:
class ThreadsDispatcher : public QObject
{
Q_OBJECT
public:
explicit ThreadsDispatcher(QObject *parent = 0);
virtual ~ThreadsDispatcher();
virtual void OnThreadFinished(IThreadable *pWorker);
public slots:
void slotThreadFinished(IThreadable *pWorker);
};
void ThreadsDispatcher::slotThreadFinished(IThreadable *pWorker)
{
OnThreadFinished(pWorker);
}
void ThreadsDispatcher::OnThreadFinished(IThreadable *pWorker)
{
qDebug << "Base method, class" << this->metaObject()->className();
}
一个子类:
class CommandsQueueDispatcher : public ThreadsDispatcher
{
Q_OBJECT
public:
explicit CommandsQueueDispatcher(CommandFactory* baseFactory, QObject *parent = 0);
~CommandsQueueDispatcher();
void OnThreadFinished(IThreadable *pWorker);
};
void CommandsQueueDispatcher::OnThreadFinished(IThreadable *pWorker)
{
qDebug << "Subclass method, class" << this->metaObject()->className();
}
在插槽中调用 OnThreadFinished 后,我得到:
Base method, class ThreadsDispatcher
如果我从另一个方法调用方法 OnThreadFinished ,我会正常:
Subclass method, class CommandsQueueDispatcher
我试图连接基类和子类,但没有任何变化:
connect(pThreadWorker, SIGNAL(sigFinished(IThreadable*)), this, SLOT(slotThreadFinished(IThreadable*)));
但是如果我从另一个类连接,即既不是子类也不是基类:
connect(pThreadWorker, SIGNAL(sigFinished(IThreadable*)), pWorker, SLOT(slotThreadFinished(IThreadable*)));
我需要this
用变量替换的地方ptr
,我得到一个正常的结果。
我连接的功能:
bool ThreadsDispatcher::AddThread(IThreadable* pThreadWorker)
{
connect(pThreadWorker, SIGNAL(sigFinished(IThreadable*)), this, SLOT(slotThreadFinished(IThreadable*)));
}
我不直接实例化 ThreadsDispatcher。我创建了 CommandsQueueDispatcher 的非静态对象。