• 5574阅读
  • 5回复

关于QTimer的问题,请指教,谢谢! [复制链接]

上一主题 下一主题
离线jackyjun
 
只看楼主 倒序阅读 楼主  发表于: 2006-05-19
我有一个关于QTimer的问题:
我现在用qt/e作一个仪器的界面,平常没有信号测试的时候,就在液晶界面上呈现随机的波形。我添加了一个定时器,50ms一个定时。等timeout()时,界面update()重画一场随机的波形。可是问题是,在程序运行的时候,发现波形是以1s为定时周期刷新的,而不是按照程序设定的50ms刷新的。我调整start(int msec)里的参数,发现改成2000后,波形就按照2s的周期刷新,而改成1000以下,无论什么数,波形始终按照1s的周期刷新。
请问大家,这是为什么呢?怎么样才能让我的随机波形以更快的速度刷新呢?
代码付下,请大家帮我看看,谢谢!

#include "mygui.h"
//#include "mymenu.h"
#include <qpen.h>
#include <qpixmap.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <qpaintdevice.h>
#include <qbitmap.h>
#include <qcolor.h>


MyMainWindow::MyMainWindow()
{
   screentimer = new QTimer(this);
   setBackgroundColor(QColor(80,80,80));
   setBackgroundMode(NoBackground);
   setGeometry(0,0,320,240);
   setupbutton = new QPushButton("Setup",this);
   setupbutton->setGeometry(0,0,40,25);
   setupmenu = new MyMenuSetup(this);
   setupbutton->setPopup(setupmenu);    

   filebutton = new QPushButton("File",this);
   filebutton->setGeometry(41,0,40,25);
   filemenu = new MyMenuFile(this);            
   filebutton->setPopup(filemenu);    

   resultbutton = new QPushButton("Result",this);
   resultbutton->setGeometry(82,0,40,25);
   
   helpbutton = new QPushButton("Help",this);
   helpbutton->setGeometry(123,0,40,25);
   
   startTimer(0);
   connect(screentimer,SIGNAL(timeout()),this,SLOT(repaint()));
   screentimer->changeInterval(1);
   screentimer->start(50);
   
}

void MyMainWindow::keyPressEvent(QKeyEvent*k)
{
   switch(k->ascii())
   {

       case 'a':
           filebutton->setDown(false);
           resultbutton->setDown(false);
           helpbutton->setDown(false);
           setupbutton->setDown(true);
           //setupbutton->setFocus();
           setupmenu->popup(QPoint(2,30));
           //setupbutton->setFocus();
           break;
       case 'b':
           setupbutton->setDown(false);
        resultbutton->setDown(false);
       helpbutton->setDown(false);
           filebutton->setDown(true);
           //filebutton->setFocus();
           filemenu->popup(QPoint(42,30));
           //filebutton->setFocus();
           break;
       case 'c':
       setupbutton->setDown(false);      
       filebutton->setDown(false);
       helpbutton->setDown(false);
           resultbutton->setDown(true);
           resultbutton->setFocus();
           break;
       case 'd':
    setupbutton->setDown(false);      
    resultbutton->setDown(false);
    filebutton->setDown(false);
           helpbutton->setDown(true);
           helpbutton->setFocus();
           QApplication::exit();
           break;
   }
}


void MyMainWindow::paintEvent(QPaintEvent *)
{
   int i;
   int value;
   int bak_value=130;
   int w=this->width();
   int h=this->height();
   QPen exborderpen(blue,2,SolidLine);
    QPen inblankpen(black,1,DotLine);
   QPen datapen(cyan,1,SolidLine);    

   QPixmap pm(w,h);
   pm.fill(white);
   srand(time(NULL));
   QPainter paint(&pm);
   paint.setPen(exborderpen);
   paint.drawRect(8,30,304,200);
   
   paint.setPen(inblankpen);
   for(i=1;i<8;i++)
   {
       paint.drawLine(10,30+i*25,309,30+i*25);
   }
   for(i=1;i<12;i++)
   {
       paint.drawLine(10+i*25,30,10+i*25,229);
   }
   
   paint.setPen(datapen);
   for(i=0;i<299;i++)
   {
       value=rand()%50+105;
       paint.drawLine(10+i,bak_value,11+i,value);
       bak_value = value;
   }
   
   paint.end();
   QPainter painter(this);
   painter.drawPixmap(0,0,pm,0,0);
}
离线jackyjun
只看该作者 1楼 发表于: 2006-05-22
难道没有人回答么?请大家帮帮我吧!
离线jackyjun
只看该作者 2楼 发表于: 2006-05-22
已解决,是paintEvent里面srand(time(NULL))的问题,去掉就好了!
离线ironner

只看该作者 3楼 发表于: 2006-05-22
恭喜 ,楼主有QQ 没啊
离线ironner

只看该作者 4楼 发表于: 2006-05-22
我一直关注你呢,可不可以把源代码都传上来让我们借鉴一下!
离线fengyuan1220
只看该作者 5楼 发表于: 2006-08-16
我也是做这方面的开发,为什么不用QWT第三方控件呢,很方便,不过我还有些疑问,QQ250297124留下,有空可以交流
快速回复
限100 字节
 
上一个 下一个