Use QT to implement windows Timed Shutdown

Source: Internet
Author: User
Tags emit

Currently, there are a lot of gadgets to shut down online at regular times, but according to my personal experience, many online gadgets are toxic or contain malicious code or rogue code. Because we often download things and need to use Timed Shutdown, we simply wrote a software for Timed Shutdown, which is easy to use.

Based on the win7 system, by the way, the small software under win7 is very beautiful.

First, the software needs to minimize the running of the tray and display the countdown floating window. It is displayed in the lower right corner. The Code is as follows:

Suswidget. h

#ifndef SUSWIDGET_H#define SUSWIDGET_H#include <QtGui/QWidget>#include <QDateTime>#include <QtGui/QLineEdit>#include <QtCore/QTextCodec>#include <QtGui/QApplication>#include <QDesktopWidget>#ifndef    REMAIN_TIME#define   REMAIN_TIME            0#endif#ifndef    TO_SHUTDOWN#define   TO_SHUTDOWN         1#endif#ifndef    SHUTDOWN_ERROR#define   SHUTDOWN_ERROR  2#endifclass SusWidget : public QWidget{Q_OBJECTpublic:SusWidget(QWidget *parent = 0, Qt::WFlags flags = 0);~SusWidget();private:QLineEdit  *timeEdit;public slots:void SetRemainTime(const QTime &time, int type);};#endif // SUSWIDGET_H

 

Suswidget. cpp

# Include "suswidget. H "suswidget: suswidget (qwidget * parent, QT: wflags flags): qwidget (parent, flags) {setfixedsize (170, 20); qtextcodec: setcodecfortr (qtextcodec :: codecforname ("gb18030"); setwindowflags (QT: mimizewindowhint | QT: windowstaysontophint | QT: tool); qrect screensize; screensize = qapplication: desktop () -> rect (); setgeometry (screensize. width ()-180, screensize. height ()-70,170, 20); timee Dit = new qlineedit (this); timeedit-> setgeometry (rect (); qpalette palette; qbrush brush (qcolor (255,255, 0,255); brush. setstyle (QT: solidpattern); qbrush brush1 (qcolor (85, 0, 0,255); brush1.setstyle (QT: solidpattern); palette. setbrush (qpalette: active, qpalette: Text, brush); palette. setbrush (qpalette: active, qpalette: Base, brush1); palette. setbrush (qpalette: inactive, qpalette: Text, brush); palette. Setbrush (qpalette: inactive, qpalette: Base, brush1); palette. setbrush (qpalette: Disabled, qpalette: Text, brush); palette. setbrush (qpalette: Disabled, qpalette: Base, brush1); timeedit-> setpalette (palette); qfont font; font. setfamily (qstring: fromutf8 ("\ 351 \ 273 \ 221 \ 344 \ 275 \ 223"); font. setbold (true); font. setweight (75); timeedit-> setfont (font); timeedit-> setalignment (QT: aligncenter); timeedit-> settext (tr ("Statistical shutdown remaining time..."); timeedit-> setreadonly (true);} suswidget ::~ Suswidget () {} void suswidget: setremaintime (const qtime & time, int type) {If (type = remain_time) {timeedit-> settext (TR ("left off shutdown and there is: ") + time. tostring (TR ("HH mm min SS seconds");} If (type = to_shutdown) {timeedit-> settext (TR ("shutting down... ");} If (type = shutdown_error) {timeedit-> settext (TR (" shutdown failed... "));}}

Timed Shutdown requires a background thread to implement the timing function:

Timethread. h

#ifndef TIMETHREAD_H#define TIMETHREAD_H#include <QDateTime>#include <QThread>#include <Windows.h>#define   REMAIN_TIME            0#define   TO_SHUTDOWN         1#define   SHUTDOWN_ERROR  2class TimeThread : public QThread{Q_OBJECTpublic:QTime m_time;bool m_start;public:TimeThread(QObject *parent = 0);void SetCountTime(QTime time, bool start);bool MySystemShutdown();signals:void RemainTimeChanged(const QTime &time, int type);protected:void run();};#endif

 

Timethread. cpp

# Include "timethread. H "timethread: timethread (qobject * parent): qthread (parent) {m_time = qtime (1, 0, 0); m_start = false;} void timethread :: setcounttime (qtime time, bool start) {m_time = time; m_start = start;} void timethread: Run () {qtime oldtime, newtime; oldtime. sethms (qtime: currenttime (). hour (), qtime: currenttime (). minute (), qtime: currenttime (). second (), 0); While (m_start) {msleep (250); newtime. seth MS (qtime: currenttime (). hour (), qtime: currenttime (). minute (), qtime: currenttime (). second (), 0); If (oldtime! = Newtime) {oldtime = newtime; m_time = m_time.addsecs (-1);} emit remaintimechanged (m_time, remain_time); If (m_time = qtime (0, 0, 0 )) {emit remaintimechanged (m_time, to_shutdown); If (! Mysystemshutdown () {emit remaintimechanged (m_time, shutdown_error);} break ;}} bool timethread: mysystemshutdown () {handle htoken; token_privileges tkp; // obtain the process flag if (! Openprocesstoken (getcurrentprocess (), token_adjust_privileges | token_query, & htoken) return false; // obtain the luidlookupprivilegevalue (null, se_shutdown_name, & tkp. privileges [0]. luid); tkp. privilegecount = 1; tkp. privileges [0]. attributes = se_privilege_enabled; // obtain the shutdown privilege adjusttokenprivileges (htoken, false, & tkp, 0, (ptoken_privileges) null, 0); If (getlasterror ()! = Error_success) return false; // force disable the computer if (! Exitwindowsex (ewx_shutdown | ewx_force, shtdn_reason_major_operatingsystem | shtdn_reason_minor_upgrade | shtdn_reason_flag_planned) return false; return true ;}

 

Generally, the current system time must be displayed when the software is shut down at a specified time:

Systemtimethread. h

#ifndef SYSTEMTIMETHREAD_H#define SYSTEMTIMETHREAD_H#include <QDateTime>#include <QThread>class SystemTimeThread : public QThread{Q_OBJECTpublic:QTime m_time;public:SystemTimeThread(QObject *parent = 0);signals:void CurrentTimeChanged(const QTime &time);protected:void run();};#endif

 

Systemtimethread. cpp

#include "SystemTimeThread.h"SystemTimeThread::SystemTimeThread(QObject *parent) :  QThread(parent){m_time = m_time.currentTime();}void SystemTimeThread::run(){while(1){msleep(500);m_time = m_time.currentTime();emit CurrentTimeChanged(m_time);}}

All right, the main parts above have been written, and the next step is the main interface. Shutdown is only a small software, so it is as easy as possible. Do not add too many unnecessary functions. The Code is as follows:

Shutdown. h

#ifndef SHUTDOWN_H#define SHUTDOWN_H#include <QtCore/QVariant>#include <QtGui/QAction>#include <QtGui/QApplication>#include <QtGui/QButtonGroup>#include <QtGui/QDialog>#include <QtGui/QGroupBox>#include <QtGui/QLabel>#include <QtGui/QLineEdit>#include <QtGui/QPushButton>#include <QtGui/QRadioButton>#include <QtGui/QTimeEdit>#include <QRegExpValidator>#include <QString>#include <QDateTime>#include <QSystemTrayIcon>#include "TimeThread.h"#include "SusWidget.h"#include "SystemTimeThread.h"class ShutDown : public QDialog{Q_OBJECTpublic:ShutDown(QWidget *parent = 0, Qt::WFlags flags = 0);~ShutDown();private:QGroupBox *chooseGroup;QRadioButton *countDownRadio;QRadioButton *timerRadio;QTimeEdit *countEdit;QTimeEdit *timeEdit;QLabel *remainText;QLineEdit *remainTimeLine;QLineEdit *systemTimeLine;QLabel *currentTimeText;QPushButton *startButton;QPushButton *exitButton;QAction *susAction;QAction *restoreAction;QAction *quitAction;QSystemTrayIcon *trayIcon;QMenu *trayIconMenu;QTime    m_time;bool       m_start;bool       m_countDown;bool       m_mini;bool       m_suspend;TimeThread  m_timeThread;SystemTimeThread  m_systemTimeThread;SusWidget *swidget;//protected://void closeEvent(QCloseEvent *event);public:void createActions();void createTrayIcon();void SetSusTime(const QTime &time, int type);signals:void RemainTimeChanged(const QTime &time, int type);private slots:void  CountTimeChanged(const QTime &time);void  ClickedCountDownRadio();void  doTimeChanged(const QTime &time);void  ClickedTimerRadio();void  ClickedStartButton();void  SetRemainTime(const QTime &time, int type);void  SetCurrentTime(const QTime &time);void  ShowDialog();void  ClikedTrayIcon(QSystemTrayIcon::ActivationReason reason);void  ShowSusWidget();void  HideOrExit();};#endif // SHUTDOWN_H

 

Shutdown. cpp

# Include "shutdown. H "# include <qtgui> shutdown: Shutdown (qwidget * parent, QT: wflags flags): qdialog (parent, flags) {If (objectname (). isempty () setobjectname (qstring: fromutf8 ("shutdown"); setfixedsize (264,261); setwindowflags (QT: windowminimizebuttonhint); qicon; icon. addpixmap (qpixmap (qstring: fromutf8 ("Resources/shutdown.png"), qicon: Normal, qicon: Off); setwindowicon (icon); createactions (); CR Eatetrayicon (); choosegroup = new qgroupbox (this); choosegroup-> setgeometry (qrect (20, 10,221,191); choosegroup-> setalignment (QT: aligncenter ); countdownradio = new week (choosegroup); countdownradio-> setgeometry (qrect (20, 30,101, 21); timerradio = new week (choosegroup); timerradio-> setgeometry (qrect (20, 70,101, 21); countedit = new qtimeedit (choosegroup); countedit-> setgeometry (qrect (120, 30, 81, 22); countedit-> setalignment (QT: alignright | QT: aligntrailing | QT: alignvcenter); countedit-> settime (qtime (1, 0, 0); timeedit = new qtimeedit (choosegroup); timeedit-> setgeometry (qrect (120, 70, 81, 22); timeedit-> setalignment (QT :: alignright | QT: aligntrailing | QT: alignvcenter); remaintext = new qlabel (choosegroup); remaintext-> setgeometry (qrect (20,110, 81, 21); remaintimeline = new qlineedi T (choosegroup); remaintimeline-> setgeometry (qrect (110,110, 91, 20); remaintimeline-> setalignment (QT: aligncenter); remaintimeline-> setreadonly (true ); systemtimeline = new qlineedit (choosegroup); systemtimeline-> setgeometry (qrect (110,150, 91, 20); systemtimeline-> setalignment (QT: aligncenter ); systemtimeline-> setreadonly (true); currenttimetext = new qlabel (choosegroup); currenttimetext-> setgeometry (qrec T (20,150, 81, 21); startbutton = new qpushbutton (this); startbutton-> setgeometry (qrect (30,220,101, 23); exitbutton = new qpushbutton (this ); exitbutton-> setgeometry (qrect (140,220, 91, 23); countdownradio-> setchecked (true); timerradio-> setchecked (false); m_time = countedit-> time (); remaintimeline-> settext (m_time.tostring (TR ("HH mm min SS seconds"); m_start = false; m_countdown = true; m_mini = false; m_suspend = True; Connect (& m_timethread, signal (remaintimechanged (const qtime &, INT), this, slot (setremaintime (const qtime &, INT); Connect (& m_systemtimethread, signal (currenttimechanged (const qtime &), this, slot (setcurrenttime (const qtime &); m_systemtimethread.start (); swidget = new suswidget (0); Connect (exitbutton, signal (clicked (), this, slot (hideorexit (); // reject () connect (countedit, signal (timecha Nged (const qtime &), this, slot (counttimechanged (const qtime &); Connect (countdownradio, signal (clicked (), this, slot (clickedcountdownradio (); Connect (timeedit, signal (timechanged (const qtime &), this, slot (dotimechanged (const qtime &); Connect (timerradio, signal (clicked (), this, slot (clickedtimerradio (); Connect (startbutton, signal (clicked (), this, slot (clickedstartbutton ())); connect (thi S, signal (remaintimechanged (const qtime &, INT), swidget, slot (setremaintime (const qtime &, INT); setwindowtitle (qapplication: translate ("shutdown ", "\ 351 \ 243 \ 230 \ 344 \ 272 \ 221 \ 345 \ 256 \ 232 \ 346 \ 227 \ 266 \ 345 \ 205 \ 263 \ 346 \ 234 \ 272 \ 347 \ 250 \ 213 \ 345 \ 272 \ 217 ", 0, qapplication: unicodeutf8); choosegroup-> settitle (qapplication: translate ("shutdown ", "\ 350 \ 256 \ 276 \ 345 \ 256 \ 232 \ 345 \ 205 \ 263 \ 346 \ 234 \ 272 \ 346 \ 226 \ 27 1 \ 345 \ 274 \ 217 ", 0, qapplication: unicodeutf8); countdownradio-> settext (qapplication: translate (" shutdown ", "\ 350 \ 256 \ 276 \ 345 \ 256 \ 232 \ 345 \ 200 \ 222 \ 350 \ 256 \ 241 \ 346 \ 227 \ 266 \ 357 \ 274 ", 0, qapplication: unicodeutf8); timerradio-> settext (qapplication: translate ("shutdown ", "\ 350 \ 256 \ 276 \ 345 \ 256 \ 232 \ 345 \ 205 \ 263 \ 346 \ 234 \ 272 \ 346 \ 227 \ 266 \ 351 \ 227 \ 264 \ 357 \ 274 \ 232 ", 0, qapplication: unicodeutf8); remaintext-> Settext (qapplication: translate ("shutdown ", "\ 345 \ 205 \ 263 \ 346 \ 234 \ 272 \ 345 \ 211 \ 251 \ 344 \ 275 \ 231 \ 346 \ 227 \ 266 \ 351 \ 227 \ 264 \ 357 \ 274 \ 232 ", 0, qapplication: unicodeutf8); currenttimetext-> settext (qapplication: translate ("dialog ", "\ 345 \ 275 \ 223 \ 345 \ 211 \ 215 \ 347 \ 263 \ 273 \ 347 \ 273 \ 237 \ 346 \ 227 \ 266 \ 351 \ 227 \ 264 \ 357 \ 274 \ 232 ", 0, qapplication: unicodeutf8); startbutton-> settext (qapplication: translate ("shutdown "," \ 345 \ 274 \ 200 \ 345 \ 247 \ 213 \ 345 \ 256 \ 232 \ 346 \ 227 \ 266 \ 345 \ 205 \ 263 \ 346 \ 234 ", 0, qapplication: unicodeutf8); exitbutton-> settext (qapplication: translate ("shutdown ", "\ 351 \ 232 \ 220 \ 350 \ 227 \ 217 \ 346 \ 210 \ 226 \ 351 \ 200 \ 200 \ 345 \ 207 \ 272", 0, qapplication :: unicodeutf8);} shutdown ::~ Shutdown () {m_systemtimethread.terminate (); m_systemtimethread.wait ();}/* void shutdown: closeevent (qcloseevent * event) {int choose = qmessagebox: Question (this, TR ("minimize to tray or exit program"), TR ("float cloud warmth prompt: \ n whether to minimize program to tray? To minimize the number, click yes. To exit the program, click NO. \ N to exit the program after minimization, right-click the tray icon and choose exit! "), Qmessagebox: Yes | qmessagebox: No, qmessagebox: No); If (choose = qmessagebox: Yes) {hide (); event-> ignore (); trayicon-> show (); m_mini = true; If (m_start) trayicon-> settooltip (TR ("Timed Shutdown program running ")); else trayicon-> settooltip (TR ("the program has stopped running at a specified time"); m_suspend = true; If (m_start) swidget-> show ();} else {reject () ;}} */void shutdown: hideorexit () {int choose = qmessagebox: Question (this, TR ("minimized to tray or exit program "), tr Show: \ n whether to minimize the program to the tray? To minimize the number, click yes. To exit the program, click NO. \ N to exit the program after minimization, right-click the tray icon and choose exit! "), Qmessagebox: Yes | qmessagebox: No, qmessagebox: No); If (choose = qmessagebox: Yes) {hide (); trayicon-> show (); m_mini = true; If (m_start) trayicon-> settooltip (TR ("Timed Shutdown program running ")); else trayicon-> settooltip (TR ("the program has stopped running at a specified time"); m_suspend = true; If (m_start) swidget-> show ();} else {reject () ;}} void shutdown: createactions () {susaction = new qaction (TR ("hidden floating window"), this); Connect (susaction, signal (triggered ()), This, slot (showsuswidget (); restoreaction = new qaction (TR ("Restore window"), this); Connect (restoreaction, signal (triggered (), this, slot (showdialog (); quitaction = new qaction (TR ("Exit program"), this); Connect (quitaction, signal (triggered (), qapp, slot (quit (); qicon icon; icon. addpixmap (qpixmap (qstring: fromutf8 ("Resources/sus.png"), qicon: Normal, qicon: Off); susaction-> seticon (icon); qicon icon1; icon1.addpi Xmap (qpixmap (qstring: fromutf8 ("Resources/restore.png"), qicon: Normal, qicon: Off); restoreaction-> seticon (icon1); qicon icon2; icon2.addpixmap (qpixmap (qstring: fromutf8 ("Resources/exit.png"), qicon: Normal, qicon: Off); quitaction-> seticon (icon2);} void shutdown:: createtrayicon () {trayiconmenu = new qmenu (this); trayiconmenu-> addaction (susaction); trayiconmenu-> addaction (restoreaction); trayiconmenu-> Ddseparator (); trayiconmenu-> addaction (quitaction); trayicon = new qsystemtrayicon (this); trayicon-> setcontextmenu (trayiconmenu); qicon icon; icon. addpixmap (qpixmap (qstring: fromutf8 ("Resources/shutdown.png"), qicon: Normal, qicon: Off); trayicon-> seticon (icon); Connect (trayicon, signal (activated (qsystemtrayicon: activationreason), this, slot (clikedtrayicon (qsystemtrayicon: activationreason); susacti On-> setenabled (false);} void shutdown: showdialog () {shownormal (); m_mini = false; trayicon-> hide (); swidget-> hide ();} void shutdown: showsuswidget () {If (m_suspend) {swidget-> hide (); susaction-> settext (TR ("display floating window"); m_suspend = false ;} else {swidget-> show (); susaction-> settext (TR ("hidden floating window"); m_suspend = true ;}} void shutdown: clikedtrayicon (qsystemtrayicon :: activationreason reason) {If (reason = qsystemtrayic On: trigger) showdialog ();} void shutdown: counttimechanged (const qtime & time) {countdownradio-> setchecked (true); timerradio-> setchecked (false ); m_time = countedit-> time (); remaintimeline-> settext (m_time.tostring (TR ("HH mm min SS seconds); m_countdown = true;} void shutdown :: clickedcountdownradio () {qtime time = countedit-> time (); counttimechanged (time);} void shutdown: dotimechanged (const qtime & time) {countdown Radio-> setchecked (false); timerradio-> setchecked (true); qtime systemtime; systemtime = systemtime. currenttime (); qtime settime = timeedit-> time (); m_time = settime. addsecs (-systemtime. hour () * 60 * 60-systemtime.minute () * 60-systemtime.second (); remaintimeline-> settext (m_time.tostring (TR ("HH mm min SS sec "))); m_countdown = false;} void shutdown: clickedtimerradio () {qtime time; time = timeedit-> time (); dotimechanged (Time) ;}void shutdown: clickedstartbutton () {If (m_countdown) {m_time = countedit-> time (); remaintimeline-> settext (m_time.tostring (TR ("HH mm min SS seconds "))); remaintimeline-> settext (m_time.tostring (TR ("HH mm min SS seconds");} else {qtime systemtime; systemtime = systemtime. currenttime (); qtime settime = timeedit-> time (); m_time = settime. addsecs (-systemtime. hour () * 60 * 60-systemtime.minute () * 60-systemtime.second (); Re Maintimeline-> settext (m_time.tostring (TR ("HH mm min SS seconds");} If (m_start) {// stop timing countdownradio-> setenabled (true ); timerradio-> setenabled (true); timeedit-> setenabled (true); countedit-> setenabled (true); susaction-> setenabled (false ); startbutton-> settext (TR ("Start Time shutdown"); m_start = false; m_timethread.setcounttime (m_time, m_start); m_timethread.terminate (); m_timethread.wait ();} else {// start timing countdownradio-> setena Bled (false); timerradio-> setenabled (false); timeedit-> setenabled (false); countedit-> setenabled (false); susaction-> setenabled (true ); startbutton-> settext (TR ("cancel Timed Shutdown"); m_start = true; m_timethread.setcounttime (m_time, m_start); m_timethread.start () ;}} void shutdown :: setsustime (const qtime & time, int type) {emit remaintimechanged (time, type); If (type = shutdown_error) {countdownradio-> setenabled (true); Ti Merradio-> setenabled (true); timeedit-> setenabled (true); countedit-> setenabled (true); susaction-> setenabled (false ); startbutton-> settext (TR ("Start Time shutdown"); m_start = false ;}} void shutdown: setremaintime (const qtime & time, int type) {If (! M_mini) {If (type = remain_time) {remaintimeline-> settext (time. tostring (TR ("HH mm min SS seconds");} If (type = to_shutdown) {remaintimeline-> settext (TR ("shutting down... ");} If (type = shutdown_error) {remaintimeline-> settext (TR (" shutdown failed... "); countdownradio-> setenabled (true); timerradio-> setenabled (true); timeedit-> setenabled (true); countedit-> setenabled (true ); susaction-> setenabled (false); startbutton-> settext (TR ("Start Time shutdown"); m_start = false ;}} if (m_mini & m_suspend) {setsustime (time, type) ;}} void shutdown: setcurrenttime (const qtime & time) {systemtimeline-> settext (time. tostring (TR ("HH mm min SS seconds ")));}

For QT, there is also a main function:

Main. cpp

#include <QtGui/QApplication>#include <QtCore/QTextCodec>#include "shutdown.h"int main(int argc, char *argv[]){QApplication a(argc, argv);QTextCodec::setCodecForTr(QTextCodec::codecForName("gb2312"));ShutDown w;w.show();return a.exec();}

So far, a simple Timed Shutdown program has been compiled. I am also a beginner in QT, and the code may be a little cool. I just want to share my first experience in using QT programming.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.