سؤال

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.

هل كانت مفيدة؟

المحلول

Your program exits right after it's set the timer - it has no time to fire.

For the timer to work, you need an event loop running. Without the event loop, no events get processed.

Change the last line of your main to

return a.exec();

Also change your test slot by adding << std::endl or flush std::cout otherwise you might see no output on the console.

Your program should then work as expected (except it won't ever finish since nothing will cause the event loop to stop - just interrupt it).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top