添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Welcome to Qt Centre .

Qt Centre is a community site devoted to programming in C++ using the Qt framework . Over 90 percent of questions asked here gets answered. If you are looking for information about Qt related issue — register and post your question.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today !

If you have any problems with the registration process or your account login, please contact us .

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:
Qt Code: Switch view
  1. void button_clicked()
  2. {
  3. QTimer *timer = new QTimer();
  4.  
  5. If ( button->text == “Start” )
  6. {
  7. button->setText(“Stop”);
  8. timer->start(5000);
  9. printf(“Hello World\n”);
  10. }
  11. else
  12. {
  13. timer->stop();
  14. button->setText(“Start”);
  15. }
  16. }
void button_clicked()
	QTimer *timer = new QTimer();
	If ( button->text == “Start” )
        button->setText(“Stop”);
		timer->start(5000);
		printf(“Hello World\n”);
		timer->stop();
		button->setText(“Start”);
To copy to clipboard, switch view to plain text mode 
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, 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):
Qt Code: Switch view
  1. QTimer *timer = new QTimer( myObject );
  2. connect( timer, SIGNAL(timeout()), myObject, SLOT(timerDone()) );
  3. timer->start( 5000, FALSE );
QTimer *timer = new QTimer( myObject );
connect( timer, SIGNAL(timeout()), myObject, SLOT(timerDone()) );
timer->start( 5000, FALSE );
To copy to clipboard, switch view to plain text mode 
So you would also need to make a slot (function) called timerDone(), in which you would put your printf statement. Sorry, that probably wasn't that helpful, let me try again.
In your header you should have something like this:
Qt Code: Switch view
  1. public:
  2. QTimer *timer;
  3.  
  4. public slots:
  5. virtual void timerDone();
public:
   QTimer *timer;
public slots:
  virtual void timerDone();
To copy to clipboard, switch view to plain text mode 
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:
Qt Code: Switch view
  1. timer = new QTimer(this);
  2. connect( timer, SIGNAL(timeout()), SLOT(timerDone()) );
timer = new QTimer(this);
connect( timer, SIGNAL(timeout()), SLOT(timerDone()) );
To copy to clipboard, switch view to plain text mode 
That actually creates the timer, assigns the slot to it.
To start the timer call:
Qt Code: Switch view
  1. timer->start(5000, false);
timer->start(5000, false);
To copy to clipboard, switch view to plain text mode 
and to stop it again:
Qt Code: Switch view
  1. timer->stop();
timer->stop();
To copy to clipboard, switch view to plain text mode 
You also need this in your code (assumes your program is 'timerexample' so just change that as appropriate:
Qt Code: Switch view
  1. void timerexample::timerDone()
  2. {
  3. printf("Hello World\n");
  4. }
void timerexample::timerDone()
 printf("Hello World\n");
To copy to clipboard, switch view to plain text mode 
Hope that helps. You can query the QTimer if it is active with the method "bool isActive()". So then you can do something like this:
Qt Code: Switch view
  1. //Somewhere in your program: in constructor maybe?
  2. YourObjectClass::YourObjectClass()
  3. {
  4. QTimer* qTimer = new QTimer();
  5. connect( qTimer, SIGNAL(timeout()), myObject, SLOT(timerDone()) );
  6. }
  7.  
  8. void YourObjectClass::button_clicked() //changing the state of the Timer with the button
  9. {
  10. if (qTimer->isActive())
  11. {
  12. qTimer->stop();
  13. }
  14. else
  15. {
  16. qTimer->start(5000);
  17. }
  18. }
  19.  
  20. void YourObjectClass::timerDone() //It will be called every 5 seconds until the timer is active
  21. {
  22. printf("Hello World\n");
  23. }
//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();
    qTimer->start(5000);
void YourObjectClass::timerDone()  //It will be called every 5 seconds until the timer is active
  printf("Hello World\n");
To copy to clipboard, switch view to plain text mode 
  1. #include "hello.h"
  2. #include <qlabel.h>
  3. #include <qpushbutton.h>
  4. #include <qtimer.h>
  5. #include <stdlib.h>
  6. #include <sys/ioctl.h>
  7. #include <fcntl.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <linux/fs.h>
  13. #include <errno.h>
  14. #include <string.h>
  15. #include <qobject.h>
  16. #include <fstream>
  17. #include <errno.h>
  18. #include <linux/i2c-dev.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <sys/ioctl.h>
  26. #include <fcntl.h>
  27. #include <linux/fs.h>
  28. #include <errno.h>
  29. #include <stdint.h>
  30. #include <getopt.h>
  31. #include <fcntl.h>
  32. #include <sys/ioctl.h>
  33. #include <linux/types.h>
  34. #include <linux/spi/spidev.h>
  35. # include <termio.h>
  36. # include <time.h>
  37. # include <string.h>
  38. # include <sys/time.h>
  39.  
  40. HelloForm::HelloForm( QWidget* parent, const char* name, WFlags fl):
  41. HelloBaseForm(parent, name, fl)
  42. {
  43. connect(PushButton1,SIGNAL(clicked()),this,SLOT(starti2c()));
  44. connect(PushButton2,SIGNAL(clicked()),this,SLOT(stopi2c()));
  45. QTimer *timer = new QTimer(this);
  46. QTimer *ptimer = new QTimer(this);
  47. connect(timer, SIGNAL(timeout()), this, SLOT(starti2c()));
  48. connect(ptimer,SIGNAL(timeout()), this, SLOT(stopi2c()));
  49. timer->start(1000);
  50. ptimer->stop();
  51. }
  52.  
  53. HelloForm::~HelloForm()
  54. {
  55. }
  56.  
  57. //*********************Code for getting i2c**************************//
  58. char HelloForm::geti2c()
  59. {
  60. char buf[100]; // BUFFER used for adc control word for writing on the i2c bus//
  61. char buff[100]; // BUFFER used for adc read
  62. char valuee;
  63. int m1; // variable used for data transfer from subroutine to main
  64. //char con_buff[10];
  65. int fd=open("/dev/i2c/0",O_RDWR);
  66. if (fd<0)
  67. {
  68. Message->setText(" NOT ABLE TO OPEN THE DRIVER ");
  69. }
  70. else
  71. {
  72. Message->setText(" I2C IS WORKING ");
  73. }
  74.  
  75. int io,wbyte,rbyte,i;
  76.  
  77. //********************************** i2c detect and Read***********************************************//
  78. buf[0]=0x48; // sending control word to adc for write mode in ch=0
  79. buf[1]=0x00; // I2C adress for arm on which adc connected
  80. buf[2]=0x91; // control word for adc set in to read mode
  81. io=ioctl(fd,I2C_SLAVE,0x48); // adress for arm for i2c communication
  82. if(io<0)
  83. {
  84. Message->setText(" ");
  85. Message->setText("error ioctl");
  86. }
  87. else
  88. {
  89. wbyte=write(fd,buf,3); // write all three control word to arm
  90. usleep(1*1000);
  91. }
  92. if(wbyte!=3)
  93. {
  94. Message->setText("error write");
  95. Message->setText(QString::number(wbyte)); //set the Text as int variable//
  96. }
  97. rbyte=read(fd,buff,10); //Read 10 words from adc driver fd//
  98. //ADC->setText(buff);
  99. sscanf(buff,"%c",&valuee); //scan char value in buff and put into the char variable valuee//
  100.  
  101. m1=int(valuee); //Type casting of char value to intger value//
  102. return(m1);
  103. }
  104. //*******************************Starting I2C function***********************************************************//
  105.  
  106. void HelloForm::starti2c()
  107. {
  108. //int i=0;
  109. float adc_val=0;
  110. adc_val=geti2c();
  111. adc_val=(adc_val*5)/255.00;
  112. usleep(1*1000); //adc conversion formulaaa//
  113. ADC->setText(QString::number(adc_val)); //print the adc value//
  114. }
  115.  
  116. //*********************************************************STOP I2C ********************************************//
  117. void HelloForm::stopi2c()
  118. {
  119.  
  120. ADC->setText(" ");
  121. // ptimer->stop();
  122. Message->setText("Stopped");
  123. }
#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(starti2c()));
    connect(PushButton2,SIGNAL(clicked()),this,SLOT(stopi2c()));
    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 ");
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");
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");
To copy to clipboard, switch view to plain text mode 
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. 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,
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..
Qt Code: Switch view
  1. void HelloForm::stopi2c()
  2. {
  3.  
  4. ADC->setText(" ");
  5. ptimer->stop();
  6. Message->setText("Stopped");
  7. }
void HelloForm::stopi2c()
       ADC->setText(" ");
      ptimer->stop();
	Message->setText("Stopped");
To copy to clipboard, switch view to plain text mode 
like this??
if yes then i doesn't work 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,
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,