简体   繁体   English

Qt5不会发送超时信号

[英]Qt5 won't send timeout signal

I'm new to QT5, and I'm having a very strange problem. 我是QT5的新手,但遇到一个非常奇怪的问题。 I want to use QTimer to read coordinate from serial device every 0.5 second, but the QTimer won't send timeout signal. 我想使用QTimer每0.5秒从串行设备读取坐标,但是QTimer不会发送超时信号。

My .h: 我的.h:

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

public slots:
    void test();

private:
    Ui::MainWindow *ui;
};

My .cpp: 我的.cpp:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QTimer myTimer(this);
    myTimer.setInterval(500);
    myTimer.setSingleShot(false);
    connect(&myTimer, SIGNAL(timeout()), this, SLOT(test()));
    myTimer.start();
}

void MainWindow::test() {
    qDebug() << "Time out";
}

MainWindow::~MainWindow()
{
    delete ui;
}

After I started the program, not a single "Time out" shows up... 启动程序后,没有一个“超时”出现。

I added the following codes after "myTimer.start()": 我在“ myTimer.start()”之后添加了以下代码:

QTime t = QTime::currentTime().addMSecs(550);
while (QTime::currentTime() < t) {
    qDebug() << myTimer.remainingTime();
}

And I found that after the remaining time of "myTimer" reduced to 0, it won't refill the remaining time and start once again, it remains at 0. 而且我发现,在“ myTimer”的剩余时间减少为0之后,它不会重新填充剩余时间并再次开始,而是保持为0。

Q_OBJECT is already added Q_OBJECT已添加

Anyone got an idea? 有人知道吗?

Thank you very much! 非常感谢你!

The problem is here: 问题在这里:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QTimer myTimer(this);
    myTimer.setInterval(500);
    myTimer.setSingleShot(false);
    connect(&myTimer, SIGNAL(timeout()), this, SLOT(test()));
    myTimer.start();
}

In the constructor you declared myTimer automatically, so it will be destroyed as soon as constructor returns. 在构造函数中,您自动声明了myTimer ,因此构造函数返回后将立即销毁它。 This way, by the time when you expect timeout event, myTimer does not exist anymore and thus it cannot send any signals! 这样,到您预期超时事件时, myTimer不再存在,因此它无法发送任何信号!

The solution is straightforward: myTimer should exist all the time MainWindow object lives, so declare it in the class, not in it's constructor, and allocate it dynamically: 解决方案很简单: myTimer应该一直存在于MainWindow对象存在的所有时间,因此请在类中而不是在其构造函数中声明它,并动态分配它:

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

public slots:
    void test();

private:
    QTimer *myTimer; // <--- pointer declaration.
    Ui::MainWindow *ui;
};

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // Allocating actual QTimer object:
    myTimer = new QTimer(this);
    // Calling methods by object pointer:
    myTimer->setInterval(500);
    myTimer->setSingleShot(false);
    connect(myTimer, SIGNAL(timeout()), this, SLOT(test()));
    myTimer->start();
}

MainWindow::~MainWindow()
{
    // Don't forget to delete after usage.
    delete myTimer;
    delete ui;
}

myTimer is local. myTimer是本地的。

Make it a class member and use it. 使其成为班级成员并使用它。 It will work. 它会工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM