继承对象中的信号和槽问题[Qt5]

Dán*_*agy 2 qt connect

我创建了一个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
Run Code Online (Sandbox Code Playgroud)

的.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;
}
Run Code Online (Sandbox Code Playgroud)

错误:

QObject::connect: No such slot QSoundEffect::asetTimer() in ../rob3/mysoundeffect.cpp:6
Run Code Online (Sandbox Code Playgroud)

mad*_*uci 5

您在构造函数之前忘记了关键字Q_OBJECT.没有它,信号/插槽机制就无法工作.

  • 天啊..对我感到羞耻.谢谢!:) (2认同)