PDA

View Full Version : How to start an action at application launch ?



fassage
3rd November 2009, 12:41
Hi All,

I have successfully implemented the progress bar example below but would like to eliminate the need for the start button !

I would like the progressBar to start as soon as the application is launched & close when the progress bar has been completed ??

Is this possible ? Can you please give a little detialed answers of how to implement this please ?

Many thanks in advance,

Fassage



************************************************** **************************/
#ifndef FORM1_H
#define FORM1_H

#include <qdialog.h>
#include <qtimer.h>

class QProgressBar;
class QPushButton;

class Form1 : public QDialog
{
Q_OBJECT

public:
Form1( QWidget* parent = 0, const char* name = 0, bool modal = FALSE, WFlags fl = 0 );
~Form1();

QPushButton *sb;
QProgressBar* pb;
QTimer timer;

protected slots:
void myiter();
void mystart();
};

#endif // FORM1_H

************************************************** **************************/
#include "form1.h"
#include <qpushbutton.h>
#include <qprogressbar.h>
#include <qtimer.h>

Form1::Form1( QWidget* parent, const char* name, bool modal, WFlags fl ) : QDialog( parent, name, modal, fl )
{
sb = new QPushButton( "&Start", this );
pb = new QProgressBar( this );
pb->setGeometry( QRect( 100, 0, 500, 30 ) );
pb->setTotalSteps( 10 );
pb->reset();

connect( sb, SIGNAL( clicked() ), this, SLOT( mystart() ) );
connect( &timer, SIGNAL( timeout() ), this, SLOT( myiter() ) );
}

Form1::~Form1() {};
void Form1::mystart() { if ( !timer.isActive() ) { timer.start(400); } else { timer.stop(); } }
void Form1::myiter() { int i = pb->progress(); pb->setProgress( ++i ); }

#include <qapplication.h>
#include "form1.h"

************************************************** **************************/
int main( int argc, char ** argv )
{
QApplication a( argc, argv );
Form1 w;
w.show();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}

caduel
3rd November 2009, 12:56
just call mystart() in your constructor (or show()); and call close() (e.g. in myiter()) when you are done

fassage
3rd November 2009, 13:09
Hi caduel !

Thats what i thought...

It worked perfect.

Many thanks,

Fassage

fassage
3rd November 2009, 13:11
Sorry forgot to post new working code for future beginners like myself !


************************************************** **************************/

#ifndef FORM1_H
#define FORM1_H

#include <qdialog.h>
#include <qtimer.h>

class QProgressBar;
class QPushButton;

class Form1 : public QDialog
{
Q_OBJECT

public:
Form1( QWidget* parent = 0, const char* name = 0, bool modal = FALSE, WFlags fl = 0 );
~Form1();

QProgressBar* pb;
QTimer timer;

protected slots:
void myiter();
};

#endif // FORM1_H

************************************************** **************************/

#include "form1.h"
#include <qprogressbar.h>
#include <qtimer.h>

Form1::Form1( QWidget* parent, const char* name, bool modal, WFlags fl ) : QDialog( parent, name, modal, fl )
{
pb = new QProgressBar( this );
pb->setGeometry( QRect( 0, 0, 500, 30 ) );
pb->setTotalSteps( 10 );
pb->reset();

connect( &timer, SIGNAL( timeout() ), this, SLOT( myiter() ) );

if ( !timer.isActive() ) { timer.start(400); } else { timer.stop(); }
}

Form1::~Form1() {};
void Form1::myiter() {
int i = pb->progress();
pb->setProgress( ++i );
if ( i == pb->totalSteps() ) { close(); }
}

************************************************** **************************/
#include <qapplication.h>
#include "form1.h"

int main( int argc, char ** argv )
{
QApplication a( argc, argv );
Form1 w;
w.show();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}