如何检测我的应用程序是否已被暂停(当有人更改应用程序时)并通过停用我的计时器做出反应,然后在我的应用程序变为未暂停时重新激活它(当有人重新打开我正在运行的应用程序时)。下面到目前为止我在这个部分编写了多远我的应用程序,但它给出了一个错误:“'QApplication :: QApplication(const QApplication&)'是私有的”,它在myapp.cpp第4行的上下文中说。如果任何人都可以告诉我,我做错了什么,非常感谢。
这是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 :(得分:4)
添加事件过滤器以检查激活和停用事件。来自QEvent documentation:
QEvent :: ApplicationActivate 121该应用程序已提供给用户。 QEvent :: ApplicationDeactivate 122应用程序已被暂停,用户无法使用。
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;
}
答案 1 :(得分:1)
QEvent::AppliciationActivate
和QEvent::ApplicationDeactivate
已被弃用(请参阅here)。相反,请使用QEvent::ApplicationStateChange
。没有必要使用事件过滤器来利用它;只需连接到QGuiApplication::applicationStateChanged(Qt::ApplicationState)
并根据需要进行处理。