PDA

View Full Version : Simple QTimer help



philips
13th February 2007, 15:40
Hi guys,

I was wondering if someone could help me out with creating a button that when clicked, will print “Hello World” every 5seconds until the button is pressed again.

So far I have:


void button_clicked()
{
QTimer *timer = new QTimer();

If ( button->text == “Start” )
{
button->setText(“Stop”);
timer->start(5000);
printf(“Hello World\n”);
}
else
{
timer->stop();
button->setText(“Start”);
}
}



Obviously, as you can probably tell, I am new to Qt and threading. Right now, it just prints out the Hello World once. How do I make it keep going every 5 seconds?
Do I have to make it an event or something?

Any help code and explanation wise would be greatly appreciated.

Thanks,

December
13th February 2007, 15:55
OK, your code so far is not actually making use of the QTimer at all.

You need to create the QTimer and then connect it to a slot (from the QTimer documentation):



QTimer *timer = new QTimer( myObject );
connect( timer, SIGNAL(timeout()), myObject, SLOT(timerDone()) );
timer->start( 5000, FALSE );


So you would also need to make a slot (function) called timerDone(), in which you would put your printf statement.

philips
13th February 2007, 16:10
Thanks a lot.

Sorry, I missed the line:
From then on, the update ( ) slot is called every second.

December
13th February 2007, 16:16
Sorry, that probably wasn't that helpful, let me try again.

In your header you should have something like this:



public:
QTimer *timer;

public slots:
virtual void timerDone();


That creates the pointer to the timer (do it in the header to any part of your program can access it, and declares the slot that the timer will call when it reaches it's time-out.

Then in the constructor for your program:



timer = new QTimer(this);
connect( timer, SIGNAL(timeout()), SLOT(timerDone()) );


That actually creates the timer, assigns the slot to it.

To start the timer call:




timer->start(5000, false);



and to stop it again:




timer->stop();



You also need this in your code (assumes your program is 'timerexample' so just change that as appropriate:



void timerexample::timerDone()
{
printf("Hello World\n");
}


Hope that helps.

^NyAw^
13th February 2007, 16:28
Hi,

You can query the QTimer if it is active with the method "bool isActive()". So then you can do something like this:



//Somewhere in your program: in constructor maybe?
YourObjectClass::YourObjectClass()
{
QTimer* qTimer = new QTimer();
connect( qTimer, SIGNAL(timeout()), myObject, SLOT(timerDone()) );
}

void YourObjectClass::button_clicked() //changing the state of the Timer with the button
{
if (qTimer->isActive())
{
qTimer->stop();
}
else
{
qTimer->start(5000);
}
}

void YourObjectClass::timerDone() //It will be called every 5 seconds until the timer is active
{
printf("Hello World\n");
}

gauravsharma0190
3rd July 2015, 08:00
hello there,
i am newbee in the Qt .
i have a code which is given as


#include "hello.h"
#include <qlabel.h>
#include <qpushbutton.h>
#include <qtimer.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/fs.h>
#include <errno.h>
#include <string.h>
#include <qobject.h>
#include <fstream>
#include <errno.h>
#include <linux/i2c-dev.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <errno.h>
#include <stdint.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
# include <termio.h>
# include <time.h>
# include <string.h>
# include <sys/time.h>

HelloForm::HelloForm( QWidget* parent, const char* name, WFlags fl):
HelloBaseForm(parent, name, fl)
{
connect(PushButton1,SIGNAL(clicked()),this,SLOT(st arti2c()));
connect(PushButton2,SIGNAL(clicked()),this,SLOT(st opi2c()));
QTimer *timer = new QTimer(this);
QTimer *ptimer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(starti2c()));
connect(ptimer,SIGNAL(timeout()), this, SLOT(stopi2c()));
timer->start(1000);
ptimer->stop();
}

HelloForm::~HelloForm()
{
}

//*********************Code for getting i2c**************************//
char HelloForm::geti2c()
{
char buf[100]; // BUFFER used for adc control word for writing on the i2c bus//
char buff[100]; // BUFFER used for adc read
char valuee;
int m1; // variable used for data transfer from subroutine to main
//char con_buff[10];
int fd=open("/dev/i2c/0",O_RDWR);
if (fd<0)
{
Message->setText(" NOT ABLE TO OPEN THE DRIVER ");
}
else
{
Message->setText(" I2C IS WORKING ");
}

int io,wbyte,rbyte,i;

//********************************** i2c detect and Read********************************************** *//
buf[0]=0x48; // sending control word to adc for write mode in ch=0
buf[1]=0x00; // I2C adress for arm on which adc connected
buf[2]=0x91; // control word for adc set in to read mode
io=ioctl(fd,I2C_SLAVE,0x48); // adress for arm for i2c communication
if(io<0)
{
Message->setText(" ");
Message->setText("error ioctl");
}
else
{
wbyte=write(fd,buf,3); // write all three control word to arm
usleep(1*1000);
}
if(wbyte!=3)
{
Message->setText("error write");
Message->setText(QString::number(wbyte)); //set the Text as int variable//
}
rbyte=read(fd,buff,10); //Read 10 words from adc driver fd//
//ADC->setText(buff);
sscanf(buff,"%c",&valuee); //scan char value in buff and put into the char variable valuee//

m1=int(valuee); //Type casting of char value to intger value//
return(m1);
}
//*******************************Starting I2C function****************************************** *****************//

void HelloForm::starti2c()
{
//int i=0;
float adc_val=0;
adc_val=geti2c();
adc_val=(adc_val*5)/255.00;
usleep(1*1000); //adc conversion formulaaa//
ADC->setText(QString::number(adc_val)); //print the adc value//
}

//************************************************** *******STOP I2C ********************************************//
void HelloForm::stopi2c()
{

ADC->setText(" ");
// ptimer->stop();
Message->setText("Stopped");
}


here i need to start the timer when starti2c button is pressed and update the value 1000ms....
and by pressing the button it stops the update and stop the i2c...
in my code it it always update the value even after i press the stopi2c...
please help me for this.

anda_skoa
3rd July 2015, 09:30
You never stop the timer, so it keeps firing.

You can stop the timer in HelloForm::stopi2c() or connect the stop button's cliicked signal to the timer's stop() slot.

Cheers,
_

gauravsharma0190
3rd July 2015, 10:53
@anda_skoa
and how can i do that sir......i really have don't idea about it...
please guide me to it.

anda_skoa
3rd July 2015, 12:00
You are kidding right?
I already gave you two options and you want more?

Cheers,
_

gauravsharma0190
3rd July 2015, 12:20
sir,
i mean to say how to write in stopi2c() to stop this if you add lines in my code i will be oblize to you..

void HelloForm::stopi2c()
{

ADC->setText(" ");
ptimer->stop();
Message->setText("Stopped");
}
like this??
if yes then i doesn't work

anda_skoa
3rd July 2015, 12:41
Yes, like this.
But obviously with having "timer" as a member variable of class HelloForm instead of just a local variable in the constructor.

Cheers,
_

gauravsharma0190
3rd July 2015, 13:09
so how can i do that sir...please tell me so???
where should i change..
if you don't mind??

anda_skoa
3rd July 2015, 14:39
My recommendation is to have a look at some beginner's C++ tutorials first.
Once you understand the absolut basics of C++, you won't have any difficulty making the necessary change.

Cheers,
_

gauravsharma0190
6th July 2015, 08:54
hello sir,
i did it...
now i want to do it with startTimer() and killTimer()
so how can i do it with???

anda_skoa
6th July 2015, 09:39
now i want to do it with startTimer() and killTimer()
so how can i do it with???

Why?
Working with QTimer is way simpler.

Cheers,
_