Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

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.

like image 293
sashoalm Avatar asked Dec 13 '14 21:12

sashoalm


1 Answers

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

like image 187
lpapp Avatar answered Nov 14 '22 23:11

lpapp