简体   繁体   English

在Qt 5.5中连续检查主循环

[英]Continuous check in the main loop in Qt 5.5

I have a device that moves on a linear path, basically a linear actuator . 我有一个在线性路径上移动的设备,基本上是一个线性执行器 When the device reaches the physical ends, it hits a limit contact which sends a signal to my software. 当设备到达物理端时,它会触及限制触点,该触点会向我的软件发送信号。 I will need to continually check if this signal is online. 我需要不断检查此信号是否在线。 But I'm having difficulties implementing this logic in Qt5.5. 但是我在Qt5.5中实现这个逻辑有困难。

I've been reading on QtConcurrent and it seems like a viable solution but after implementing it on a test drive I found out that I cannot solve my problem without some sort of a while(true) loop. 我一直在阅读QtConcurrent ,它似乎是一个可行的解决方案,但在测试驱动器上实现它后,我发现如果没有某种while(true)循环我无法解决我的问题。 However implementing a while(true) loop seems to slow down everything else on my code therefore rendering this solution completely useless. 然而,实现while(true)循环似乎会减慢我的代码中的其他所有内容,因此使此解决方案完全无用。

I would post code but given that it uses libraries and nomenclature of devices that are of a very specific niche I will spare you the pain but if anyone can guide me towards reading up on something like this I would be most grateful. 我会发布代码,但考虑到它使用了一个非常具体的利基设备的库和命名法,我将免除你的痛苦,但如果有人能指导我阅读这样的事情,我将非常感激。 I would prefer to steer clear of QtThread and basically manually setting up threads since I do not feel comfortable working with them at this point, and I do have a time limit on this project so it would be best if I don't experiment much. 我宁愿避开QtThread并基本上手动设置线程,因为我觉得QtThread使用它们,而且我对这个项目有一个时间限制,所以如果我不进行多少实验,那将是最好的。

tldr : I need to put some code somehow within the main loop of the program that checks for a boolean value change. tldr :我需要在程序的主循环中以某种方式放置一些代码来检查布尔值的变化。 The said change is sent to the program externally by a device which is communicating through Ethernet. 所述改变由通过以太网通信的设备在外部发送到程序。

class Checker : public QObject
{
    Q_OBJECT
public:
    Checker(void)
    {
        timer.setInterval(100);// give it a interval, like 100ms?
        connect(timer, SIGNAL(timeout()), this, SLOT(checkHW()));
    }
    void start(void)
    {
        timer.start();// although you can start the timer in the constructor 
                      // but I guess you want to start it later, after HW is   
                      // ready.
    }
private Q_SLOTS:
    void checkHW()
    {
        bool hit = false;
        // check the hardware here
        if(hit){
            emit hitEnd();// tell others about the event
        }
    }
signals:
    void hitEnd(void);
private:
    QTimer timer;
}

If checking the hardware switch doesn't take much time, then you don't really need another thread. 如果检查硬件开关没有花费太多时间,那么你真的不需要另一个线程。 However, if checkHW() does takes a lot of time, then it helps if this class is moved out of the main thread. 但是,如果checkHW()确实需要花费很多时间,那么如果将此类移出主线程,则会checkHW()帮助。

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

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