1

I am trying to record sound by QAudioInput. According to the doc in this website QAudioInput. But when I ran, it exported an empty-raw file. After checking, It seems like the function QTimer::singleShot didn't working ( I added statement qWarning << "Done" in void stopRecording() and It didn't display "Done" so I thought it had some mistake in QTimer::singleShot function ).

This is my code used to check function QTimer::singleShot

----Check.pro----
QT += core
QT -= gui
TARGET = Check
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
HEADERS += test.h

-----test.h------
#ifndef TEST_H
#define TEST_H

#include <QCoreApplication>
#include <QTimer>
#include <iostream>
#include <QObject>
#include <test.h>
#include <QDebug>

using namespace std;

class Object: public QObject {
   Q_OBJECT
private slots:
  void func() { cout << "Hello"; }
};

#endif // TEST_H

----main.cpp----
#include <QCoreApplication>
#include <QTimer>
#include <iostream>
#include <QObject>
#include <test.h>
#include <QDebug>

using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Object *o = new Object;
    QTimer::singleShot(10000, o, SLOT(func()));
    return 0;
}

And this code doesn't working, too. Can anyone explain me? I am newbie at Qt-programming.

4

1 回答 1

2

您的程序在设置计时器后立即退出 - 它没有时间触发。

要使计时器工作,您需要运行一个事件循环。如果没有事件循环,则不会处理任何事件。

将你的最后一行更改main

return a.exec();

<< std::endl还可以通过添加或刷新来更改测试槽,std::cout否则您可能会在控制台上看不到任何输出。

然后您的程序应该按预期工作(除非它永远不会完成,因为没有什么会导致事件循环停止 - 只是中断它)。

于 2013-07-28T16:26:23.310 回答