我创建了MySoundEffect类,是因为我想通过使其能够返回自播放开始后经过的时间来增强其isPlaying()函数。所以我做了您在代码中看到的。
问题是,构造函数中的连接会引发错误。就像我连接到父母的asetTimer()插槽(当然不存在)一样。我在运行时用调试器检查了此指针,它指向MySoundEffect对象。
我究竟做错了什么?
。H
#ifndef MYSOUNDEFFECT_H
#define MYSOUNDEFFECT_H
#include <QSoundEffect>
#include <QElapsedTimer>
class MySoundEffect : public QSoundEffect
{
QElapsedTimer* timer;
public slots:
void asetTimer();
public:
MySoundEffect();
~MySoundEffect();
int isPlaying();
};
#endif // MYSOUNDEFFECT_H
.cpp
#include "mysoundeffect.h"
MySoundEffect::MySoundEffect() : QSoundEffect()
{
timer = new QElapsedTimer();
connect(this,SIGNAL(playingChanged()), this, SLOT(asetTimer()));
}
void MySoundEffect::asetTimer(){
if (QSoundEffect::isPlaying() == true){
timer->restart();
}
}
int MySoundEffect::isPlaying(){
if (QSoundEffect::isPlaying() == true){
return timer->elapsed();
}
else{
return -1;
}
}
MySoundEffect::~MySoundEffect(){
delete timer;
}
错误:
QObject::connect: No such slot QSoundEffect::asetTimer() in ../rob3/mysoundeffect.cpp:6
您在构造函数之前忘记了魔术关键字Q_OBJECT。没有它,信号/插槽机制将无法工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句