In which case,the timerEvent no work?



  • class MyObject : public QObject
    {
    public:
        MyObject(QObject *parent = 0) : QObject(parent)
        {
        }
    
        void startT()
        {
            cc1 = 0,cc2=0,cc3=0;
            it1=startTimer(100);     // 50-millisecond timer
            it2=startTimer(200);   // 1-second timer
            it3=startTimer(500);  // 1-minute timer
        }
        void stopt() {killTimer(it1);cc1++;}
        void stopt1(){killTimer(it2);cc2++;}
        void stopt2(){killTimer(it3);cc3++;}
        void doSomething(){}
    
    protected:
        int it1,it2,it3;
        int cc1,cc2,cc3;
        void timerEvent(QTimerEvent *event)
        {
            if(event->timerId()==it1)
                cc1++;
            if(event->timerId()==it2)
                cc2++;
            if(event->timerId()==it3)
                cc3++;
    
            if(cc1 == 100) stopt();
            if(cc2 == 60) stopt1();
            if(cc3 == 30) stopt2();
    
        }
    };
    

    Like this class,doSomething maybe 100 msec invoking.



  • It's probably just me but I did not understand the question. what is the problem here?


  • Moderators

    @Wang.Mr I have no idea what this code is supposed to do - it just cannot work. What is startTimer(), what is killTimer(), ...?
    If you need a timer why don't you use QTimer http://doc.qt.io/qt-5/qtimer.html ?

    it1=startTimer(100);     // 50-millisecond timer
    it2=startTimer(200);   // 1-second timer
    it3=startTimer(500);  // 1-minute timer
    

    if 100 is 50 milliseconds, then how can 200 be a second?



  • @jsulm said in In which case,the timerEvent no work?:

    What is startTimer(), what is killTimer(), ...?

    Those are QObject methods, QTimer uses them internally anyway


  • Moderators

    @VRonin Yes, you're right. I thought it would be something defined in MyObject.



  • @VRonin Oh,my english too bad!That is when i call 'doSomething','timerEvent' never triggered.


  • Moderators

    @Wang.Mr What is doSomething() doing? In the code you posted before it does nothing.



  • @jsulm Ok, this is my code .```

    uint MChannel::unChannelCount = 0;
    QByteArray MChannel::lstUseFlag;
    QString MChannel::filePath;

    MChannel::MChannel(QObject *parent) : QObject(parent)
    {
    if(lstUseFlag.count() == int(unChannelCount)){
    unIqueness = unChannelCount++;
    lstUseFlag.append('1');
    }else{
    for(int i = 0 ;i < lstUseFlag.count();i++)
    {
    if(lstUseFlag.at(i) == '0')
    {
    lstUseFlag[i] = '1';
    unIqueness = i;
    unChannelCount++;
    break;
    }
    }
    }

    //
    color = Qt::gray;
    strTipInfor = QString("Wait config");
    pTechnology = NULL;
    pcurStep = NULL;
    this->fCount = 0.0;
    this->bChannelOnline = false;
    this->bHaveSetupTechnology = false;
    this->bHaveWarnning = false;
    this->fE = 0.0;
    this->fELast = 0.0;
    this->fV = 0.0;
    this->fVLast = 0.0;
    this->Status = ChannelStatu_Stop;
    this->runStatus = Channel_Work_Status_None;
    this->pcurStep = NULL;
    this->unCurStepRunTime = 0;
    this->unCurTime = 0;
    this->unLastTime = 0;
    this->unTotalRunTime = 0;
    this->unID = 0;
    bCurrentStepFinish = false;
    bHaveNextStepRequest = false;
    bStartChannel = false;
    bFileCreate = false;
    iTimerID = 0;
    

    }
    MChannel::~MChannel()
    {
    if(iTimerID > 0)
    killTimer(iTimerID);
    if(bFileCreate )
    file.close();
    lstUseFlag[unID]='0';
    if(pTechnology != NULL)
    delete pTechnology;
    }

    bool MChannel::setUseTechnology(MTechnology* technology)
    {
    if(pTechnology != NULL)
    {
    delete pTechnology;
    }
    pTechnology = new MTechnology(technology);
    bHaveSetupTechnology = true;
    pdfns = &technology->dfns;
    UpdateData();
    return true;
    }
    void MChannel::UpdateData()
    {
    switch(runStatus )
    {
    case Channel_Work_Status_None:
    strTipInfor = QString("通道 %1 等待确认").arg(unID);
    color = Qt::lightGray;
    break;
    case Channel_Work_Status_Normal:
    strTipInfor = "就绪";
    color = QColor("lightskyblue");
    break;
    case Channel_Work_Status_Delay:
    strTipInfor = "非正常停机";
    color = ColorTechStepType_Fault;
    break;
    case Channel_Work_Status_NoBattery:
    strTipInfor = "无电池";
    color = ColorTechStepType_Fault;
    break;
    case Channel_Work_Status_Resev:
    break;
    case Channel_Work_Status_ReverseBattery:
    strTipInfor = "电池接反";
    color = ColorTechStepType_Fault;
    break;
    case Channel_Work_Status_TemperatureH:
    strTipInfor = "温度过高";
    color = ColorTechStepType_Fault;
    break;
    case Channel_Work_Status_VAPulsation:
    strTipInfor = "电压电流波动";
    color = ColorTechStepType_Fault;
    break;
    case Channel_Work_Status_Abnormal:
    strTipInfor = "通道异常";
    color = ColorTechStepType_Fault;
    break;
    case Channel_Work_Status_NeedTechnology:
    strTipInfor = "请求全局参数";
    color = Qt::black;
    break;
    case Channel_Work_Status_NeedStep:
    strTipInfor = "请求工步";
    color = QColor("blueviolet");
    break;
    case Channel_Work_Status_WFixAC:
    strTipInfor = "恒流充";
    color = ColorTechStepType_Charge_Fix_E;
    break;
    case Channel_Work_Status_WFixVC:
    strTipInfor = "恒压充";
    color = ColorTechStepType_Charge_Fix_V;
    break;
    case Channel_Work_Status_WFixAA:
    strTipInfor = "恒流放";
    color = ColorTechStepType_Acquittal_Fix_E;
    break;
    case Channel_Work_Status_WFixPC:
    strTipInfor = "恒功率充";
    color =QColor("greenyellow") /ColorTechStepType_Charge_Fix_P/;
    break;
    case Channel_Work_Status_WFixPA:
    strTipInfor = "恒功率放";
    color = ColorTechStepType_Acquittal_Fix_P;
    break;
    case Channel_Work_Status_WFixSt:
    strTipInfor = "静止充";
    color = QColor("brown")/ColorTechStepType_JingZhi/;
    break;
    case Channel_Work_Status_FinishWork:
    strTipInfor = "工艺完成";
    color = ColorTechStepType_Finish;
    break;
    }

    if(bStartChannel)
    {
        if(this->bHaveSetupTechnology == false)
            strTipInfor = QString("未工艺设置\n%1").arg(strTipInfor);
        else
            strTipInfor = QString(" V:%1\n I:%2\n C:%3").arg(fV).arg(fE).arg(fCount);
        //strTipInfor = QString(" V:%1\n I:%2\n C:%3").arg(fV,5,'g',4,'0').arg(fE,10,'g',4,'0').arg(fCount,10,'g',4,'0');
    }
    
    emit signalUpdatewX();
    

    }

    //update data,that is called from other place by 1mes or more fast
    void MChannel::UpdateData(float fv,float fe,int tm,quint8 st)
    {
    int iTimeCha=0;
    qreal tlen = 0;

    fVLast = fV;
    fELast = fE;
    unLastTime = unCurTime;
    
    fV = fv;
    fE = fe;
    unCurTime = tm;
    
    iTimeCha = unCurTime - unLastTime;
    if(iTimeCha < 0) iTimeCha = (-iTimeCha);
    tlen = (iTimeCha) / 1000;
    
    switch(Channel_Work_Status(st))
    {
    case Channel_Work_Status_Normal:
    case Channel_Work_Status_NoBattery:
    case Channel_Work_Status_ReverseBattery:
    case Channel_Work_Status_Abnormal:
    case Channel_Work_Status_VAPulsation:
    case Channel_Work_Status_TemperatureH:
    case Channel_Work_Status_Delay:
    case Channel_Work_Status_Resev:
    case Channel_Work_Status_NeedTechnology:
    case Channel_Work_Status_None:
        runStatus = Channel_Work_Status(st);
        break;
    case Channel_Work_Status_NeedStep:
    case Channel_Work_Status_WFixAC:
    case Channel_Work_Status_WFixVC:
    case Channel_Work_Status_WFixAA:
    case Channel_Work_Status_WFixPC:
    case Channel_Work_Status_WFixPA:
    case Channel_Work_Status_WFixSt:
    case Channel_Work_Status_FinishWork:
    {
        if(bFileCreate)
        {
            if(lastTime.elapsed() >= int(MTechnology::dfns.uInterval*1000))
            {
                lastTime.restart();
                out << QTime::currentTime().toString("hh:mm:ss:zzz") << "," << fv << "," << fe << "," << runStatus <<"\n";
                out.flush();
                file.flush();
            }
        }
        if(runStatus != Channel_Work_Status(st))
        {
            fCount = 0;
            runStatus = Channel_Work_Status(st);
            if(runStatus == Channel_Work_Status_WFixAA || runStatus == Channel_Work_Status_WFixPA)
            {
                fValFlag = -1;
            }
            else
            {
                if(runStatus == Channel_Work_Status_FinishWork)
                {
                    stopTechnology();
                }
                fValFlag = 1;
            }
        }
        break;
    }
    
    }
    
    unTotalRunTime  = timeBegin.secsTo(QDateTime::currentDateTime()) / 60;
    fCount = fCount + (((tlen * fE) / 3600.0)*fValFlag);
    
    UpdateData();
    

    }
    //record data
    void MChannel::recordData()
    {
    #ifdef DEBUG_OUT
    qDebug()<< "记录数据:" << dataFileName ;
    #endif
    out << QTime::currentTime().toString("hh:mm:ss") << "," << fV << "," << fE << "," << runStatus <<"\n";
    out.flush();
    file.flush();
    }

    //here,open timer
    void MChannel::startTechnology()
    {
    QString str;
    //文件名格式
    dataFileName = QString("%1").arg(QDateTime::currentDateTime().toString(pdfns->strDate));

    if(pdfns->bUsePortName)
        dataFileName.append(chnUIInfo->strCommAddr);
    if(pdfns->bUseAddrNo)
    {
        str = QString("%1").arg(chnUIInfo->uiTermAddr,3,10,QChar('0'));
        dataFileName.append(str);
    }
    if(pdfns->bUseBitNo)
    {
        str = QString("%1").arg(chnUIInfo->uiChnAddr,5,10,QChar('0'));
        dataFileName.append(str);
    }
    dataFileName.append(".dat");
    //工艺工步设置到当前工步
    pTechnology->gotoStep(1);
    pcurStep = pTechnology->getStepOf(1)->data;
    bStartChannel = true;
    bHaveNextStepRequest = false;
    bTechnologyStepSendFinish = false;
    //创建文件
    if(file.isOpen())
        file.close();
    file.setFileName(dataFileName);
    bFileCreate = file.open(QIODevice::WriteOnly|QIODevice::Text);
    if(bFileCreate)
    {
        out.setDevice(&file);
        iTimerID = startTimer(int(MTechnology::dfns.uInterval*1000));
        lastTime.start();
        unCurTime = lastTime.second()*1000;
    }
    else
    {
    

    #ifdef DEBUG_OUT
    qDebug()<< "创建记录文件失败:" << dataFileName ;
    #endif
    }
    }

    //here end timer
    void MChannel::stopTechnology()
    {
    #ifdef DEBUG_OUT
    qDebug()<< "停止记录:" << dataFileName ;
    #endif
    bStartChannel = false;

    if(bFileCreate)
    {
        if(iTimerID > 0)
            killTimer(iTimerID);
        iTimerID = 0;
        file.close();
        bFileCreate = false;
    }
    

    }
    void MChannel::timerEvent( QTimerEvent* event )
    {
    if(event->timerId() == iTimerID)
    recordData();
    }


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.