hitcounter
dpointer
Sunday, January 30, 2005
 
Final Countdown

Do you want to know how many days are still until certain date? This simple countdown program will do it for you. When you run it, a number will shown in the top right part of your desktop; this is number of days until August 17th, 2005.

Basically it uses QDate to calculate the date difference. Also, this small code illustrates the use of unique combination of WStyle flags to show a small window without the normal titlebar and border. Neat for some sort of trick.

For the braves, here are some suggestion to improve. First of all, use QTimer to update the countdown; not to often but say every an hour or so. This means that if you start the program and let it run forever, it will decrease the numbers of days automatically. Next thing of course to configure the due date, as now it is hardcoded in the source code. Last (perhaps), add a way to terminate the program without (x)kill.


#include <qapplication.h>
#include <qdatetime.h>
#include <qlabel.h>
#include <qmainwindow.h>

class Countdown : public QMainWindow
{
public:
  Countdown();
};

Countdown::Countdown(): QMainWindow( 0, "countdown", 
	     WStyle_StaysOnTop | 
         WStyle_Customize | 
         WStyle_NoBorder | 
         WStyle_Tool | 
         WX11BypassWM )
{
  QLabel* label = new QLabel( this );
  setCentralWidget( label );
  
  QDate ref = QDate( 2005, 8, 17 ); // 17 Aug 2005
  int days = QDate::currentDate().daysTo( ref );  
  QString text = QString("%1 days").arg( days );
  
  label->setEraseColor( QColor(255,255,153) );
  label->setText( text );
  label->setAlignment( AlignCenter );
  label->show();
  label->raise();
  
  int scr = QApplication::desktop()->screenNumber( this );
  QRect rect = QApplication::desktop()->screenGeometry( scr );
  move( rect.width() - width() - 70, 1 );
  resize( 79, 18 );
}

int main( int argc, char ** argv )
{
  QApplication a( argc, argv );
  QMainWindow* v = new Countdown( );
  v->show();
  a.connect( &a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()) );
  return a.exec();
}



Powered by Blogger