功能问题QTimer :: singleShot

时间:2013-07-28 16:22:03

标签: qt qtimer

我正在尝试通过QAudioInput录制声音。根据本网站的文档QAudioInput。但是当我跑步时,它导出了一个空的原始文件。检查后,看起来函数QTimer :: singleShot不起作用(我在qWarning << "Done"中添加了语句void stopRecording()并且它没有显示“完成”所以我认为它在QTimer中有一些错误: :singleShot功能)。

这是我用来检查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;
}

此代码也不起作用。有人能解释一下吗?我是Qt-programming的新手。

1 个答案:

答案 0 :(得分:2)

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

要使计时器起作用,您需要运行一个事件循环。没有事件循环,就不会处理任何事件。

main的最后一行更改为

return a.exec();

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

您的程序应该按预期工作(除非它不会完成,因为没有任何东西会导致事件循环停止 - 只是中断它)。

相关问题