Results 1 to 15 of 15

Thread: Simple QTimer help

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2015
    Posts
    5
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    1

    Default Re: Qtimer help

    hello there,
    i am newbee in the Qt .
    i have a code which is given as
    Qt Code:
    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. }
    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Qtimer help

    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,
    _

  3. #3
    Join Date
    Jul 2015
    Posts
    5
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    1

    Default Re: Simple QTimer help

    @anda_skoa
    and how can i do that sir......i really have don't idea about it...
    please guide me to it.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Simple QTimer help

    You are kidding right?
    I already gave you two options and you want more?

    Cheers,
    _

  5. #5
    Join Date
    Jul 2015
    Posts
    5
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    1

    Default Re: Simple QTimer help

    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..
    Qt Code:
    1. void HelloForm::stopi2c()
    2. {
    3.  
    4. ADC->setText(" ");
    5. ptimer->stop();
    6. Message->setText("Stopped");
    7. }
    To copy to clipboard, switch view to plain text mode 
    like this??
    if yes then i doesn't work

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Simple QTimer help

    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,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    gauravsharma0190 (3rd July 2015)

  8. #7
    Join Date
    Jul 2015
    Posts
    5
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    1

    Default Re: Simple QTimer help

    so how can i do that sir...please tell me so???
    where should i change..
    if you don't mind??

  9. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Simple QTimer help

    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,
    _

  10. #9
    Join Date
    Jul 2015
    Posts
    5
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    1

    Default Re: Simple QTimer help

    hello sir,
    i did it...
    now i want to do it with startTimer() and killTimer()
    so how can i do it with???

  11. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Simple QTimer help

    Quote Originally Posted by gauravsharma0190 View Post
    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,
    _

Similar Threads

  1. Simple way to expand all nodes on a QTreeView?
    By cboles in forum Qt Programming
    Replies: 10
    Last Post: 12th April 2014, 16:54
  2. using QTimer in staticlib
    By jeetu_happy in forum Qt Programming
    Replies: 4
    Last Post: 22nd January 2007, 09:34
  3. QTimer based Splash Screen
    By bpetty in forum Newbie
    Replies: 6
    Last Post: 15th December 2006, 00:51
  4. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  5. QTimer issue
    By qball2k5 in forum Qt Programming
    Replies: 1
    Last Post: 31st March 2006, 18:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.