سؤال

I'm need to do some stuf at specific time. In Android I use AlarmManager to do this but in qt I don't know how to do it. With in my experience with qt, QTimer::singleShot stops when application closed, but I need to make it run after my application closed. I will run application on background but i really don't want to see my application on open application screen.

Thanks for any help.

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

المحلول

You could make a class to handle this. The example below uses a "WindowManager" class, and a subclassed QMainWindow, but of course you could use any 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();
    }
};

"main.cpp"

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

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    WindowManager wm;
    return a.exec();
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top