1

全部!我有一些带有 3 个选项卡的应用程序,一个选项卡从另一个选项卡初始化 QWidget。我在构造函数中启动 QAudioInput(读取到内存,我不需要文件) - 它开始工作。但是当我将选项卡更改为我的小部件时,数据停止并且 audioInput 的状态仍然为 isActive。我已经更改并最小化了来源。它是针对 ARM Linux 平台的交叉编译。现在它可以正常工作,但很少更改活动选项卡崩溃流。

int main(int argc, char *argv[])
{
    QApplication myapp(argc, argv);
    Widget wid;
    wid.show();
    return myapp.exec();
}
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    // Default widget in new tab
    wMetod = new Vnimi(this);//this
    ui->tabWidget->insertTab(1, wMetod, "Empty"); // Set Tab's name

    ui->pButton_Vnimi->setChecked(true);
    /*QButtonGroup **/
    grMetod = new QButtonGroup(this);
    grMetod->addButton(ui->pButton_Vnimi );
    grMetod->addButton(ui->pButton_ExpVibro);
    grMetod->setExclusive(true);
    connect(grMetod, static_cast<void(QButtonGroup::*)(QAbstractButton *)>(&QButtonGroup::buttonPressed),
        [=](QAbstractButton *button){ metChanged(button); });

    ui->tabWidget->setCurrentIndex(0);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::metChanged(QAbstractButton *button)
{
    /* switch for strings */
    if( button->objectName() =="pButton_Empty" ) {
        ui->tabWidget->removeTab(1);
        wMetod->disconnect(); delete wMetod;
        wMetod = new Vnimi(this);//this
        ui->tabWidget->insertTab(1, wMetod, "Empty"); // Set Tab's name
    }else if( button->objectName() =="pButton_Input" ) {
        ui->tabWidget->removeTab(1);
        wMetod->disconnect(); delete wMetod;
        wMetod = new VibrExpr(this);//this
        ui->tabWidget->insertTab(1, wMetod, "Input"); // Set Tab's name
    }else {
        qDebug() << "Unknow method!";
    }
    qDebug() << button->objectName() << " pressed";
}
VibrExpr::VibrExpr(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VibrExpr)
{
    ui->setupUi(this);
    ui->lb_level->setText( QString().number(minLevel) );

    QAudioFormat frmt;
    frmt.setCodec( "audio/pcm"), frmt.setChannelCount( 2);
    frmt.setSampleRate( 44100), frmt.setSampleType( QAudioFormat::SignedInt);
    frmt.setSampleSize( 16);

    rawSrc = new QAudioInput(QAudioDeviceInfo::defaultInputDevice(), frmt, this);
    rawSrc->setNotifyInterval(250);
    connect(rawSrc, SIGNAL(notify()), this, SLOT(slot()));
    connect(rawSrc, SIGNAL(stateChanged(QAudio::State)), this,  SLOT(iStateChanged(QAudio::State)));

    buff = new QBuffer(this);
    buff->open(QBuffer::ReadWrite);
    connect(buff, SIGNAL(readyRead()), this, SLOT(newData()) );

    rawSrc->start(buff);

    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this,  SLOT(timSlot()) );
    timer->start(1000);

    qDebug() << "Created";
}

void VibrExpr::timSlot()
{
    int sbVal = ui->lb_level->text().toInt();
    if(sbVal < -6 ) {
        sbVal += 1;
    }
    if(buff != nullptr)  \
    if(buff->isOpen() && sbVal >= -10) {
        qDebug() << "Sz " << buff->size();
        rawSrc->stop();
        buff->close();
        qDebug() << " Input closed";
    }
    ui->lb_level->setText( QString().number(sbVal));
    if(rawSrc != nullptr)  qDebug() << "iStat " << rawSrc->state();
}

void VibrExpr::newData(void)
{
    int k = buff->size();
    QByteArray qba = buff->read(k);
    buff->seek(0);
    buff->buffer().remove(0, k);
    qDebug() << "n=" << k;
}

void VibrExpr::slot(void)
{
    qDebug() << "USec:" << rawSrc->processedUSecs() ;
}

void VibrExpr::iStateChanged(QAudio::State state)
{
    qDebug() << "Now state: " << state;
}
4

0 回答 0