Qt 应用程序挂起

发布于 2024-12-11 07:02:39 字数 3037 浏览 0 评论 0原文

如何检测我的应用程序是否已暂停(当有人更改应用程序时)并通过停用计时器做出反应,然后在我的应用程序取消暂停时(当有人重新打开我的半运行应用程序时)重新激活它。下面是我到目前为止在我的应用程序的这一部分上编码的程度,但它给出了一个错误:“'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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单身情人 2024-12-18 07:02:39

添加事件过滤器以检查激活和停用事件。来自 QEvent 文档

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;
}

Add an event filter to check for the activate and deactivate events. From the 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;
}
半透明的墙 2024-12-18 07:02:39

QEvent::AppliciationActivateQEvent::ApplicationDeactivate 已被弃用(请参阅此处)。相反,请使用QEvent::ApplicationStateChange。无需使用事件过滤器即可利用此功能;只需连接到 QGuiApplication::applicationStateChanged(Qt::ApplicationState) 并根据需要进行处理。

QEvent::AppliciationActivate and QEvent::ApplicationDeactivate have been deprecated (see here). Instead, use QEvent::ApplicationStateChange. There is no need to use an event filter to utilise this; just connect to QGuiApplication::applicationStateChanged(Qt::ApplicationState) and handle it as appropriate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文