[SOLVED] QDialog in fullscreen - disable OS screensaver?
-
Finally trying to achieve this function after many user requested it.
I'm thinking of a easy cross-platform way to do it (Windows and Mac OS X)
What I'm thinking:
- start a QTimer that fire each 1 minute
- [Slot] emulate a key press on the keyboard or a small mouse movement (keyboard or mouse is not used in this part of the software so shouldn't matter)
I'll see if I can achieve it (not sure how to emulate key or mouse movement in Qt) and post result!
Thanks Qt!
-
I tried this one on my QDialog. But still not working (screensaver activates)
Maybe QDialog doesn't handle KeyPress event? investigating..@void WorkoutDialog::desactivateScreensaver() {
qDebug() << "OK DESACTIVATE SCREENSAVER!"; QKeyEvent *event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier); QCoreApplication::postEvent(this, event); QKeyEvent *event1 = new QKeyEvent(QEvent::KeyRelease, Qt::Key_Enter, Qt::NoModifier); QCoreApplication::postEvent(this, event1);
}@
-
I tried this one on my QDialog. But still not working (screensaver activates)
Maybe QDialog doesn't handle KeyPress event? investigating..@void WorkoutDialog::desactivateScreensaver() {
qDebug() << "OK DESACTIVATE SCREENSAVER!"; QKeyEvent *event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier); QCoreApplication::postEvent(this, event); QKeyEvent *event1 = new QKeyEvent(QEvent::KeyRelease, Qt::Key_Enter, Qt::NoModifier); QCoreApplication::postEvent(this, event1);
}@
-
Weird because I see my eventFilter being called, but the action never takes place on the UI (key pressed, mouseMove, ..)
@bool WorkoutDialog::eventFilter(QObject *watched, QEvent *event) {
Q_UNUSED(watched); // qDebug() << "EventFilter " << watched << "Event:" << event; /// Disable enter pressed on dialog (bug cause it to minimize) if(event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event); if(keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return ) return true; // mark the event as handled } if(event->type() == QEvent::WindowStateChange) { if(windowState().testFlag(Qt::WindowMaximized) == true) { qDebug() << "Going call showFullSceen here"; this->showFullScreenWin(); return true; } } return false;
}@
-
Weird because I see my eventFilter being called, but the action never takes place on the UI (key pressed, mouseMove, ..)
@bool WorkoutDialog::eventFilter(QObject *watched, QEvent *event) {
Q_UNUSED(watched); // qDebug() << "EventFilter " << watched << "Event:" << event; /// Disable enter pressed on dialog (bug cause it to minimize) if(event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event); if(keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return ) return true; // mark the event as handled } if(event->type() == QEvent::WindowStateChange) { if(windowState().testFlag(Qt::WindowMaximized) == true) { qDebug() << "Going call showFullSceen here"; this->showFullScreenWin(); return true; } } return false;
}@
-
[quote author="MarianMMX" date="1397293908"]Ok, tested on Windows XP:
DisableScreenSaver.c
@
#include <Windows.h>
#pragma comment(lib, "user32.lib")int main()
{
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE , NULL, SPIF_SENDWININICHANGE);
return 0;
}@
[/quote]Tested and works on windows 7 too. thanks
Update: not really what I wanted, it still disable the ScreenSaver after you leave the app.. don't want to mess with personal settings..I gave up trying to emulate a key press, will use this code instead.
Trying to find an equivalent for Mac OS X now -
[quote author="MarianMMX" date="1397293908"]Ok, tested on Windows XP:
DisableScreenSaver.c
@
#include <Windows.h>
#pragma comment(lib, "user32.lib")int main()
{
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE , NULL, SPIF_SENDWININICHANGE);
return 0;
}@
[/quote]Tested and works on windows 7 too. thanks
Update: not really what I wanted, it still disable the ScreenSaver after you leave the app.. don't want to mess with personal settings..I gave up trying to emulate a key press, will use this code instead.
Trying to find an equivalent for Mac OS X now -
I read you can call Objective-C from Qt
I want "to use this method":http://stackoverflow.com/questions/5596319/how-to-programmatically-prevent-a-mac-from-going-to-sleep/5596946#5596946
I made a header file named "MacUtil.h" and a "MacUtil.mm" that will call the Objective-C code. Will see if I can get it to work and post result
-
I read you can call Objective-C from Qt
I want "to use this method":http://stackoverflow.com/questions/5596319/how-to-programmatically-prevent-a-mac-from-going-to-sleep/5596946#5596946
I made a header file named "MacUtil.h" and a "MacUtil.mm" that will call the Objective-C code. Will see if I can get it to work and post result
-
On the way, will edit this post with solution
MacUtil.h@class MacUtil
{public:
MacUtil();
~MacUtil();void disableScreensaver(); void releaseScreensaverLock();
private:
IOPMAssertionID assertionID;};
#endif // MACUTIL_H@
MacUtil.mm
@//http://stackoverflow.com/questions/5596319/how-to-programmatically-prevent-a-mac-from-going-to-sleep/5596946#5596946
void MacUtil::disableScreensaver() {CFStringRef* reasonForActivity= CFSTR("MaximumTrainer FullScreen Workout"); IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep, kIOPMAssertionLevelOn, reasonForActivity, &assertionID); if (success == kIOReturnSuccess) { //return sucess? todo.. }
}
void MacUtil::releaseScreensaverLock() {
IOPMAssertionRelease(assertionID);
}
@ -
On the way, will edit this post with solution
MacUtil.h@class MacUtil
{public:
MacUtil();
~MacUtil();void disableScreensaver(); void releaseScreensaverLock();
private:
IOPMAssertionID assertionID;};
#endif // MACUTIL_H@
MacUtil.mm
@//http://stackoverflow.com/questions/5596319/how-to-programmatically-prevent-a-mac-from-going-to-sleep/5596946#5596946
void MacUtil::disableScreensaver() {CFStringRef* reasonForActivity= CFSTR("MaximumTrainer FullScreen Workout"); IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep, kIOPMAssertionLevelOn, reasonForActivity, &assertionID); if (success == kIOReturnSuccess) { //return sucess? todo.. }
}
void MacUtil::releaseScreensaverLock() {
IOPMAssertionRelease(assertionID);
}
@ -
Anyone good with Objective-C?
I'm getting theses error tryings to compile the new class (mm file) I just added:
duplicate symbol __ZN8MacUtilsC2Ev in:
MacUtils.o
macutils.o
duplicate symbol __ZN8MacUtilsC1Ev in:
MacUtils.o
macutils.oSeems like a conflict with
@#import <IOKit/pwr_mgt/IOPMLib.h> in MacUtil.mm@
and my .pro has already theses:
@ LIBS += -framework IOKit
LIBS += -framework CoreFoundation@ -
Anyone good with Objective-C?
I'm getting theses error tryings to compile the new class (mm file) I just added:
duplicate symbol __ZN8MacUtilsC2Ev in:
MacUtils.o
macutils.o
duplicate symbol __ZN8MacUtilsC1Ev in:
MacUtils.o
macutils.oSeems like a conflict with
@#import <IOKit/pwr_mgt/IOPMLib.h> in MacUtil.mm@
and my .pro has already theses:
@ LIBS += -framework IOKit
LIBS += -framework CoreFoundation@ -
Working for both Windows and OS X!
On dialog open (constructor):
@// Disable ScreenSaver
#ifdef Q_OS_MAC
macUtil.disableScreensaver();
#else
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE , NULL, SPIF_SENDWININICHANGE);
#endif@On dialog close (destructor):
@#ifdef Q_OS_MAC
macUtil.releaseScreensaverLock();
#else
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, TRUE , NULL, SPIF_SENDWININICHANGE);
#endif@Tested and work for Windows, if you have no screensaver, it will not put one with the the destructor, all possible cases seems to work
-
Working for both Windows and OS X!
On dialog open (constructor):
@// Disable ScreenSaver
#ifdef Q_OS_MAC
macUtil.disableScreensaver();
#else
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE , NULL, SPIF_SENDWININICHANGE);
#endif@On dialog close (destructor):
@#ifdef Q_OS_MAC
macUtil.releaseScreensaverLock();
#else
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, TRUE , NULL, SPIF_SENDWININICHANGE);
#endif@Tested and work for Windows, if you have no screensaver, it will not put one with the the destructor, all possible cases seems to work
-
You should rather use a elif defined(Q_OS_WIN) since it's completely platform specific
-
You should rather use a elif defined(Q_OS_WIN) since it's completely platform specific
-
@maximus said:
Working for both Windows and OS X!
On dialog open (constructor):
@// Disable ScreenSaver
#ifdef Q_OS_MAC
macUtil.disableScreensaver();
#else
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE , NULL, SPIF_SENDWININICHANGE);
#endif@On dialog close (destructor):
@#ifdef Q_OS_MAC
macUtil.releaseScreensaverLock();
#else
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, TRUE , NULL, SPIF_SENDWININICHANGE);
#endif@Tested and work for Windows, if you have no screensaver, it will not put one with the the destructor, all possible cases seems to work
Can you share your source or at least the relevant part? I've been using the same code for my app to disable/re-enable on Windows, but now need to do the same for Mac clients. Thanks.