我在愚蠢的问题上磕磕绊绊,而且我对Qt很陌生。
我有一个类( SoundSampler )继承来自基类( BaseSampler )的信号,并且此信号在UI构造函数中连接( MainWindow < / em>)到UI中的一个插槽( sampleAvailable())。
即使连接发生正确( connect()在UI类中返回true, isSignalconnected 在 SoundSampler 类中也返回true) , 从不调用插槽。 .................................................. ...............................
这是我的代码(修剪到要点):
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
}
主窗口
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;
}
答案 0 :(得分:1)
好的,我解决了。
问题不在MainWindow类中,而是在调用它的类中... 这是我的同事严重执行的(MainWindow实例化对象仅在构造函数中,而不是作为类的成员)。
因此,一旦构造函数完成,插槽就会被取消注册。
(抱歉这个烂摊子,无论如何都要感谢vahancho;)