Qtimer不起作用。我认为它没有更新

时间:2019-03-13 20:32:18

标签: c++ qt qtimer

我想每10毫秒发送udp数据包。 我创建了QTimer,但是SLOT无法正常工作。 我创建了一个无限循环,输出为timer-> remainingTime(),它似乎不想重置计时器。 在输出中: 10 10 10 ... 9 9 9 ... ... ... 1个 1个 1个 ... 0 0 0 0 0 0 然后是零也许这种方法是绝对错误的。我怎么还能做?

这是我的代码

usingbks.h

fieldset

usingbks.cpp

#ifndef USINGBKS_H
#define USINGBKS_H
#include "bks.h"
#include "sendtimer.h"
#include <stdlib.h>
#include <QtNetwork>
#include <thread>


class UsingBKS : public QObject
{
Q_OBJECT


private slots:
    void sendPackets();

private:
    QTimer* timer = new QTimer(this);
    QUdpSocket* socket = new QUdpSocket(this);

...

    int TrandmissionStart(QString ip, quint16 port);
    int SendBKSOutBKSCond();
    int SendBKSOutPrimaKVCond2();
};

#endif // USINGBKS_H

main.cpp

UsingBKS::UsingBKS()
{

}

UsingBKS::~UsingBKS()
{
    delete socket;
    delete timer;
}

void UsingBKS::sendPackets() {
    SendBKSOutBKSCond();
    SendBKSOutPrimaKVCond2();
}


...

int UsingBKS::TrandmissionStart(QString ip, quint16 port)
{
    socket->connectToHost(QHostAddress(ip), port);
    connect(timer, SIGNAL(timeout()), this, SLOT(sendPackets()));
    timer->start(1000);
    while(1) { //it`s only for test
        std::cout << timer->remainingTime() << " " << timer->isActive() << std::endl; 
    }
    return 1;
}

0 个答案:

没有答案