Qt 应用程序挂起
如何检测我的应用程序是否已暂停(当有人更改应用程序时)并通过停用计时器做出反应,然后在我的应用程序取消暂停时(当有人重新打开我的半运行应用程序时)重新激活它。下面是我到目前为止在我的应用程序的这一部分上编码的程度,但它给出了一个错误:“'QApplication::QApplication(const QApplication&)' is private”,它在 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;
}
}
How do I detect if my application has been suspended (when someone changes applications) and react by deactivating my timer and then re-activating it when my application becomes un-suspended (when someone re-opens my half running app). Beneath Is how far I have coded so far on this part my app but it gives an error: "'QApplication::QApplication(const QApplication&)' is private" and it says within the context of myapp.cpp line 4. Please if anyone can tell me what I'm doing wrong it would be greatly appreciated.
Here is my code for the 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加事件过滤器以检查激活和停用事件。来自 QEvent 文档:
Add an event filter to check for the activate and deactivate events. From the QEvent documentation:
QEvent::AppliciationActivate
和QEvent::ApplicationDeactivate
已被弃用(请参阅此处)。相反,请使用QEvent::ApplicationStateChange
。无需使用事件过滤器即可利用此功能;只需连接到 QGuiApplication::applicationStateChanged(Qt::ApplicationState) 并根据需要进行处理。QEvent::AppliciationActivate
andQEvent::ApplicationDeactivate
have been deprecated (see here). Instead, useQEvent::ApplicationStateChange
. There is no need to use an event filter to utilise this; just connect toQGuiApplication::applicationStateChanged(Qt::ApplicationState)
and handle it as appropriate.