2011-10-24 176 views
1

我如何检测是否我的应用程序已经暂停(当有人改变了应用程序),并通过停用我的定时器反应,然后再重新激活它,当我的应用程序变得无法暂停(当有人再次打开我的一半运行应用程序)。下面是我到目前为止编码到目前为止我的应用程序到目前为止,但它给出了一个错误:“'QApplication :: QApplication(const QApplication &)'是私人的”,它说在myapp.cpp第4行的上下文中。请if任何人都可以告诉我我做错了什么,将不胜感激。Qt应用程序暂停

这里是我的main.cpp中的代码:

#include <QtGui/QApplication> 
#include "qmlapplicationviewer.h" 
#include <QObject> 
#include <QGraphicsObject> 
#include <QTimer> 
#include <QVariant> 
#include "timecontrol.h" 
#include "scorecontrol.h" 
#include "Retry.h" 
#include <QEvent> 

int main(int argc, char *argv[]) 
{ 
QApplication app(argc, argv); 

QmlApplicationViewer viewer; 
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationLockLandscape); 
viewer.setMainQmlFile(QLatin1String("qml/Raker/main.qml")); 
viewer.showExpanded(); 

QObject *rootObject = viewer.rootObject(); 

QTimer *timmer = new QTimer; 
timmer->setInterval(1000); 

TimeControl *timcon = new TimeControl; 

scorecontrol *scorer = new scorecontrol; 

Retry *probeer = new Retry; 

QObject::connect(timmer, SIGNAL(timeout()), timcon, SLOT(updateTime())); 
QObject::connect(timcon, SIGNAL(setTime(QVariant)), rootObject, SLOT(setTime(QVariant))); 
QObject::connect(rootObject, SIGNAL(blockClicked(int, int)), scorer, SLOT(checkRight(int, int))); 
QObject::connect(scorer, SIGNAL(setScore(QVariant)), rootObject, SLOT(setScore(QVariant))); 
QObject::connect(scorer, SIGNAL(setState(QVariant)), rootObject, SLOT(setState(QVariant))); 

QObject::connect(rootObject, SIGNAL(start()), probeer, SLOT(Reetry())); 
QObject::connect(probeer, SIGNAL(start()), timmer, SLOT(start())); 
QObject::connect(probeer, SIGNAL(start(int)), scorer, SLOT(randomNum(int))); 
QObject::connect(probeer, SIGNAL(sReset()), timcon, SLOT(reset())); 
QObject::connect(probeer, SIGNAL(tReset()), scorer, SLOT(reset())); 
QObject::connect(timcon, SIGNAL(timeOut()), scorer, SLOT(reset())); 

QObject::connect(timcon, SIGNAL(setState(QVariant)), rootObject, SLOT(setState(QVariant))); 
QObject::connect(timcon, SIGNAL(changeFinal()), scorer, SLOT(changeFinal())); 
QObject::connect(scorer, SIGNAL(setFinal(QVariant)), rootObject, SLOT(setFinal(QVariant))); 

return app.exec(); 
} 

myApp.h:

#ifndef MYAPP_H 
    #define MYAPP_H 

    #include <QApplication> 
    #include <QObject> 

    class MyApp : public QApplication 
    { 
    public: 
     MyApp(QApplication &app); 

    protected: 
     bool eventFilter(QObject *obj, QEvent *event); 
    }; 

    #endif // MYAPP_H 

MyApp.cpp中:

#include "myapp.h" 
    #include <QEvent> 

    MyApp::MyApp(QApplication &app) : QApplication(app) 
    { 
     installEventFilter(this); 
    } 

    bool MyApp::eventFilter(QObject *obj, QEvent *event) 
    { 
     if (event->type() == QEvent::ApplicationDeactivate) 
     { 

     } 

     if (event->type() == QEvent::ApplicationDeactivate) 
     { 

     } 

     else 
     { 
      return false; 
     } 
    } 
+0

你问如何实现在QT一[看门狗定时器(http://en.wikipedia.org/wiki/Watchdog_timer)? – 2011-10-24 04:31:10

回答

4

添加事件过滤器来检查激活和停用事件。从QEvent documentation

QEvent::ApplicationActivate 121 The application has been made available to the user. QEvent::ApplicationDeactivate 122 The application has been suspended, and is unavailable to the user.

MainWindow::MainWindow(QWidget *parent) : 
QMainWindow(parent),ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
    ui->comboBox->installEventFilter(this); 
    . 
    . 
    . 
} 

bool MainWindow::eventFilter(QObject *object, QEvent *event) 
{ 
    if (event->type() == QEvent::ApplicationDeactivate) 
    { 
     // Deactivate timer 
    } 
    if (event->type() == QEvent::ApplicationActivate) 
    { 
     // Turn timer back on 
    } 
    return false; 
} 
+0

我将如何使用QML与C++应用程序一起使用这个功能? – Gerharddc

+0

在QApplication对象而不是MainWindow上安装事件过滤器。 – 2011-10-24 15:50:41

+0

我将如何更改此代码以使用该代码: – Gerharddc

1

QEvent::AppliciationActivateQEvent::ApplicationDeactivate已被否决(见here)。相反,使用QEvent::ApplicationStateChange。没有必要使用事件过滤器来利用它;只需连接到QGuiApplication::applicationStateChanged(Qt::ApplicationState)并根据需要进行处理。