QGuiApplication stops the event-loop when phone is locked when compiled with Qt 5.3 or Qt 5.4 (but not with Qt 5.2)
Asked Answered
P

1

7

I have created a simple program that reproduces the problem. When I lock the phone, or if I switch to another application in my android phone, the worker thread continues printing, but the event loop stops. When I switch back to my application, the event-loop resumes.

If I replace QGuiApplication with QCoreApplication, the problem disappears. If I compile with Qt 5.2 instead of Qt 5.3, the problem disappears. Qt 5.4 has the same problem as Qt 5.3.

static int count = 0;

void workerThread()
{
    while (1) {
        qDebug("Worker thread %d", count++);
        sleep(1);
    }
}

void MyObject::step()
{
    qDebug("Event loop %d", count++);
}

int main(int argc, char *argv[])
{
    QGuiApplication a(argc, argv);
    MyObject w;
    QTimer timer;
    QObject::connect(&timer, SIGNAL(timeout()), &w, SLOT(step()));
    timer.start(1000);
    QtConcurrent::run(workerThread);
    return a.exec();
}

How do I prevent QGuiApplication from stopping the event loop when the app loses focus? I need my app to process events even when not in the foreground.

Passifloraceous answered 13/12, 2014 at 21:24 Comment(4)
Can you show the output of logcat?Uela
sashoalm, I created a bugreport for you, but please provide more details about the OS and phone for the developers: bugreports.qt-project.org/browse/QTBUG-43434Uela
I would probably also verify that application state (changed signal, etc).Uela
@lpapp Here's the logcat output - pastebin.com/1WGPYFue.Passifloraceous
G
4

As mentioned in my comments, I created a bugreport for you with the risk of being rejected, but at least we would get feedback from the official maintainers.

QGuiApplication "stops" when locking the phone or switching application.

It has now been withdrawn, which is fair enough, because as the maintainer claims, you should focus on creating a service rather than activity. There is no dedicated Qt API for that just yet, however.

The reason why it is not good idea to this in an activity is simply that the application may be killed, even instantly, when it gets into the "background. Also, it may drain battery earlier than the user may expect it to. In short, this is considered as a bug rather than a feature in 5.2 by the maintainer(s) which seems to have been fixed.

Here you can find some help how to create a service as of today:

Build background service with Qt on android

Godhead answered 18/12, 2014 at 8:55 Comment(3)
I understand what you mean. I was going for an easy answer, just to get a hat. :D +1 to you.Talbott
@karlphillip: my hats off? ;-) If you mean winter hats, all of mine is yours. I disabled that feature.Uela
As my "mysteriously vanishing" comment already stated, it is not a bug but a new feature. You are trying too hard...Drayage

© 2022 - 2024 — McMap. All rights reserved.