微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在while循环内更​​新Qt LCDNumber

如何解决如何在while循环内更​​新Qt LCDNumber

第一次使用 Qt,因为我是新手并且可能问了一个愚蠢的问题。

我正在构建的 Qt 应用程序是连接到一个在轮询时返回值的表。我使用 ZMQ 进行连接。

目标:应用程序将连接到表,并持续轮询位置和速度信息,然后更新两个 QLCDNumber 字段以显示值。

我知道我需要一个信号和插槽

  • 如何创建与执行更新的函数的连接?

.h 文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <string>
#include <zmq.h>

#include "tableSocketMonitor.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void connectAndPoll();          // Connects to the Table and Polls for info.

signals:
    void updateValues(double p,double v); // Updates the GUI fields.

private:
    Ui::MainWindow *ui;
    bool isConnected = true;       // Bool - Changes to true when connected to table.
};
#endif // MAINWINDOW_H

.cpp 文件

#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "tableSocketMonitor.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iomanip>
#include <string>
#include <iostream>
#include <zmq.hpp>

const std::string ipAddr = "tcp://*";

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setwindowTitle("Position / VeLocity GUI");
    setMinimumSize(331,221);

    connectAndPoll();
}

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

void MainWindow::updateValues(double p,double v){
    // update Position field
    ui->pLCD->display(p);
    // Update VeLocity field
    ui->vLCD->display(v);
}

void MainWindow::connectAndPoll(){
        zmq::context_t context (1);                             // Creating context for connection.
        zmq::socket_t socket(context,zmq::socket_type::req);   // Setting socket with REQUEST type. Table is a REPLY type.
        socket.set(zmq::sockopt::routing_id,"gui");             // Setting the routing ID for the socket

        socketMonitor monitor;                                  // Creating monitor to check for socket events.
        const int events = ZMQ_EVENT_CONNECTED;                 // Only Event we are interested in. at this time.
        monitor.init(socket,"inproc://conmon",events);        // Initialize the monitor.

        // While the program is not connected to the rate table,attempt connection.
        while(!isConnected){
            try{
                socket.connect(ipAddr);                         // Connect to the ip address
                if(monitor.check_event(10)){                    // Wait 10ms - *IF* the CONNECT event kicks
                    isConnected = true;                         // Set the connection bool
                    continue;                                   // continue past the while loop
                }
            }
            catch(zmq::error_t err){                            // Catch any error
                cout << "ERROR: " << err.what() << endl;        // Output error
            }
            sleep(2);                                           // Sleep for 2s if not connected - then re-try
        }

        std::string posReq = "PPO";                             // Holds the position request
        std::string velReq = "PVE";                             // Holds the veLocity request
        zmq::message_t posRep;                                  // Holds the position reply.
        zmq::message_t velRep;                                  // Holds the veLocity reply.
        std::string::size_type posSize;     // alias of size_t
        std::string::size_type velSize;     // alias of size_t

        // DO-While loop to allow requests and replys while program is connected.
        do{
            // Get Table Positioning - Catch any error.
            try{
                // Send Position Request
                socket.send(zmq::buffer(posReq),zmq::send_flags::none);
                // Get Reply
                socket.recv(posRep,zmq::recv_flags::none);
            }
            catch(zmq::error_t err){
                // Catch any error and print it to console.
                cout << "ERROR: " << err.what() << endl;
            }
            // Get Table VeLocity - Catch any error.
            try{
                // Send VeLocity Request
                socket.send(zmq::buffer(velReq),zmq::send_flags::none);
                // Get Reply
                socket.recv(velRep,zmq::recv_flags::none);
            }
            catch(zmq::error_t err){
                // Catch any error and print it to console.
                cout << "ERROR: " << err.what() << endl;
            }

            // Data returned *should* be double type
            updateValues(std::stod(posRep.to_string(),&posSize),std::stod(velRep.to_string(),&velSize));

        }while(isConnected);

        // Clean up.
        socket.disconnect(ipAddr.c_str());
        socket.close();
        context.shutdown();
        context.close();
}

主要

#include "mainwindow.h"
#include <QApplication>

int main(int argc,char *argv[])
{
    QApplication a(argc,argv);
    MainWindow w;
    w.show();
    return a.exec();
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。