Skip to content

Instantly share code, notes, and snippets.

@xim
Last active October 4, 2016 08:19
Show Gist options
  • Save xim/c6b7cdce980e18039389d0421fef2431 to your computer and use it in GitHub Desktop.
Save xim/c6b7cdce980e18039389d0421fef2431 to your computer and use it in GitHub Desktop.
Minimal program to reproduce hanging qt apps
#ifndef ACTIVITY_NOTIFIER_H
#define ACTIVITY_NOTIFIER_H
void setIsActive(bool);
#endif
#include "activityNotifier.h"
#include <Foundation/NSProcessInfo.h>
static id activity = nullptr;
void setIsActive(bool active) {
if (active && !activity) {
activity = [[NSProcessInfo processInfo] beginActivityWithOptions: NSActivityUserInitiatedAllowingIdleSystemSleep reason:@"Disable napping"];
[activity retain];
} else if (!active && activity) {
[[NSProcessInfo processInfo] endActivity: activity];
[activity release];
activity = nullptr;
}
}
#include <QGuiApplication>
#include <QElapsedTimer>
#include <QTimer>
#include "activityNotifier.h"
int main(int argc, char *argv[])
{
static bool detectedNap = false;
static QElapsedTimer elapsedTimer;
QGuiApplication a(argc, argv);
QObject::connect(&a, &QGuiApplication::applicationStateChanged, [](Qt::ApplicationState state){
qDebug("At %llds: App state: %d", elapsedTimer.elapsed() / 1000, state);
detectedNap = false;
});
QTimer spamTimer;
spamTimer.setInterval(1);
elapsedTimer.start();
static auto last = elapsedTimer.elapsed();
QObject::connect(&spamTimer, &QTimer::timeout, &a, []{
auto n = elapsedTimer.elapsed();
if ((n - last) > 5000 && !detectedNap) {
qInfo("At %llds: Napped", last / 1000);
detectedNap = true;
}
last = n;
});
static bool disablerActive = false;
static size_t interval = 45000;
static QTimer napToggler;
napToggler.setInterval(interval);
QObject::connect(&napToggler, &QTimer::timeout, &a, []{
setIsActive(disablerActive = !disablerActive);
qDebug("At %llds: Nap disabler state: %d", elapsedTimer.elapsed() / 1000, disablerActive);
napToggler.start(disablerActive ? interval *= 2 : 45000);
detectedNap = false;
});
spamTimer.start();
napToggler.start();
return a.exec();
}
TARGET = timer-test
QT += widgets
CONFIG += c++11
TEMPLATE = app
LIBS += -framework Foundation
SOURCES += main.cpp
OBJECTIVE_SOURCES += activityNotifier.mm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment