Domanda

I'm stumbling on a dumb problem and i'm quite new to Qt.

I have a class (SoundSampler) who inherit a signal from a base class (BaseSampler) and this signal is connected in the UI constructor (MainWindow) to a slot in the UI (sampleAvailable()).

The problem:

Even thought the connection happend properly (connect() returns true in the UI class and isSignalconnected returns also true in the SoundSampler class), the slot is never called. .................................................................................

Here is my code (trimmed to the essentials):

BaseSampler

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;
};

SoundSampler

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

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);

public slots:
    void sampleHandler(QByteArray*);

private:
    QWidget *window;
    SoundSampler *ss;
};

.................................................................................

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;
}
È stato utile?

Soluzione

Ok, I solved it.

The problem wasn't in the MainWindow class but in the class calling it... Which was badly implemented by my colleague (the MainWindow instantiated object was only in the constructor and not as a member of the class).

So the slot was unregistered once the constructor was done.

(Sorry for the mess and thank you anyway vahancho ;) )

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top