【问题标题】:Updating ac value for Qtimer更新 Qtimer 的 ac 值
【发布时间】:2015-06-16 07:20:23
【问题描述】:

我正在为 mini2440 使用 qt 工具。 我为具有 starti2c 和 stopi2c 的 i2c adc 制作了一个 gui。 一切运行良好:按下 starti2c 按钮时读取信号值,但我希望更新此值。我知道可以使用Qtimer,但是我该怎么做呢? 代码如下:


# 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()));

}

HelloForm::~HelloForm()  

{

}

//*********************Code for getting i2c**************************// 
  char HelloForm::geti2c()
{

  char buf[100];                              
  char buff[100];     
  char valuee;

  int m1;   

  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;

   //********i2cdetect and read************

   buf[0]=0x48;    

  buf[1]=0x00;   

  buf[2]=0x91;  

  io=ioctl(fd,I2C_SLAVE,0x48); 

  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));

rbyte=read(fd,buff,10);    

//ADC->setText(buff);

sscanf(buff,"%c",&valuee);             

m1=int(valuee);     

return(m1);

}


void HelloForm::starti2c()

{


while(1)

{   

float adc_val=0;

adc_val=geti2c();

adc_val=(adc_val*5)/255.00;

usleep(1*1000);   

ADC->setText(QString::number(adc_val));   
  }
  }

//***********stop********//
void HelloForm::stopi2c()
{                        

   ADC->setText(" ");
Message->setText("Stopped");
}        

【问题讨论】:

  • 谁能告诉我...紧急我正在使用 trolltech qt 2.2.0
  • 我不太明白一个问题。但是QTimer 的timeout 信号用于更新。还有Qt 2.2?这是 15 岁。您可能应该考虑使用更新的版本
  • i want this value to be update => 哪一个? @Bowdzone 他可能指的是Qt Creator 2.2,它有点年轻
  • 先生,我想每 2 秒更新一次 adc value starti2c() 函数的值……我在谷歌上试过,但我不明白我怎么能在我的这个程序中做到这一点。 .我是qt的新手

标签: qt user-interface adc


【解决方案1】:

希望这可以帮助您入门 - 它会创建一个每 1000 毫秒超时的计时器。计时器的超时信号连接到 PushButton1 连接到的同一插槽 - starti2c。

QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(starti2c()));
timer->start(1000);

该代码应放在您拥有 2 个连接语句的下方。

【讨论】:

  • 你需要#include
  • HelloForm::HelloForm( QWidget* parent, const char* name, WFlags fl): HelloBaseForm(parent, name, fl) { connect(PushButton1,SIGNAL(clicked()),this,SLOT( starti2c()));连接(PushButton2,SIGNAL(点击()),这个,SLOT(stopi2c())); QTimer *timer = new QTimer(this);连接(定时器,信号(超时()),这个,插槽(starti2c()));计时器->开始(1000); }
  • hello.cpp:6:18: 错误: QTimer: 没有这样的文件或目录 hello.cpp: 在构造函数中'HelloForm::HelloForm(QWidget*, const char*, uint)': 你好。 cpp:47: 错误: 使用不完整类型'struct QTimer'/opt/FriendlyARM/mini2440/arm-qtopia/qtopia-2.2.0-FriendlyARM/qt2/include/qwindowdefs.h:81: 错误: 前向声明' struct QTimer' hello.cpp:48: error: no matching function for call to 'HelloForm::connect(QTimer*&, const char [11], HelloForm* const, const char [12])'
  • /opt/FriendlyARM/mini2440/arm-qtopia/qtopia-2.2.0-FriendlyARM/qt2/include/qobject.h:208:注意:bool QObject::connect(const QObject*, const char*, const char*) const hello.cpp:49: 错误:无效使用不完整类型 'struct QTimer' /opt/FriendlyARM/mini2440/arm-qtopia/qtopia-2.2.0-FriendlyARM/qt2/include/qwindowdefs。 h:81:错误:'struct QTimer' hello.cpp 的前向声明:在成员函数'char HelloForm::geti2c()'中:hello.cpp:74:警告:未使用的变量'i' make:*** [。 obj/release-shared/hello.o] 错误 1
  • 是的!!!它有效.....这些错误与使用 QTimer.h 将其更正为 qtimer....现在通过按下按钮更新值......但是当按下停止时它停止并重新开始...... .
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-30
  • 1970-01-01
  • 2019-02-01
  • 1970-01-01
  • 2018-08-03
相关资源
最近更新 更多