2

我需要在特定时间做一些事情。在 Android 中,我使用 AlarmManager 来执行此操作,但在 qt 中我不知道该怎么做。根据我使用 qt 的经验,QTimer::singleShot 在应用程序关闭时停止,但我需要在我的应用程序关闭后让它运行。我将在后台运行应用程序,但我真的不想在打开的应用程序屏幕上看到我的应用程序。

谢谢你的帮助。

4

1 回答 1

0

你可以创建一个类来处理这个问题。下面的示例使用“WindowManager”类和子类 QMainWindow,但当然您可以使用任何 QWidget。

“wm.h”

#include <QtGui>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow() {}

signals:
    void startTimer(int);

protected:
    void closeEvent(QCloseEvent *event)
    {
        event->setAccepted(false);
        startTimer(5000);
        hide();
    }
};

class WindowManager : public QObject
{
public:
    WindowManager()
    {
        MainWindow *w = new MainWindow;
        QTimer *timer = new QTimer(this);

        connect(w, SIGNAL(startTimer(int)), timer, SLOT(start(int)));
        connect(timer, SIGNAL(timeout()), w, SLOT(show()));

        w->show();
    }
};

“主.cpp”

#include <QtCore/QCoreApplication>
#include "wm.h";

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    WindowManager wm;
    return a.exec();
}
于 2012-04-21T17:19:05.353 回答