I made a program that will shutdown PC after a set time. User enters time using 2 spin boxes (one for hours and the other for minutes), then i convert it to miliseconds:
ms = (ui->hBox->text().toInt() * 3600 + ui->mBox->text().toInt() * 60) * 1000;
then it calls timer() function
void Dialog::timer()
{
QTimer *time = new QTimer(this);
time->setInterval(ms);
time->setSingleShot(1);
connect(time, SIGNAL(timeout()), this, SLOT(shutdown()));
time->start();
}
then signal timeout() calls shutdown() slot/function that check whether user want to shutdown, restart or log off PC and then executes appropriate command.
void Dialog::shutdown()
{
if(ui->radioButton->isChecked())
system("shutdown -s -t 0");
else if(ui->radioButton_2->isChecked())
system("shutdown -r -t 0");
else if(ui->radioButton_3->isChecked())
system("shutdown -l -t 0");
}
(also tried "shutdown -s -f", and even only "shutdown -s", nothing seems to help resolve problem)
I want to run this application only on my Windows PC, so using system() is not problem either.
The above application executes perfectly when i run it from Qt Creator, but when I deploy it, put all needed .dll files, the program wait for timer and then just opens empty console and then launch new instance of the program, PC never turn off or restart. So what am I doing wrong? Am I missing some .dll files or something?
A simple fix is to rename your deployed exe to something like: shut.exe or anything instead of shutdown.exe, then the system will execute it's shutdown command instead of executing your application again.
LE: there might be other ways to solve this, but i haven't investigated this... so stay close for other solutions too ;)