C++ Qt用户界面插槽为';由从基类继承的信号调用

C++ Qt用户界面插槽为';由从基类继承的信号调用,c++,qt,inheritance,slot,qt-signals,C++,Qt,Inheritance,Slot,Qt Signals,我在一个愚蠢的问题上绊倒了,我对Qt是个新手 我有一个类(SoundSampler),它从基类(BaseSampler)继承一个信号,这个信号在UI构造函数(MainWindow)中连接到UI中的一个插槽(sampleAvailable()) 问题是: 即使连接正确发生(connect()在UI类中返回true,而isSignalconnected在SoundSampler类中也返回true), 插槽从未被调用。 徖 void SoundSampler::stopRecording(){

我在一个愚蠢的问题上绊倒了,我对Qt是个新手

我有一个类(SoundSampler),它从基类(BaseSampler)继承一个信号,这个信号在UI构造函数(MainWindow)中连接到UI中的一个插槽(sampleAvailable())

问题是: 即使连接正确发生(connect()在UI类中返回true,而isSignalconnected在SoundSampler类中也返回true), 插槽从未被调用。 徖

void SoundSampler::stopRecording(){
    ...
    mSample->append("test");
    emit sampleAvailable(mSample);
    qDebug() << "Signal emmited"; //this get properly displayed in output
}
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    window = new QWidget();
    ss = new SoundSampler();

    boutonStart = new QPushButton(tr("&Start"));

    layout = new QHBoxLayout;
    layout->addWidget(boutonStart);

    window->setLayout(layout);
    window->show();

    connect(boutonStart, SIGNAL(clicked()),
            ss, SLOT(getSample())); //This connection works
    //The getSample() starts a Timer witch successfully calls the stopRecording slot

    connect(ss, SIGNAL(sampleAvailable(QByteArray*)),
            this, SLOT(sampleHandler(QByteArray*))); //This connection should work
    //The connect returns true, indicating the connection happend.

}

//This slot is never called.
void MainWindow::sampleHandler(QByteArray *sample){
    qDebug() << "Passed Value: " << *sample;
}
以下是我的代码(精简到基本内容):

基准采样器

class BaseSampler : public QObject
{
    Q_OBJECT
public:
    explicit BaseSampler(QObject *parent = 0);
    void getSample();

signals:
    void sampleAvailable(QByteArray *returnSample);
public slots:
    virtual void getSample() = 0;

protected:
    QByteArray *mSample;
};
class SoundSampler : public BaseSampler
{
    Q_OBJECT
public:
    SoundSampler();

signals:

public slots:
    void stopRecording();
    void getSample();

private:
    QAudioInput *mAudioInput;
    QBuffer *mBuffer;
};
声音采样器

class BaseSampler : public QObject
{
    Q_OBJECT
public:
    explicit BaseSampler(QObject *parent = 0);
    void getSample();

signals:
    void sampleAvailable(QByteArray *returnSample);
public slots:
    virtual void getSample() = 0;

protected:
    QByteArray *mSample;
};
class SoundSampler : public BaseSampler
{
    Q_OBJECT
public:
    SoundSampler();

signals:

public slots:
    void stopRecording();
    void getSample();

private:
    QAudioInput *mAudioInput;
    QBuffer *mBuffer;
};

void SoundSampler::stopRecording(){
    ...
    mSample->append("test");
    emit sampleAvailable(mSample);
    qDebug() << "Signal emmited"; //this get properly displayed in output
}
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    window = new QWidget();
    ss = new SoundSampler();

    boutonStart = new QPushButton(tr("&Start"));

    layout = new QHBoxLayout;
    layout->addWidget(boutonStart);

    window->setLayout(layout);
    window->show();

    connect(boutonStart, SIGNAL(clicked()),
            ss, SLOT(getSample())); //This connection works
    //The getSample() starts a Timer witch successfully calls the stopRecording slot

    connect(ss, SIGNAL(sampleAvailable(QByteArray*)),
            this, SLOT(sampleHandler(QByteArray*))); //This connection should work
    //The connect returns true, indicating the connection happend.

}

//This slot is never called.
void MainWindow::sampleHandler(QByteArray *sample){
    qDebug() << "Passed Value: " << *sample;
}

void SoundSampler::stopRecording(){
    ...
    mSample->append("test");
    emit sampleAvailable(mSample);
    qDebug() << "Signal emmited"; //this get properly displayed in output
}
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    window = new QWidget();
    ss = new SoundSampler();

    boutonStart = new QPushButton(tr("&Start"));

    layout = new QHBoxLayout;
    layout->addWidget(boutonStart);

    window->setLayout(layout);
    window->show();

    connect(boutonStart, SIGNAL(clicked()),
            ss, SLOT(getSample())); //This connection works
    //The getSample() starts a Timer witch successfully calls the stopRecording slot

    connect(ss, SIGNAL(sampleAvailable(QByteArray*)),
            this, SLOT(sampleHandler(QByteArray*))); //This connection should work
    //The connect returns true, indicating the connection happend.

}

//This slot is never called.
void MainWindow::sampleHandler(QByteArray *sample){
    qDebug() << "Passed Value: " << *sample;
}
MainWindow::MainWindow(QWidget*父项):
QMainWindow(父级),
用户界面(新用户界面::主窗口)
{
window=newqwidget();
ss=新的声音采样器();
boutonStart=新的QPushButton(tr(“&Start”);
布局=新的QHBoxLayout;
布局->添加小部件(boutonStart);
窗口->设置布局(布局);
窗口->显示();
连接(boutonStart,信号(单击()),
ss,SLOT(getSample());//此连接有效
//getSample()启动一个计时器,该计时器成功调用stopRecording插槽
连接(ss,信号(样本可用(QByteArray*)),
这个插槽(sampleHandler(QByteArray*);//这个连接应该可以工作
//connect返回true,表示连接发生。
}
//这个插槽从未被调用。
void主窗口::sampleHandler(QByteArray*示例){
好的,我解决了

问题不在MainWindow类中,而是在调用它的类中。。。 我的同事实现得很糟糕(MainWindow实例化的对象只在构造函数中,而不是作为类的成员)

因此,一旦构造完成,插槽就被取消注册

(很抱歉弄得一团糟,谢谢你瓦汉乔;)

好的,我解决了

问题不在MainWindow类中,而是在调用它的类中。。。 我的同事实现得很糟糕(MainWindow实例化的对象只在构造函数中,而不是作为类的成员)

因此,一旦构造完成,插槽就被取消注册


(很抱歉搞得一团糟,谢谢你vahancho;)

如果你存储指向基类而不是派生类的指针,比如
BaseSampler*ss;
而不是
SoundSampler*ss;
没有改变,插槽也没有被调用。我还尝试将另一个信号连接到插槽:
connect(boutonStart,signal(单击()),此插槽(sampleHandler());
但也不起作用…仍在调查中。(我将sampleHandler更改为当然不需要参数)是否生成了主窗口的moc文件?是否尝试清理并再次运行qmake?如果存储指向基类而不是派生类的指针,即
BaseSampler*ss;
而不是
SoundSampler*ss;
没有更改,插槽也没有调用。我还尝试将另一个信号连接到插槽:
connect(boutonStart,SIGNAL(clicked()),this,SLOT(sampleHandler());
但它也不起作用…仍在调查。(我将sampleHandler更改为当然不需要参数)是否生成了MainWindow的moc文件?是否尝试清理并再次运行qmake?